javascript - How do I call a Vue.js method from a link inside data table?
问题描述
I am building a Vue.js application using the Vue Webpack template. Within a component, I am initializing a Datatable like this:
<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>
This works fine and the table is rendered in the browser. My question - how do I call the openModal()
method from inside the data table? As you can see, I am trying to call the method in columns[2].render
but that doesn't work (likely because Vue does not compile the return string. How can I make this work?
解决方案
您可以在数据表回调上添加点击事件。希望这可以帮助
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');
}
}
推荐阅读
- django - 如何将一个应用程序中的视图调用到django中另一个应用程序中的模板
- python - 我的角色在这个 pygame 脚本中停止了跳跃,为什么?
- python - 如何检测和过滤时间序列数据的峰值?
- python - 如何使用 wtf.quick_form 为 Flask 表单中的隐藏字段设置值?
- sockets - 如何为node_redis js配置TCP连接超时
- c# - Mvc Core 动态绑定配置
- html - Bootstrap 响应式查询无法按照我想要的方式工作
- python - 使用带有标题的 np.savetxt 保存结构化的 numpy 数组
- git-merge - Git替换整个目录
- python - 计算一列在 Pandas 中包含某个值的次数