【问题标题】:How do display vue data of clicked element from v-for in modal如何在模态中显示来自 v-for 的单击元素的 vue 数据
【发布时间】:2021-03-16 06:53:22
【问题描述】:

这就是我的 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;
},
  
};

【问题讨论】:

  • 您能创建一个可重现的示例吗? ??? stackoverflow.com/help/minimal-reproducible-example
  • 你有 v-for 列表作为按钮。您单击一个按钮,模态打开,并且您希望在模态中显示该按钮包含的来自 json 的信息。
  • 由于您在父级中设置this.offer,它应该是可用的。试试{{ offer.jobDescription }}
  • @Dan ,不会这样工作,当我这样做时,页面变为空白
  • 嗯,这是一个猜测,因为看起来您在父级中设置了 this.offer。但这只是一个猜测,因为您没有显示该组件。你应该证明这一点。也许可以直接从id 完成,但这似乎没有必要,取决于父项中定义的内容

标签: javascript vue.js vuejs2 vue-component bootstrap-vue


【解决方案1】:

这是您可以采取的一种简单方法。在数据中有一个selectedItem 属性,如下所示:

data: {
   return {
      selectedItem: {}
   }
}

并像这样在元素上添加一个点击,将点击的对象分配给 selectedItem:

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

然后只需将 selectedItem 作为道具传递给模态框,因此当模态框出现时,它将显示被点击的道具selectedItem

【讨论】:

    【解决方案2】:

    将您的 data 更改为:

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

    在您的模板中使用以下内容:

    p {{ offer.jobDescription }}
    

    并将click 处理程序更改为:

    @click="openModal(offer)"
    

    一旦offer首先在数据中定义并通过点击传递,来自 cmets 的这个建议应该会起作用。您确实在 openModal 操作中设置了 offer

    您的模态不应在v-for 内。把它拿出来,硬编码一个 id:

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

    打开它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-23
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2020-02-25
      • 2019-01-21
      • 2019-08-20
      相关资源
      最近更新 更多