icheck結合datatable使用方法及實現全選、反選功能
阿新 • • 發佈:2019-02-03
var table = $("#user_index").DataTable({
order: [
["1", 'asc']
], //按照發布時間降序排序
page: false,
serverSide:true,
info: true,
autoWidth: true,
searching:true,
ajax: "<?php echo APP_URL_ROOT?>/Test/get_datatable",
columns: [{
data: null
},{
data: "id",
},{
data: "name",
},/*{
data: "create_category_time",
},{
data: "ROLENAME",
},*/{
data: "null",
}],
columnDefs: [{
targets:0,
data: null,
defaultContent:"<input type ='checkbox' name='test' class='icheckbox_minimal' value=''>" ,
},{
targets: -1,
data: null,
defaultContent: "<a>編輯</a>|<a>刪除</a>",
},{
"orderable": false, "targets": [0,-1], //設定第一列和最後一列不可排序
}],
createdRow: function(row, data, index) {
$(row).data('id' ,data.id);
$(row).find('.icheckbox_minimal').first().val(data.id);
},
"fnDrawCallback": function(){
$("#all_checked").prop("checked",false);
},
"language": {
"processing": "處理中...",
"lengthMenu": "顯示 _MENU_ 項結果",
"zeroRecords": "沒有匹配結果",
"info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
"infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
"infoFiltered": "(由 _MAX_ 項結果過濾)",
"infoPostFix": "",
"search": "搜尋:",
"searchPlaceholder": "搜尋...",
"url": "",
"emptyTable": "表中資料為空",
"loadingRecords": "載入中...",
"infoThousands": ",",
"paginate": {
"first": "首頁",
"previous": "上頁",
"next": "下頁",
"last": "末頁"
},
"aria": {
paginate: {
first: '首頁',
previous: '上頁',
next: '下頁',
last: '末頁'
},
"sortAscending": ": 以升序排列此列",
"sortDescending": ": 以降序排列此列"
},
"decimal": "-",
"thousands": "."
}
});
//實現全選功能
$("#all_checked").click(function(){
$('[name=test]:checkbox').prop('checked',this.checked);//checked為true時為預設顯示的狀態
});
//實現反選功能
$("#checkrev").click(function(){
$('[name=test]:checkbox').each(function(){
this.checked=!this.checked;
});
});
$("#btn").click(function(){
var a = [];
$('input[name="test"]:checked').each(function(){
a.push($(this).val());
// console.log($(this).val());
});
console.log(a);
});