我有以下代码
<script type="text/javascript">
function EditTemmplate(o) {
return "<a class='btn btn-primary' href=\"/Customer/Edit/" + o + "\">Edit</a>" + "</li>";
}
$(document).ready(function () {
var table = $('#table').DataTable({
"dom": 'Bfrtip',
"order": [2, "asc"],
"createdRow": function (row, data, dataIndex) {
if (data.IsActive === false) {
$(row).addClass('inactive');
}
},
"serverSide": true,
"processing": true,
"paging": true,
"bLengthChange": false,
"iDisplayLength": 10,
"ajax": {
"url": "/Customer/Get",
"type": "POST",
"dataType": "json",
"complete": function (response) {
console.log(response);
}
},
"buttons": [
{
extend: 'excel',
text: 'Download Excel',
className: "btn",
exportOptions: {
columns: [1, 2, 3]
}
}
],
"columns":
[
{ data: "ID", title: "", render: EditTemmplate, bSortable: false, searchable: false },
{ data: "CompanyName", title: "Company Name", "searchable": false },
{ data: "Contact", title: "Contact Person" },
{ data: "IDNumber", title: "ID/Reg #" },
{ data: "EmailAddress", title: "Email" },
{ data: "ContactNumber", title: "Contact #" }
]
});
var text = "<tr>";
$('#table thead th').each(function () {
var title = $(this).text();
//Test here if column should be searched
text += '<td><input type="text" class="form-control" placeholder="Search ' + title + '" /></td>'
});
text += "</tr>";
$("#table thead").append(text);
// Apply the search
table.columns().every(function (index) {
var that = this;
$('input', this.footer()).on('keyup change', function () {
that.search(this.value).draw();
});
});
});
</script>
问题是我不知道如何测试列是否可搜索,没有css类被添加到列标题中,其次我不知道我是否遵循正确的食谱
答案 0 :(得分:1)
您可以通过在表格中搜索仅在前两列中的值来非编程地进行测试 - 如果它命中,则出现错误!
在代码中,您可以检查table.settings()[0].aoColumns