我有一个html表,其中冻结了标题,冻结了左2列,并显示了对所有其他列(不是左2列)进行过滤的选项。当我自己完成固定列时,效果很好..但是当我添加代码以包括对列的下拉过滤时,固定列选项不起作用。知道我该如何解决吗?下面的代码:
<script>
$(document).ready(function () {
$('#primary').DataTable({
initComplete: function () {
this.api().columns([2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).every(function () {
var column = this;
var select = $('<select><option value="">Show All</option></select>')
.appendTo($(column.header()))
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
var val = $('<div/>').html(d).text();
select.append('<option value="' + val + '">' + val + '</option>');
});
});
},
paging: false,
sort: false,
scrollY: 500,
scrollX: true,
select: true,
fixedColumns: {
leftColumns: 2
},
fixedHeader: {
header: true,
},
});
});
</script>
html表格:
<table id="1" class="table table-striped">
@if (Model.1Function != null)
{
<thead>
<tr>
@foreach (DataColumn column in (Model.1Function as DataTable).Columns)
{
<th>@column.ColumnName.ToUpper()</th>//header row
}
</tr>
</thead>
<tbody>
@if ((Model.1Function as DataTable).Rows.Count > 0)
{
foreach (DataRow dr in (Model.1Function as DataTable).Rows)
{
<tr>
@foreach (DataColumn column in (Model.1Function as DataTable).Columns)
{
<td style="max-width:200px; white-space:normal">
@dr[column].ToString()
</td>//write one row at a time.
}
</tr>
}
}
else
{
var count = (Model.1Function as DataTable).Columns.Count;
<tr>
<td colspan='@count' style="color:red;">
No Data Found.
</td>
</tr>
}
</tbody>
}
else
{
<tr>
<td style="color:red;">
@(Model.1Function .HasErrors != false ? Model.1Function .HasErrors.ToString() : "")
</td>
</tr>
}
</table>
任何解决此问题的帮助都将非常有用。预先感谢。
答案 0 :(得分:0)
我不小心在this.api()。column数组中添加了额外的列。这就是它失败的原因。解决该问题后,即可使用。