Search code examples
yadcf

Dynamic column filters


Instead of referencing columns by number, I would like to define filters more dynamically. In particular, I'd like a way to say "all columns with th class xx get filter type yyy". Is that possible?


Solution

  • Since 0.9.4.beta.10 you can also use the columnDefs and provide yadcf with a column_selector instead of column_number, see it in action

    var columnDefs = [{
        "targets": "API",
        "data": "API"
      },
      {
        "targets": "Description",
        "data": "Description"
      },
      {
        "targets": "Category",
        "data": "Category"
      },
    ];
    
    
    
    var exampleTable1 = $("#example1").DataTable({
      "ajax": {
        "url": "https://api.publicapis.org/entries?category=Animals",
        "cache": true, // Or else `&_=23628934` gets added to the query string.
        "dataSrc": "entries"
      },
      "columnDefs": columnDefs
    });
    
    var yadcfConfiguration = [{
          // https://datatables.net/reference/type/column-selector
          column_selector: '.API'
        },
        {
          // https://datatables.net/reference/type/column-selector
          column_selector: '.Description',
          filter_type: "text"
        },
        {
          // https://datatables.net/reference/type/column-selector
          column_selector: '.Category',
          filter_type: "text"
        }
      ];
    
     yadcf.init(exampleTable1, yadcfConfiguration);