1. 程式人生 > >bootstrap table 保留翻頁選中資料

bootstrap table 保留翻頁選中資料

$(function () {
  $('#exampleTable').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table',function(e,rows){
var datas = $.isArray(rows) ? rows : [rows]; // 點選時獲取選中的行或取消選中的行
examine(e.type,datas); // 儲存到全域性 Array() 裡
});
})
//exampleTable為bootstrap table名稱
var overAllIds = new Array();  //全域性陣列
function examine(type,datas){
if(type.indexOf('uncheck')==-1){
$.each(datas,function(i,v){
// 新增時,判斷一行或多行的 id 是否已經在數組裡 不存則新增 
overAllIds.indexOf(v.id) == -1 ? overAllIds.push(v.id) : -1;
});
}else{
$.each(datas,function(i,v){
overAllIds.splice(overAllIds.indexOf(v.id),1); //刪除取消選中行
});
}
}
//設定bootstrap table checkbox自動選中
columns : [
{
checkbox : true,
formatter : function(value, row, index) {
if (row.userId == $("#userId").val()) {
overAllIds.indexOf(row.id) == -1 ? overAllIds.push(row.id) : -1;
return {
checked: true//設定選中
};
}
}
},
  {
title : '操作',
width : '130',//設定列寬
field : 'id',
align : 'center'
  }
]