我在使用jQuery DataTables Individual Column Searching,在我的一个表格上,其中一个列包含复选框。jQuery DataTables搜索栏是一个复选框
HTML
<table id="NewTable" class="table table-bordered table-striped">
<thead>
<tr>
<th class="col-sm-1 text-center ">
@Html.DisplayNameFor(model => model.c_AMake.AMake)
</th>
<th class="text-center col-sm-1">
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.Test)
</th>
</tr>
</thead>
<tfoot id="NewTable-Foot" style="display: table-header-group">
<tr>
<th class="col-sm-1 text-center ">
@Html.DisplayNameFor(model => model.c_AMake.AMake)
</th>
<th class="text-center col-sm-1">
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.Test)
</th>
</tr>
</tfoot>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.c_AMake.AMake)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.Test)
</td>
</tr>
}
</table>
jQuery的
var newTable = $('#NewTable tfoot th').length;
// Setup - add a text input to each footer cell
$("#NewTable tfoot th").each(function (index) {
if (index !== newTable - 1) {
var title = $(this).text().trim();
$(this).html('<input type="text" class="form-control" placeholder="' + title + '"/>');
}
});
var newDTTable = $('#NewTable').DataTable();
newDTTable.columns().every(function() {
var that = this;
console.log(this.data());
$('input', this.footer()).on('keyup change',
function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
这显示我所说的,像这样的列:
但是,如果我开始在文本框中输入“True”,我的表刷新为“找不到匹配的记录”。不够公平,因为复选框在HTML渲染方面的值不是“true”,而只是说“checked =”选中了“。
但作为一个参数,当我做console.log(this.data())
时,我看到表中的每一行,这些值都会回到“True”,“False”,“False”。
有没有一种方法来搜索通过jQuery的数据表选中或未选中的复选框?
UPDATE
这里是我正在创建的搜索框:
$("#NewTable tfoot th").each(function (index) {
if (index !== newTable - 1) {
var title = $(this).text().trim();
$(this).html('<input type="text" class="form-control" placeholder="' + title + '"/>');
}
});
更新2
var newDTTable = $('#NewTable').DataTable({
dom: 'Bfrtip',
buttons: [
{
extend: 'excel',
exportOptions: {
columns: ':not(:last-child)'
}
}],
aoColumnDefs: [
{
"render": function (data, type, row) {
if (data.indexOf("checked") > -1) {
return data + "<input type='hidden' value='True' />";
}
else return data + "<input type='hidden' value='False' />";
},
"targets": 5
},
{ "bSortable": false, "aTargets": [2, 18] },
{ "bSearchable": false, "aTargets": [4,7,8,9,10,11,12,13,14,15,16,17,18] }
]
});
newDTTable.columns().every(function (colIdx) {
var that = this;
console.log(this.data());
$('input', this.footer()).on('keyup change',
function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
这里是我的DataTable的我的定义/创建..有几个隐藏的列在这个问题的背景下无关紧要。
Downvote没有评论为什么? –
也许有人不打扰阅读?似乎有趣的问题。我会试试看。 –
你真的想通过文本框搜索条目吗?如果做一个用于过滤的复选框,它会更有意义吗? –