如何在modal中显示v-for中被单击元素的vue数据

busg9geu  于 2022-12-23  发布在  Vue.js
关注(0)|答案(2)|浏览(178)

这是我的json的样子,我用v-for格式显示了一堆,我可以点击其中一个,我想用模态显示我点击的元素的数据。

[{
        "id": 1,
        "companyName": "test",
        "image": "https://mmelektronik.com.pl/wp-content/uploads/2017/10/Insert-logo.jpg.png",
        "location": "Warsaw",
        "salary": "10000",
        "skill": "Junior",
        "tags": "test",
        "jobDescription": "test",
        "title": "UI Designer"
    }

]    

Now I want to access only jobDescription and display it in the modal.
b-modal(hide-footer="", :id="id")
      template(#modal-title="")
        | Information
      .d-block.text-center
        p {{ want the jobDescription here }}
        b-button(variant="primary") Apply

这是我如何打开模态。

openModal(item) {
      this.offer = item;
      this.$bvModal.show(this.id);
    }

组件

b-container
    b-card.joblist__post.mt-2(
      v-for="offer in filteredOffers",
      :id="id"
      :key="offer.id",
      @click="openModal"
    )
      .d-flex
        .joblist.d-flex.w-100
          .joblist__info.d-flex.align-items-center
            .company-logo.d-flex.align-items-center.mr-3
              b-img.logo(:src="offer.image")
            .joblist-name.d-flex.flex-column.text-left
              h5.mb-0.font-weight-bold {{ offer.title }}
              .located.d-flex.align-items-center.mt-2.justify-content-start
                b-icon.mr-2(icon="geo-alt-fill")
                p.m-0.text-secondary.joblist-span {{ offer.location }}
                b-icon.mx-2(icon="person-fill")
                p.m-0.text-secondary.joblist-span {{ offer.companyName }}
                b-icon.mx-2(icon="bar-chart-fill")
     

    b-modal(hide-footer="", :id="id")
      template(#modal-title="")
        | Information 
      .d-block.text-center
        p {{offer.jobDescription}}
      b-button(variant="primary") Ok

export default {
  data() {
    return {
      search: "",
    };
  },
  computed: {
    ...mapState({
      offers: (state) => state.offer.offers,
      loading: (state) => state.offer.loading
    }),
 
    filteredOffers(){
      return this.offers.filter((offer) => {
        return offer.title.match(this.search);
      })
      
    },

  },
  methods: {
    ...mapActions({
      fetchOffers: "fetch",
    }),
    openModal(item) {
      this.offer = item;
      this.$bvModal.show(this.id);
    }
  },
  mounted() {
    this.fetchOffers();
    this.id = this._uid;
},
  
};
mi7gmzs6

mi7gmzs61#

这里有一个简单的方法,在数据中设置一个selectedItem属性,如下所示:

data: {
   return {
      selectedItem: {}
   }
}

并在元素上添加一个on click,如下图所示,其中您将单击的对象赋给selectedItem:

<button v-for="(e, i) in whateverDataArray" :key="i" @click="selectedItem=e">
    {{ e.companyName }}
</button>

然后简单地将selectedItem作为props传递给modal,这样当modal出现时,它将显示被单击的props selectedItem

**EDIT:**在您的情况下,您也应该从for循环中删除模态。另外,您不需要将selectedItem作为props传递给模态,因为您可以访问selectedItem

uurity8g

uurity8g2#

将您的data更改为:

data() {
  return {
    search: "",
    offer: null
  };
},

在模板中使用以下内容:

p {{ offer.jobDescription }}

并将click处理程序更改为:

@click="openModal(offer)"

一旦offer首先在数据中定义并从点击中传递,来自评论的建议应该起作用。
你的modal不应该在v-for里面,把它取出来,硬编码一个id:

b-modal(hide-footer="", id="offerModal")

打开它:

this.$bvModal.show('offerModal');

相关问题