【发布时间】:2021-02-13 00:19:31
【问题描述】:
我尝试实现一个下拉过滤器来过滤我的数据表,如下所示:Individual column searching (select inputs)。
这也适用于我尝试使用 UIHints 设置输出样式的那一刻。我认为问题在于我的 javascript 代码会查找旧的单元格内容,而实际的单元格内容已被我的 UIHint 更改。让我的下拉过滤器正常工作的最佳方法是什么?
提前谢谢你!
这是我的代码的一部分:
Status.cshtml(状态显示模板)
@model short?
@if (Model == 0)
{
<b>Not Set</b>
}
else if (Model == 1)
{
<b>Created</b>
}
else if (Model == 2)
{
<b>Approved</b>
}
else if (Model == 3)
{
<b>Done</b>
}
Index.cshtml(查看)
@model IEnumerable<data>
<h2>Data</h2>
<table class="display" id="db_table" style="width:100%">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.month)
</th>
<th>
@Html.DisplayNameFor(model => model.year)
</th>
<th>
@Html.DisplayNameFor(model => model.country)
</th>
<th>
@Html.DisplayNameFor(model => model.status)
</th>
</tr>
</thead>
<tbody>
@for (var i = 1; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model.ElementAt(i).month)
</td>
<td>
@Html.DisplayFor(modelItem => Model.ElementAt(i).year)
</td>
<td>
@Html.DisplayFor(modelItem => Model.ElementAt(i).country)
</td>
<td>
@Html.DisplayFor(modelItem => Model.ElementAt(i).status)
</td>
</tr>
}
</tbody>
</table>
@section scripts
{
<script src="~/Scripts/Custom/datatable.js"></script>
}
datatable.js
$(document).ready(function () {
$('#db_table').DataTable({
ordering: false,
paging: false,
lengthChange: false,
initComplete: function () {
this.api().columns(3).every(function () {
var column = this;
var select = $('<select><option value=""></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) {
select.append('<option value="' + d + '">' + d + '</option>')
});
});
}
});
});
【问题讨论】:
标签: javascript jquery asp.net-mvc datatables