如何在模态中显示来自 v-for 的单击元素的 vue 数据
How do display vue data of clicked element from v-for in modal
这就是我的 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;
},
};
您可以采用以下简单方法。在数据中有一个 selectedItem
属性,如下所示:
data: {
return {
selectedItem: {}
}
}
并像这样在元素上添加点击,将点击的对象分配给 selectedItem:
<button v-for="(e, i) in whateverDataArray" :key="i" @click="selectedItem=e">
{{ e.companyName }}
</button>
然后只需将 selectedItem 作为 props 传递给模态框,这样当模态框出现时,它就会显示被点击的 props selectedItem
!
将您的 data
更改为:
data() {
return {
search: "",
offer: null
};
},
在您的模板中使用以下内容:
p {{ offer.jobDescription }}
并将 click
处理程序更改为:
@click="openModal(offer)"
一旦 offer
首先在数据中定义并从点击传递,来自评论的建议应该有效。您确实在 openModal
操作中设置了 offer
。
您的模态框不应在 v-for
内。拿出来硬编码一个id:
b-modal(hide-footer="", id="offerModal")
打开它:
this.$bvModal.show('offerModal');
这就是我的 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;
},
};
您可以采用以下简单方法。在数据中有一个 selectedItem
属性,如下所示:
data: {
return {
selectedItem: {}
}
}
并像这样在元素上添加点击,将点击的对象分配给 selectedItem:
<button v-for="(e, i) in whateverDataArray" :key="i" @click="selectedItem=e">
{{ e.companyName }}
</button>
然后只需将 selectedItem 作为 props 传递给模态框,这样当模态框出现时,它就会显示被点击的 props selectedItem
!
将您的 data
更改为:
data() {
return {
search: "",
offer: null
};
},
在您的模板中使用以下内容:
p {{ offer.jobDescription }}
并将 click
处理程序更改为:
@click="openModal(offer)"
一旦 offer
首先在数据中定义并从点击传递,来自评论的建议应该有效。您确实在 openModal
操作中设置了 offer
。
您的模态框不应在 v-for
内。拿出来硬编码一个id:
b-modal(hide-footer="", id="offerModal")
打开它:
this.$bvModal.show('offerModal');