Search in two columns with multiple values separated by a pipe

Search in two columns with multiple values separated by a pipe

adf_espoiradf_espoir Posts: 4Questions: 1Answers: 0

I'd like to filter my table with a multiple values "condition1|condition2|..." and on two columns 0 and 4.
` $.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
var comp = $('#search-me').val();
let vName = "";
let vFatherName = "";
if (data[0]) {
vName = data[0].toLowerCase();
}
if (data[4]) {
vFatherName = data[4].toLowerCase();
}

        if (comp !== "") {
           let arrComp = comp.split('|');
           if (arrComp.length > 1) {
                arrComp.forEach((item) => {
                    if (vName.indexOf(item.toLowerCase()) > 0 || vFatherName.indexOf(item.toLowerCase()) > 0) {
                        console.log(item);
                        return true;
                    } else {
                        item == "";
                    }
                });
           } else {
               return vName.indexOf(comp.toLowerCase()) > 0 || vFatherName.indexOf(comp.toLowerCase()) > 0;
           }
        } else  {
            return comp == ""; 
        }
    });`

Please help me.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 16,400Questions: 25Answers: 3,891

    Here i sone option:
    http://live.datatables.net/wenaxuti/226/edit

    It uses an OR search between the Position and Office columns. You cna change it to AND if that is a requirement.

    Kevin

  • adf_espoiradf_espoir Posts: 4Questions: 1Answers: 0

    I try like this, but i've no solution
    http://live.datatables.net/buculiqa/1/edit

  • kthorngrenkthorngren Posts: 16,400Questions: 25Answers: 3,891
    Answer ✓

    You are getting this console error:

    Uncaught ReferenceError: searchData is not defined

    You changed the second parameter in the search plugin from searchData to a. I changed it back.

    You are pointing to the wrong column. I changed filters.includes(searchData[3]) to filters.includes(searchData[4]).

    Now this search works:

    Tiger Nixon|Donna Snider
    

    http://live.datatables.net/wejacuqe/1/edit

    Kevin

  • adf_espoiradf_espoir Posts: 4Questions: 1Answers: 0

    Thank you Kevin, yes, you are right, it's my mistake. Is it possible to filter with a part of the text, like col|nna. but not the complete value

  • kthorngrenkthorngren Posts: 16,400Questions: 25Answers: 3,891
    Answer ✓

    In the search plugin you can use any supported Javascript method like regex search().

    Kevin

  • adf_espoiradf_espoir Posts: 4Questions: 1Answers: 0

    Thanks Kevin, all is correct.

Sign In or Register to comment.