Search on input field and text

Search on input field and text

vpeigneuxvpeigneux Posts: 3Questions: 1Answers: 0

Hello,

I have a table with input text fields and text data.
When I search for a value the text fields content seems to be ignored.
I tried to write a custom filter with the following code :

$.fn.dataTable.ext.search.push(
            function (settings, searchData, index, rowData, counter) {
                var search = $('div.dataTables_filter input').val();

                if (search == "") {
                    return true;
                } else {
                var i;
                for (i in rowData) {
                    if (rowData[i].startsWith("<input")) {
                        if ($(rowData[i]).val().match(new RegExp(search, "i")) !== null) {
                            return true;
                        }
                    } else {
                        if (rowData[i].match(new RegExp(search, "i")) !== null) {
                            return true;
                        }
                    }
                }
                    return false;
                }
            }
        );

But the rowData array seems to not contain the rows with input fields when the search field contains more than one character.
Is it normal ?

Thanks

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Can you link to a test case showing the issue please?

    Thanks,
    Allan

  • vpeigneuxvpeigneux Posts: 3Questions: 1Answers: 0

    Hello Allan,

    You can find an example here :
    http://webapp.rossignol.com/assortment_demo/Pages/OF/Assortment.html

    In Assortment.html l.42 rowData array never contains the line with inputs when searching for a value that is in an input (example: search for RO) .

    Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Ah I see - thanks for the link. What you need to do is get the actual element to get the current DOM property:

    var td = table.cell( index, columnIndex ).node();
    var val = $('input', td);
    

    The reason for this is that using $(rowData[i]) is actually converting the string to be a new input element and getting its value. You need to get the existing element's value.

    Allan

  • vpeigneuxvpeigneux Posts: 3Questions: 1Answers: 0

    Thanks for your answer.
    The problem is when I type the second character (for example "o" in "ro"), the row containing the inputs is skipped by the function in $.fn.dataTable.ext.search.push : the first value of index is 1 instead of 0 (I tried in debug).

    Thanks

This discussion has been closed.