javascript - 如何从数据表内的链接调用 Vue.js 方法?

标签 javascript vue.js datatable

我正在使用 Vue Webpack 模板构建 Vue.js 应用程序。在组件内,我正在初始化 Datatable像这样:

<script>
export default {
  name: 'DataTable',

  mounted() {
    $('#datatable').dataTable({
      serverSide: true,
      ajax: {
        url: '/tableData',
      },
      processing: true,
      searching: false,
      pageLength: 25,
      order: [[0, 'desc']],
      columns: [
        {
          searchable: false,
          data: 'updatedAt',
          render: data => format(new Date(data), 'MMM Do, YYYY - h:mm:ss a'),
        },
        {
          orderable: false,
          data: 'user',
          render: (data) => {
            return `${data.firstName} ${data.lastName}`;
          },
        },
        {
          searchable: false,
          orderable: false,
          render() {
            return '<a href="javascript:void" @click="openModal">View Details</a>';
          },
        },
      ],
    });
  },

  methods: {
    openModal() {
      console.log('Open modal code goes here');
    }
  }
}
</script>

这工作正常并且表格在浏览器中呈现。我的问题 - 如何从数据表内部调用 openModal() 方法?正如您所看到的,我尝试调用 columns[2].render 中的方法,但这不起作用(可能是因为 Vue 不编译返回字符串。我怎样才能使其工作?

最佳答案

您可以在数据表回调上添加点击事件。 希望这有帮助

      mounted() {
               var self = this;
                $('#datatable').dataTable({
                    serverSide: true,
                    ajax: {
                        url: '/tableData',
                    },
                    processing: true,
                    searching: false,
                    pageLength: 25,
                    order: [[0, 'desc']],
                    columns: [
                        {
                            .........
                        },
                        { title: 'Action', targets:7, data: 'id',
                            "render":function(data, type, row, meta){
                                //data item in case you want to send any data
                                return '<a  href="javascript:void" data-item-id='+data+' class="open-item">Open Modal</a>';
                            }
                        }
                    ],
                    "drawCallback": function( settings ) {
                        $(".open-item").on( 'click', function (e) {
                            self.OpenModal(e.target.dataset.itemId);
                        });
                    }
                });
            },
            methods: {
                openModal:function() {
                    console.log('Open modal code goes here');
                }
            }

关于javascript - 如何从数据表内的链接调用 Vue.js 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51895034/

相关文章:

javascript - 如何在 HighCharts 上重绘系列?

laravel - dd() 在 vue-laravel 项目 axios 调用中不起作用。是否可以在 axios 调用中使用转储?

vue.js - 如何让 vuetify 显示 'mdiSvg' 图标?

ecmascript-6 - 在 VueJS + JSPM 中作为注释呈现的组件

javascript - 如何在数据表中添加附加特性/功能?

r - styleColorBar : have the size of the color bar be proportional to absolute values of a column

c# - 使用 (LINQ/Predicate) 将 DataTable 的所有列名放入字符串数组

javascript - JQUERY 旋转 img

javascript - ajax数据过滤不起作用

javascript - Uncaught ReferenceError : $ is not defined at iterateId