英文:
Datatables column select filtering - how to disable filtered out values in select box?
问题
Sure, here's the translated content without the code:
问题:如何修改JavaScript以动态更改筛选SELECT菜单,以仅显示来自过滤选择的值。例如,选择了东京后,它现在应该限制“职位”为“会计师、集成专家、支持工程师、区域营销”:
(禁用“无效”选择值也是一种选择,可以使用<option value="..." disabled>...</option>
)
英文:
Using the datatables.net example Individual column searching (select inputs) with the example javascript to append SELECT menus into the footer:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function () {
$('#example').DataTable({
initComplete: function () {
this.api()
.columns()
.every(function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.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>');
});
});
},
});
});
<!-- end snippet -->
Question:
how to modify the JavaScript to dynamically change the filter SELECT menus to only show values from the filtered selection. For example, after selecting Tokyo it should now limit "Position" to "Accountant, Integration Specialist, Support Engineer, Regional Marketing":
(Disabling the "invalid" select values would be an option as well using <option value="..." disabled>...</option>
)
答案1
得分: 1
I was able to solve this for my use case. See @andrewJames comment's above about possible UI issue and how to get around it.
In my use case it works fine to disable SELECT OPTIONS that would result in 0 records shown. I'm also using a "reset filter" button.
$(document).ready(function () {
$('#example').DataTable({
initComplete: function () {
this.api()
.columns()
.every(function () {
var column = this;
// small change to add ids to the SELECT boxes
var column_idx = column.index()
var select = $('')
.appendTo($(column.footer()).empty())
.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>');
});
});
},
});
});
// gets called on first load and every time filter or search changes
footerCallback: function ( row, data, start, end, display ) {
// loop over all columns - hard coded in my example
for (var col = 1; col < 15; col++) {
column_id = 'column_' + col
// retrieve the unique column value after a filter has been applied
column_values = api.column( col, {search:'applied'} ).data().unique()
// get the select box by id
var sel = document.getElementById(column_id)
// on first page load select box isn't rendered yet
if (sel != null){
op = sel.getElementsByTagName('option');
// disable all select options that are not included in the "available" values
// we start at 0 because we don't want to disable the blank default
for (var i = 1; i < op.length; i++) {
if (Array.from(column_values).includes(op[i].value)){
op[i].disabled = false
} else {
op[i].disabled = true
}
}
}
}
}
英文:
I was able to solve this for my use case. See @andrewJames comment's above about possible UI issue and how to get around it.
In my use case it works fine to disable SELECT OPTIONS that would result in 0 records shown. I'm also using a "reset filter" button
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function () {
$('#example').DataTable({
initComplete: function () {
this.api()
.columns()
.every(function () {
var column = this;
// small change to add ids to the SELECT boxes
var column_idx = column.index()
var select = $('<select id="column_' + column_idx + '"><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.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>');
});
});
},
});
});
// gets called on first load and every time filter or search changes
footerCallback: function ( row, data, start, end, display ) {
// loop over all columns - hard coded in my example
for (var col = 1; col < 15; col++) {
column_id = 'column_' + col
// retrieve the unique column value after a filter has been applied
column_values = api.column( col, {search:'applied'} ).data().unique()
// get the select box by id
var sel = document.getElementById(column_id)
// on first page load select box isn't rendered yet
if (sel != null){
op = sel.getElementsByTagName('option');
// disable all select options that are not included in the "available" values
// we start at 0 because we don't want to disable the blank default
for (var i = 1; i < op.length; i++) {
if (Array.from(column_values).includes(op[i].value)){
op[i].disabled = false
} else {
op[i].disabled = true
}
}
}
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论