【发布时间】:2016-04-06 08:34:24
【问题描述】:
我需要使用 DataTable 插件在表格中添加“全选”复选框。我没有找到标准方法,我为此手动使用添加。好的,但是如果我尝试使用本地化(“语言”属性),我的“全选”复选框就会消失。我尝试修复是通过在 DataTable 库中添加我的代码,但这是不好的方法。
<table id="devices" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th style="padding:8px; text-align:center;">
<input type='checkbox' class='minimal check-all' id='check-all-device' name='check-all-device'></input>
</th>
<th>{% trans "STATUS" %}</th>
<th>{% trans "DEVICE NAME" %}</th>
<th>{% trans "ACTIONS" %}</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>{% trans "STATUS" %}</th>
<th>{% trans "DEVICE NAME" %}</th>
<th>{% trans "ACTIONS" %}</th>
<th></th>
</tr>
</tfoot>
<tbody id="devices-table-rows">
{% for device in object_list %}
{% include "device_add_row.html" %}
{% endfor %}
</tbody>
</table>
在 javascript 上添加选择处理程序:
devicesTable = $('#devices').DataTable({
// disable sorting first column
'aoColumnDefs': [{
'bSortable': false,
'aTargets': [0] /* 1st one, start by the right */
}],
stateSave: false
});
// Action's select insert in to search row
$('#devices_filter').append($('#devices-actions'));
// Settings Check ALL
var firstTh = $($($('#devices > thead').find('tr')[0]).find('th')[0]);
firstTh.removeClass("sorting_asc");
//iCheck for checkbox and radio inputs
$('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
checkboxClass: 'icheckbox_minimal-blue',
radioClass: 'iradio_minimal-blue'
});
// Check handlers All
var checkAll = $('input.check-all');
var checkboxes = $('input.check-single');
checkAll.on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
checkboxes.iCheck('check');
} else {
checkboxes.iCheck('uncheck');
}
});
checkboxes.on('ifChanged', function(event){
if(checkboxes.filter(':checked').length == checkboxes.length) {
checkAll.prop('checked', 'checked');
} else {
checkAll.removeProp('checked');
checkAll.prop('checked', false);
}
checkAll.iCheck('update');
});
结果 - 好的!:
为表格本地化添加使用语言:
var languageUrl = "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json";
}
devicesTable = $('#devices').DataTable({
// disable sorting first column
'aoColumnDefs': [{
'bSortable': false,
'aTargets': [0] /* 1st one, start by the right */
}],
stateSave: false,
language: {
"url": languageUrl
}
});
我的设置已重置:
【问题讨论】:
标签: javascript jquery html datatables icheck