2016-11-19 73 views
1

我正在處理數據過濾。我從多個文本框中獲取值並嘗試應用「包含」過濾器。以下是我的方法。基於來自多個文本框的值進行過濾

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

我發現了錯誤。

$('#testTable').dataTable().fnFilter("^.*(" + msg2 + ").*$", 1, true); 

我用前

else if(fil3=="contains"){ 
    $('#testTable').dataTable().fnFilter("^.*" + msg2 + ".*$", 1, true); 
    console.log(msg2); 
} 

現在包含的值到支架如下內部匹配