Is it possible to filter jQuery DataTable by data attribute?

I was wondering if it is even possible to filter jQuery DataTable with one of its data attributes, rather than the contents of its cells. To dynamically apply a filter to a column, this call is used:

$table.fnFilter('^(Some value)$', columnIndex, true, false);

This will filter the exact contents of the default cell using regex. However, suppose my cells are structured this way:

<td data-label="Active"><i class="fa fa-check fa-lg"></i></td>

or

<td data-label="Active">Active<br /><span class="mute">Some text</span></td>

I would like to have a DataTable filter with the exact contents of the attribute data-labelinstead of the contents of the cell. Is the question of determining the type of search when setting up columns in the init table? Or is there a way to define a filter by attribute instead of content?

+4
source share
2 answers

, :

$.fn.dataTable.ext.search.push(
   function(settings, data, dataIndex) {
      var dataLabel = table
          .row(dataIndex)         //get the row to evaluate
          .nodes()                //extract the HTML - node() does not support to$     
          .to$()                  //get as jQuery object 
          .find('td[data-label]') //find column with data-label
          .data('label');         //get the value of data-label
      return dataLabel  == 'Active'; 
   }     
);

demo → http://jsfiddle.net/x83zm7qq/

data-label , , data-label data-search data-filter:

<td data-search="Active"><i class="fa fa-check fa-lg"></i></td>

dataTables .

+6

, "" initComplete "" . :

$(function(){
  // the common/unified plugin (for all datatables)
  $.fn.DataTable.ext.search.push(
        function(settings, columnsOutput, dataIndex, data, outputIndex) {
          // this = ext.search array (all custom search functions (including this one)
          if (settings._myFilter){
              return settings._myFilter.call(settings, {
                  data: data,
                  dataIndex: dataIndex,
                  outputIndex: outputIndex,
                  columnsOutput: columnsOutput,
                  settings: settings
              });
          } else {
              return true;
          }
      }
      );

  // exact datatable initialization
  var dTable = $("#example").DataTable({
    // some sample data
    data: [{name: "John"}, {name: "Jack"}, {name: "Andy"}],
    columns: [{data: 'name'}],
    // setting our custom function under the initComplete callback
    initComplete: function(settings, json) {
      settings._myFilter = function(info){
        if ($('#jFilter').prop('checked')){
          return (info.data.name.toLowerCase().indexOf('j') >= 0);
        } else {
          return true;
        }
      }
    }
  });
  
  $('#jFilter').on('click', function(){
    dTable.draw(); // redraw will apply all the filters
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<link href="https://cdn.datatables.net/1.10.0/css/jquery.dataTables.css" rel="stylesheet"/>

<h2>Click checkbox below to filter by data using a callback</h2>
<label><input id="jFilter" type="checkbox"> J only</label>

<table id="example">
</table>
Hide result
0

Source: https://habr.com/ru/post/1598247/


All Articles