columns().search() not working on multiple columns?

columns().search() not working on multiple columns?

lastbytelastbyte Posts: 8Questions: 0Answers: 0
edited March 2014 in DataTables 1.10
OK. I'm 0 for 2 on getting any responses on posts so far, but here goes #3 :-).

Is there a bug in columns(...).search(), or am I just not using it right? When I try to search multiple columns, only the first col gets searched. The below:

[code]
$('#search').on('keyup', function () {
table.api().columns([0,1,2]).search($(this).val()).draw();
});
[/code]

will only search column 0. Same thing if I use the other selectors (eg, class name). Any suggestions?

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Third time is the charm ;-) (In all seriousness - I'm sorry I haven't been able to reply to your other two posts, its gotten to the point where it is simply impossible for me to reply to every forum post - it takes up three hours every day at the moment, and that's not even all of them :-( . Its getting very difficult to balance it with actually doing any development on DataTables!).

    I think the problem you might be seeing here is that it is an AND search that DataTables is doing on the columns - i.e. `$(this).val()` is being applied to all three columns. If it isn't in all three, then the record won't be shown.

    My guess is you are looking for an OR filter - is that correct? I'm sorry to say that DataTables doesn't have the ability to selectively do an OR filter over a set of columns, other than the global filter. Although it a darn fine idea for a future enhancement.

    Allan
  • lastbytelastbyte Posts: 8Questions: 0Answers: 0
    edited March 2014
    HAHAHAH Thanks for responding. Yah, keep doing what you're doing; datatables dev is more important :-). The new datatables is awesome!

    Thanks for the insight. That does make sense. I found a simple work around, but for the sake of completeness, I'll tell you what I was trying to do.

    Basically, the issue that I was up against is that I wanted table.api().search(val) to only search a subset of the columns. I know I can simply set the non-searchable columns to searchable: false, but this renders the rest of the columns "invisible" to the other search methods (added via $.fn.dataTable.ext.search.push()).

    To elaborate, I have 3 sets of fields: text, number, date. I want the text fields to be searchable via a text input, the date field to be searchable via a date field, and the number field to be searchable via a drop down. If all of the columns (text, number, date) are searchable: true, then I get unwanted results when the user types in a numeric value. And, if I set the numeric and date columns to searchable: false, then the middle parameter of the per-row search callback doesn't include the non-searchable values.

    The work-around is to set all of the non-text fields to searchable: false, keep a reference to the entire data set, and use the 3rd parameter of the search function to index into it:

    [code]
    var dateMinSecs = null,
    dateMaxSecs = null,
    selIds = null;

    $.fn.dataTable.ext.search.push(
    function (settings, searchRow, dataIndex) {
    if (dateMinSecs && dateMinSecs) {
    return data[dataIndex][3] <= dateMaxSecs && data[dataIndex][3] >= dateMinSecs;
    }
    return true;
    },
    function (settings, searchRow, dataIndex) {
    if (selIds) {
    return selIds[data[dataIndex][13]];
    }
    return true;
    }
    );

    $('#campaigns').on('change', function (evt) {
    var $this = $(this);
    selIds = ($this.prop('selectedIndex') < 0) ? null : {};

    $.map($this.val(), function (val, i) {
    selIds[parseInt(val, 10)] = true;
    });
    table.api().draw();
    });

    $('#search').on('keyup', function () {
    var data = table.api()
    .search($(this).val())
    .draw();
    });
    [/code]
  • kshippkshipp Posts: 6Questions: 0Answers: 0
    fyi - The first post isn't correct. The .api is not valid. Here's the correct example:
    table.columns([0]).search($(this).val()).draw();
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Looks perfectly fine to me. Its just that @lastbyte is using the jQuery object and the api() method DataTables adds to it, while you are using a DataTables API instance directly.

    Allan
This discussion has been closed.