动态列过滤器

时间:2018-11-14 13:13:26

标签: yadcf

我不想按数字引用列,而是想更动态地定义过滤器。特别是,我想说一种方式:“所有第xx类的列都获得yyy过滤器类型”。有可能吗?

2 个答案:

答案 0 :(得分:0)

我能够解决这个问题。发布代码以防他人使用:

// Set filters
var THs = document.getElementsByTagName("th");
var filterArray = [];
for (i = 0; i < THs.length; i++) {
    var colNumber = i;
    if ((THs[i].classList.contains('searchText')) || (THs[i].classList.contains('searchSelect')) || (THs[i].classList.contains('searchDate'))) {
        if (THs[i].classList.contains('searchText')) {
            var colFilter = "text";
        }
        if (THs[i].classList.contains('searchSelect')) {
            var colFilter = "select";
        }
        if (THs[i].classList.contains('searchDate')) {
            var colFilter = "range_date";
        }
        filterArray.push({column_number: colNumber , filter_type: colFilter});
    }
}

yadcf.init(containerTable, filterArray);

答案 1 :(得分:0)

0.9.4.beta.10起,您还可以使用 columnDefs 并为yadcf提供column_selector而不是column_numbersee 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);