javascript - 将 @click 事件绑定(bind)到数组中的 href

标签 javascript vue.js

我有以下代码,用于填充数据表。 对于数据表中的每一行,我都有一个 update关联。单击 update应该调用ShowModal()方法。

如何调用ShowModal()从这里<a>

   <template>
     <table id="buildings" class="mdl-data-table" width="100%"></table>
    </template>

    methods: {
        ShowModal(buildingId){
          // do something here...
        }, 
        listBuildings() {                      
         axios
            .get("https://localhost:44349/api/Building/List", headers)
            .then(response => {
              response.data.forEach(el => {            
                this.dataset.push([
                  el.buildingId,
                  el.projectName,
                  el.city,                 
                  `<a click='${this.ShowModal(el.buildingId)}'>Update</a>`  // I was trying this...        
               ]);

            $("#buildings").DataTable({
                  data: this.dataset});

           });
         }

最佳答案

我认为你想做的不是 Vue 方式,而是 JQuery 方式。所以让我向你提出更正确的代码(恕我直言)

<template>
     <table id="buildings" class="mdl-data-table" width="100%">
        <tr
            v-for="(item, index) in dataset"
            :key=(index) // or building id if u like
        >
            <td>{{ item.buildingId }}</td>
            <td>{{ item.projectName }}</td>
            <td>{{ item.city }}</td>
            <td
                @click="showModal(item.buildingId)"
            >
            click me! A not required here. U can use div if u want to have not cell clicked and style it with css
            </td>
        </tr>
     </table>
</template>
    methods: {
        showModal(buildingId){
          // do something here...
        }, 
        listBuildings() {                      
         axios
            .get("https://localhost:44349/api/Building/List", headers)
            .then(response => {
                // I suppose u have as responce here array of objects?
                this.dataset = response.data
            })
            .catch(e => console.warn(e));
        }

关于javascript - 将 @click 事件绑定(bind)到数组中的 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53961331/

相关文章:

vue.js - 在 Vue js 中更改参数化子组件时如何防止父组件重新加载

javascript - 如何使用 JQuery Fancybox2 将嵌套输入隐藏子项的值获取到自定义内容中

javascript - 如何在具有相同类名的div中动态设置不同的<a href>和<img src>?

javascript - Chrome/FF 中的竞争条件

javascript - vue.js中如何向组件发送数据?

javascript - 如何从 vue.js 中的并行组件动态更新 var

javascript - Vuejs组件无法访问自己的数据

javascript - 如何从Vue中创建的axios函数访问属性

javascript - 如何克隆实际的 iFrame 内容,而不仅仅是 iFrame 标签内容?

javascript - 在 MVC4 中,如何在 Razor 文件中创建脚本包