场景
在进行编辑时DataTables中第一列的checkbox回显默认选中。
效果
实现
dataTables赋值代码
columns: [
{ data: 'id' ,
"orderable" : false},
{ data: 'goodsLocationNumber' },
{ data: 'locationTypeName' ,
"orderable" : false},
{ data: 'saveMaterialTypeName' ,
"orderable" : false},
{ data: 'materielStatusName',
"orderable" : false},
{ data: 'shelveName' ,
"orderable" : false},
{ data: 'remark',
"orderable" : false }
],columnDefs: [{
// 指定第1列,从0开始,0表示第一列,1表示第二列……
"targets": 0,
"bSortable": false,
"render": function(data, type, row, meta) {
if (row.isSelected == 1){
return '<input type="checkbox" class="checkchild" onclick="childClick(this)" checked="checked" value="' + row.id + '" />'
}
return '<input type="checkbox" class="checkchild" onclick="childClick(this)" value="' + row.id + '" />'
}
}],
完整初始化DataTables代码
$(document).ready(function() {
//获取下拉框的值,判断是否为空,不为空则是编辑,那么就要初始化dataTables
var selected = $("#businessInitiator option:selected").val();
if(selected!=""){
var t = $('#example').DataTable({
"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": "."
},
"processing": true,
"searching" : false,
"pageLength": 100,
"serverSide": true,
"columnDefs": [ {
"searchable": false,
"orderable": false,
"targets": "_all",
}],
"dom": '<"top">rt<"bottom"flpi><"clear">',
columns: [
{ data: 'id' ,
"orderable" : false},
{ data: 'goodsLocationNumber' },
{ data: 'locationTypeName' ,
"orderable" : false},
{ data: 'saveMaterialTypeName' ,
"orderable" : false},
{ data: 'materielStatusName',
"orderable" : false},
{ data: 'shelveName' ,
"orderable" : false},
{ data: 'remark',
"orderable" : false }
],columnDefs: [{
// 指定第1列,从0开始,0表示第一列,1表示第二列……
"targets": 0,
"bSortable": false,
"render": function(data, type, row, meta) {
if (row.isSelected == 1){
return '<input type="checkbox" class="checkchild" onclick="childClick(this)" checked="checked" value="' + row.id + '" />'
}
return '<input type="checkbox" class="checkchild" onclick="childClick(this)" value="' + row.id + '" />'
}
}],
"ajax": function (data, callback, setting) {
data.editActionId = $("#refundOrderId").val();
data.selected=selected;
$.ajax({
type: 'POST',
url: "/busGoodsLocation/getRejectsLocationsList",
cache: false, //禁用缓存
//async: true,
data: JSON.stringify(data), //传入组装的参数
contentType: "application/json",
dataType: "json",
success: function (result) {
callback(result);
}
})
}
});
}
});