我目前正在使用 JQuery 数据表来存储信息并使用 MD 数组填充它。
但如果满足条件,我想默认将复选框属性设置为 :checked
。我尝试了各种方法,例如 .addclass
和 .addprop
来找到任何解决方法,但我无法让它按照我需要的方式工作。
E.G 因此,如果值禁用,则不执行任何操作,否则如果启用,则将复选框值设置为 :checked
。
我已附上JSfiddle复制我遇到的问题。
代码如下:
HTML
<table id="userTable" class="display" width="100%">
<thead>
<tr>
<th>Enable/Disable</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Javascript/JQuery
jQuery(function($) {
data = [
['User_488', 'User 1', 'disable'],
['User_487', 'User 2', 'disable'],
['User_477', 'User 3', 'disable'],
['User_490', 'User 4', 'disable'],
['1000', 'User 5', 'enable'],
['1001', 'User 6', 'enable'],
['1002', 'User 7', 'enable'],
['1004', 'User 8', 'enable']
]
var t = $('#userTable').DataTable({
'columnDefs': [{
'targets': 0,
'searchable': false,
'orderable': false,
'className': 'dt-body-center',
'render': function(data, type, full, meta) {
return '<input type="checkbox" class="checkbox_check">';
}
}],
order: []
});
function checkbox() {
t.clear();
if (data) {
for (var i = 0; i < data.length; i++) {
var number = data[i][0];
var name = data[i][1];
var statustemp = "";
var resarr = new Array(statustemp, number, name);
if (status === "disable" || status == null) {
t.row.add(resarr).draw(false);
} else {
t.row.add(resarr).draw(false);
}
}
t.draw(false);
}
};
checkbox();
});
以前有人遇到过这个问题吗?
最佳答案
查看attr
:
'columns': [
{
"title": "Enable/Disable",
"render": function(data, type, row, meta){
var checkbox = $("<input/>",{
"type": "checkbox"
});
if(row[2] === "enable"){
checkbox.attr("checked", "checked");
checkbox.addClass("checkbox_checked");
}else{
checkbox.addClass("checkbox_unchecked");
}
return checkbox.prop("outerHTML")
}
},{
"title": "Number",
"render": function(data, type, row, meta){
return row[0];
}
},{
"title": "Name",
"render": function(data, type, row, meta){
return row[1];
}
}
]
工作中JSFiddle
关于javascript - JQuery Datatables 将复选框设置为选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37257711/