Search specific columns

Search specific columns

NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I am using an external text box to search the datatable using:

$('#tbSearch').keyup(function() {
    myTable.search($(this).val()).draw();
});

But this searches entire table and I was wondering if I can restrict it to search two columns.
I am trying to search by site name or zip code, so just need to look at these two columns.

Answers

  • kthorngrenkthorngren Posts: 21,680Questions: 26Answers: 5,019
    edited 12:23AM

    There is columns().search() but it is basically an AND search between the two columns, meaning both columns must match the search term. Instead use search.fixed(). See this example.

    Kevin

  • NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

    Thank you Kevin, but this does not seem to do what I need, right?
    I want to search if what user is typing in either site name OR site zip code columns.

  • kthorngrenkthorngren Posts: 21,680Questions: 26Answers: 5,019
    edited 12:53AM

    It will work for what you want. Take a closer look at the example. The second parameter data contains the data object for the row. The example is comparing the inputs to the Age column by using var age = parseFloat(data[3]).

    For your case you will compare the input value to the two columns of interest using the data parameter.

    Here is a simple example that looks for Ashton in column 0 or 1:
    https://live.datatables.net/micuwali/1/edit

    Kevin

  • NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

    Thank you. Is this part of data table 1.10.22?
    I get "myTableName.search.fixed is not a function" error.
    Do you I need a specific library?

  • NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

    I have fixedHeader js and css in my project

  • NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

    Searching online suggested adding fixeHeader: true to datatable initialization; did that and now table is not populated at all!
    I thought I was there until I wasn't.

  • kthorngrenkthorngren Posts: 21,680Questions: 26Answers: 5,019

    It's a new feature in Datatables 2. It's not part of fixedheader. The V1 equivalent is a search plugin. See this V1 range search example. The search plugin docs have been removed due to phasing out that feature with search.fixed(). See the search docs for more details.

    Kevin

  • NoBullManNoBullMan Posts: 73Questions: 21Answers: 2

    This is what I have based on your suggestion.
    tbSearchSite is the textbox to enter search text.

        $('#tbSearchSites').keyup(function () {
                tblSites.search.fixed('myFunc', (row, data) => {
                    if (data[2].includes($(this).val()) || data[8].includes($(this).val())) {
                        return true;
                    }
                    return false;
                });
                tblSites.draw();
            });
    
  • kthorngrenkthorngren Posts: 21,680Questions: 26Answers: 5,019
    edited 1:59AM

    You don't want to initiate the search plugin or search.fixed() in the keyup event. Look at the examples. They are initialized only once. The event handler then calls draw() which will execute the plugin or search.fixed() function. The value of the input is obtained within the function.

    search.fixed() won't work with Datatables 1.x as it wasn't introduced until V2.

    data[2] and data[8] work only if you are using array based data. If you have columns.data defined then use the object key instead of the array index.

    Kevin

Sign In or Register to comment.