ste*_*esu 3 javascript jquery datatables vue.js vuejs2
我已经为 VueJS 创建了一个围绕 jQuery DataTables 的轻量级包装器,如下所示:
<template>
<table ref="table" class="display table table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th v-for="(column, index) in columns">
{{ column.name }}
</th>
</tr>
</thead>
</table>
</template>
<script>
export default {
props: ['columns', 'url'],
mounted: function () {
$(this.$refs.table).dataTable({
ajax: this.url,
columns: this.columns
});
// Add any elements created by DataTable
this.$compile(this.$refs.table);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我正在像这样使用数据表:
<data-table
:columns="
[
{
name: 'County',
data: 'location.county',
},
{
name: 'Acres',
data: 'details.lot_size',
},
{
name: 'Price',
data: 'details.price',
className: 'text-xs-right',
},
{
name: 'Actions',
data: null,
render: (row) => {
return "\
<a @click='editProperty' class='btn btn-warning'><i class='fa fa-pencil'></i> Edit</a>\
";
}
},
]
"
url="/api/properties"
></data-table>
Run Code Online (Sandbox Code Playgroud)
请注意 Actions 列的“render”方法。此函数运行良好并按预期呈现按钮,但是@click处理程序不起作用。
环顾四周,我发现了两个没有帮助的链接:
VueJS GitHub 存储库上的问题 254为 VueJS 1.0(使用this.$compile)提供了解决方案,但这在 VueJS 2.0 中已被删除
Will Vincent 的一篇博客文章讨论了如何在本地数据动态更改时重新渲染 DataTable,但没有提供将处理程序附加到渲染元素的解决方案
如果渲染的元素无法编译和挂载,只要我可以DataTable在单击时运行组件的方法就可以了。也许是这样的:
render: (row) => {
return "\
<a onclick='Vue.$refs.MyComponent.methods.whatever();' />\
";
}
Run Code Online (Sandbox Code Playgroud)
有没有这样的方法可以从 Vue 上下文之外调用方法?
这符合您的最小可行解决方案。
在您的列定义中:
render: function(data, type, row, meta) {
return `<span class="edit-placeholder">Edit</span>`
}
Run Code Online (Sandbox Code Playgroud)
在您的 DataTable 组件中:
methods:{
editProperty(data){
console.log(data)
}
},
mounted: function() {
const table = $(this.$refs.table).dataTable({
ajax: this.url,
columns: this.columns
});
const self = this
$('tbody', this.$refs.table).on( 'click', '.edit-placeholder', function(){
const cell = table.api().cell( $(this).closest("td") );
self.editProperty(cell.data())
});
}
Run Code Online (Sandbox Code Playgroud)
示例(使用不同的 API,但想法相同)。
这是使用 jQuery,但您已经在使用 jQuery,所以感觉并没有那么糟糕。
我玩了一些游戏,试图让一个组件挂载render到数据表的函数中并取得了一些成功,但我对 DataTable API 不够熟悉,无法使其完全工作。最大的问题是 DataTable API 期望渲染函数返回一个字符串,这是......限制。API也非常令人恼火,没有给你当前所在单元格的引用,这似乎很明显。否则你可以做类似的事情
render(columnData){
const container = document.createElement("div")
new EditComponent({data: {columnData}).$mount(container)
return container
}
Run Code Online (Sandbox Code Playgroud)
此外,渲染函数以多种模式调用。我能够将一个组件渲染到单元格中,但不得不用该模式玩很多游戏,等等。这是一个尝试,但它有几个问题。我链接它是为了让你知道我在尝试什么。也许你会有更多的成功。
最后,您可以将组件安装到由 DataTable 呈现的占位符上。考虑这个组件。
const Edit = Vue.extend({
template: `<a @click='editProperty' class='btn btn-warning'><i class='fa fa-pencil'></i> Edit</a>`,
methods:{
editProperty(){
console.log(this.data.name)
this.$emit("edit-property")
}
}
});
Run Code Online (Sandbox Code Playgroud)
在您安装的方法中,您可以执行以下操作:
mounted: function() {
const table = $(this.$refs.table).dataTable({
ajax: this.url,
columns: this.columns
});
table.on("draw.dt", function(){
$(".edit-placeholder").each(function(i, el){
const data = table.api().cell( $(this).closest("td") ).data();
new Edit({data:{data}}).$mount(el)
})
})
Run Code Online (Sandbox Code Playgroud)
}
这将在每个占位符的顶部渲染一个 Vue,并在绘制时重新渲染它。这是一个例子。