使用vue和datatables进行表格的服务器端分页实例代码
ronyongxian 人气:0想法很简单,用vue生成表格的行,datatables生成分页信息,不想过程曲折,特此记录。
datatables端代码:
$('#dataTables-example').DataTable({ responsive: true, "serverSide" : true, "ajax": function (data, callback, settings) { postJson( "/AccessControlSystem/user/selectByPrimary", {'pageSize':data.length,'pageNo':data.starthttps://img.qb5200.com/download-x/data.length+1}, function(result){ callback({'draw':data.draw,'recordsTotal':userCount,'recordsFiltered':userCount,'data':[]}); $("#userList").html(""); getRoleForUser(result.data); rendorUserList(result.data); } ); } });
vue端代码:
//用户列表 var UserListComponent = Vue.extend({ template: `<tbody id="userList"> <tr v-for="(user, index) in userList" v-bind:class="index%2==0?'odd':'even'"> <td>{{user.name}}</td> <td> <label v-for="role in user.roleList" class="checkbox-inline"> <input type="checkbox" v-bind:value="role.id" disabled v-model="role.checked">{{role.name}} </label> </td> <td>{{user.createTime}}</td> <td class="center"><button type="button" class="btn btn-primary btn-xs" v-on:click="editUser(user.id)">修改</button></td> <td class="center"><button type="button" class="btn btn-primary btn-xs" v-on:click="deleteUser(user.id)">删除</button></td> </tr> </tbody>`, data: function () { return {'userList':[]}; }, methods: { editUser:function(id){}, deleteUser:function(id){} } }); function rendorUserList(userList){ var userListComponent = new UserListComponent(); userListComponent.userList = userList; userListComponent.$mount('#userList'); }
重点在rendorUserList函数中,每次生成表格行不能复用已有的vue实例,需要先destroy,再重新生成vue实例,否则无法正常显示第1页后面的页。
不知为何,希望懂原理的高手告知。
加载全部内容