我正在研究数据表过滤。我从多个文本框中获取值并尝试应用"包含"过滤。以下是我的方法。
var counter2 = 2;
$('#selectionIN').on('change', function () {
fil3 = $(this).val();
});
$("#addButton2").click(function () {
if(counter2>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv2 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv2' + counter2);
newTextBoxDiv2.after().html('<label>Item Name #'+ counter2 + ' : </label>' +
'<input type="text" name="textbox2' + counter2 +
'" id="textbox2' + counter2 + '" value="" >');
newTextBoxDiv2.appendTo("#TextBoxesGroup2");
counter2++;
});
$("#removeButton2").click(function () {
if(counter2==2){
return false;
}
counter2--;
$("#TextBoxDiv2" + counter2).remove();
});
$("#getButtonValue2").click(function () {
var msg2 = '';
for(i=1; i<counter2; i++){
msg2 += $('#textbox2' + i).val();
msg2 +="|";
}
msg2=msg2.slice(0, -1);
if(fil3=="exact"){
var patt = msg2.replace(/[\[\]\/\{\}\(\)\*\+\?\.\\\^\$-]/g, "\\$&"); //escape regex chars except "|"
patt = patt.replace(/ /g, "\\\s"); //change space to "\s" to make fnFilter works
if (patt)
patt = "^(" + patt + ")$"; //enclose with parentheses only when pattern is not empty
console.log(patt);
$("#testTable").dataTable().fnFilter(patt, 1, true);
console.log(msg2);
}
else if(fil3=="Begin"){
$('#testTable').dataTable().fnFilter("^" + msg2 + "$", 1, true);
console.log(msg2);
}
else if(fil3=="contains"){
$('#testTable').dataTable().fnFilter("^.*" + msg2 + ".*$", 1, true);
console.log(msg2);
}
else{
$("#testTable").dataTable().fnFilter(msg2, 1, true);//Exact value, column, reg*/
console.log(msg2);
}
});
现在问题是,它正在过滤包含第一个文本框中提到的行,而对于第二个文本框,它只过滤以给定值开头的行。谁能告诉我哪里出错?
答案 0 :(得分:0)
我发现了错误。
我以前用过
else if(fil3=="contains"){
$('#testTable').dataTable().fnFilter("^.*" + msg2 + ".*$", 1, true);
console.log(msg2);
}
现在包括要在括号内匹配的值,如下所示:
$('#testTable').dataTable().fnFilter("^.*(" + msg2 + ").*$", 1, true);