将 select2 与数据表一起使用以筛选多个值



我目前正在尝试使用 select2 下拉列表过滤数据表的每一列。

我的代码目前正确过滤,选择标签上没有多个标签,但是一旦我将多个值添加到我的选择标签,我就会收到以下错误:

TypeError: a.replace is not a function

我一直在尝试调整以下数据表javascript: 链接

这是我目前拥有的:

$('#caseTable').DataTable( {
initComplete: function () {
var x = 0;
this.api().columns().every( function () {
var column = this;
var select = $('<select class="search2" multiple="multiple"><option value=""></option></select>')
.appendTo( $('.dropdown'+x)) 
.on( 'change', function () { 
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false ) //find this value in this column, if it matches, draw it into the table.
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option id="'+d+'"value="'+d+'">'+d+'</option>' )
} );
x++   
} );
$(".search2").select2();
}
} );

编辑:我设法修复了它。我需要将多个值存储到一个数组中,然后将这些值通过管道组合在一起以搜索每个单独的值。 以下是我对.on('change'(函数所做的更改。

.on( 'change', function () { //when an option is selected
var val = new Array();
//set val to current element in the dropdown.
val = $(this).val();
if (val.length > 1){
valString = val.toString();
valPiped =  valString.replace(/,/g,"|")
column
.search( valPiped ? '^'+valPiped+'$' : '', true, false ) //find this value in this column, if it matches, draw it into the table.
.draw();
} else if (val.length == 1) {
column
.search( val ? '^'+val+'$' : '', true, false ) //find this value in this column, if it matches, draw it into the table.
.draw();
} else {
column
.search('',true,false)
.draw();
}
} );

我设法修复了它。我需要将多个值存储到一个数组中,然后将这些值通过管道组合在一起以搜索每个单独的值。以下是我对.on('change'(函数所做的更改。

.on( 'change', function () { //when an option is selected
var val = new Array();
//set val to current element in the dropdown.
val = $(this).val();
if (val.length > 1){
valString = val.toString();
valPiped =  valString.replace(/,/g,"|")
column
.search( valPiped ? '^'+valPiped+'$' : '', true, false ) //find this value in this column, if it matches, draw it into the table.
.draw();
} else if (val.length == 1) {
column
.search( val ? '^'+val+'$' : '', true, false ) //find this value in this column, if it matches, draw it into the table.
.draw();
} else {
column
.search('',true,false)
.draw();
}
} );

最新更新