Is it possible to allow a column to be filterable without being searchable?

Is it possible to allow a column to be filterable without being searchable?

jspathjspath Posts: 6Questions: 1Answers: 0
edited November 2014 in Free community support

I have some hidden columns in my table that I would like to filter upon, but not be searchable by the user.

Is this possible? Or do I need to implement a custom search input that only searches particular columns?

This question has an accepted answers - jump to answer

Answers

  • jspathjspath Posts: 6Questions: 1Answers: 0

    The only way I can figure to do this is to have the hidden columns marked as searchable, but implement the dataTables search input myself, and restrict it to the user searchable columns.

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    In DataTables searchable and filterable are basically the same thing (actually, technically is always a filter that DataTables does, but the search terminology is used for simplicity and to not conflict with filter()). So I'm afraid I'm not sure what you are looking for. Is it that you want to be able to search the data using the API, but the end user shouldn't be able to using the search input?

    If so, at the moment you are correct, there isn't a way to distinguish between the two. You would need to implement a custom search plug-in.

    Allan

  • jspathjspath Posts: 6Questions: 1Answers: 0

    In the old dataTables (pre 1.10), I could mark a column with bSearchable false, while still using fnFilter on it. We'd use this to allow customers to filter on various id values, while not having the id's searchable by the user.

    I'll look into the search plugin idea, but as of right now, I think implementing our own search input that will call .columns().search() so that I can limit user searches to specific columns, instead of all searchable columns.

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    Answer ✓

    In the old dataTables (pre 1.10), I could mark a column with bSearchable false, while still using fnFilter on it

    Heh - that wasn't an intentional feature! However, this is exactly the behaviour that is implemented for columns.orderable whereby it removes the user's ability to order on a column, but the API can still order on that column.

    I think it would be correct behaviour to allow that for columns.searchable as well. I'll look into that for the 1.10.5 release.

    Thanks for flagging this up!

    Allan

  • jspathjspath Posts: 6Questions: 1Answers: 0
    edited November 2014

    As it turns out, the best solution was the custom search plugin:

    $.fn.dataTable.ext.search.push(
      function( settings, searchData, index, rowData, counter ) {
    
        var search_regex =
          new RegExp(
            $.fn.dataTable.util.escapeRegex(
              $.trim($('#attributes_list_filter input').val())
            ),
            'i'
          );
    
        var matches = false;
        $.each([1,4,6], function(i, colidx){
          if ( search_regex.test( rowData[colidx] ) ) {
            matches = true;
            return false;
          }
        });
    
        return matches;
      }
    );
    

    My idea of columns().search() didn't work, since all the columns had to match, not just 1.

  • jspathjspath Posts: 6Questions: 1Answers: 0

    One final question allan ... is it possible to limit a search plugin to a particular table? Or is the search plugin always going to be applied globally to all dataTables on the page?

    Thanks for your help!

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    You need to check settings.nTable (which is the table node, where settings is the first parameter passed into the function). You could usesettings.nTable.id` for example to get the id of the table.

    Unfortunately that is currently the only way with the global filtering plug-ins. I'm going to improve that in a future release.

    Allan

  • jspathjspath Posts: 6Questions: 1Answers: 0

    OK, if I check the table id and decide that I do not want to use my custom search plugin, is there an easy way to execute the default search at that point?

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    Just return true so that the custom filter isn't filtering any rows out.

    Allan

This discussion has been closed.