vue datatables 服务端分页

vue渲染生成表格的行极为便利,datatables处理分页的样式是一把好手,于是将两者结合。

从服务器获取数据后,在$('#dataTables-example').DataTable() 中自定义渲染方法。
根据服务器返回的分页数据用datatables的callback方法生成分页样式,再根据服务器返回的列表数据用vue component生成行。

 

datatables代码:

 

$('#dataTables-example').DataTable({
            responsive: true,
            "serverSide" : true,  
            "ajax": function (data, callback, settings) {
            	postJson(
            			"/AccessControlSystem/user/selectByPrimary",
            			{'pageSize':data.length,'pageNo':data.start/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');	
}

 

 

 

每次vue生成一页数据时都需要重新new Component(),否则无法正常显示第1页后面的页, 应是vue和datatables有冲突。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值