【发布时间】:2015-12-21 17:34:18
【问题描述】:
我正在尝试实现 Vue.js + jQuery 的 DataTables,但发生了一件奇怪的事情。
在 Firefox 上检查这个小提琴 (不适用于 chrome):
http://jsfiddle.net/chrislandeza/xgv8c01y/
当我更改 DataTable 的状态时(例如排序、搜索等):
- 列表中新添加的数据消失
- DOM 没有读取指令或 vue 属性
我很确定任何尝试混合使用 vue.js+datatables 的人都遇到过这个问题。你是怎么解决这个问题的?
或者是否有一个纯 Vue.js 脚本/插件具有与 jquery 的 DataTable 相同(或接近)的功能? (分页、搜索、排序、要显示的条目数等)。
这是上面小提琴中的代码:
HTML:
<div class='container-fluid' id="app">
<div class='row'>
<div class='col-md-9'>
<table class="table table-bordered" id="app-datatable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-repeat="user: users">
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
<td>
<button type="button" v-on="click: foo(user)">Action</button>
</td>
</tr>
</tbody>
</table>
</div>
<div class='col-md-3'>
<div class="form-group">
<label>Name</label>
<input type="text"
class="form-control"
v-model="newUser.name"
>
</div>
<div class="form-group">
<label>Age</label>
<input type="name"
class="form-control"
v-model="newUser.age"
>
</div>
<button type="submit" class="btn btn-primary" v-on="click: addUser()">Add</button>
</div>
</div>
</div>
JavaScript:
$(document).ready(function () {
var dT = $('#app-datatable').DataTable();
});
var vm = new Vue({
el: '#app',
data: {
newUser: {},
users: [
{name: 'Chris', age: 1},
{name: 'John', age: 2}
]
},
methods:{
addUser: function(){
this.users.push(this.newUser);
this.newUser = {};
},
foo: function(user){
console.log(user.name);
}
}
});
非常感谢任何建议。
【问题讨论】:
-
小提琴不起作用,因为您直接引用 githubusercontent - 您应该使用 rawgithub.com -> jsfiddle.net/cLd46juf 请参阅 Reference GitHub file in jsFiddle
-
这个问题与在 Angular 中使用 jQuery 样式的 dataTables 时相同。两者都试图操纵 DOM,vue 正在赢得这场战斗。遗憾的是,我认为没有简单的开箱即用解决方案。
标签: javascript jquery datatables vue.js