jquery dataTables搜索扩展,访问列信息

时间:2017-05-15 07:41:25

标签: jquery datatables

从此处https://datatables.net/manual/plug-ins/search 我想写一个搜索插件但是

  1. 我希望它能够根据我在columns:[]中定义的键进行操作,在我的示例中customSearch
  2. 当前提案显示列需要进行硬编码,例如searchData[3] ) || 0; // using the data from the 4th column
  3. 下面的示例

    目的: 1.在编写插件时,我如何定位列以进行搜索是否在columns:[]定义中定义了某个键

    示例:

    //datatables column definition
    columns:[{
        data:"id",
        title:"ID",
        customSearch:"custom"
    }]
    // writing a search plugin
    $.fn.dataTable.ext.search.push(
        function( settings, searchData, index, rowData, counter ) {
            var age = parseFloat( searchData[3] ) || 0; // using the data from the 4th column
            // this is hard coded. I dont want that. 
            // 
            // 
            // how can I get  column information here so I can write a plugin based on
            var age = parseFloat(rowData[column.data]) ; // dynamically get data
    
        }
    );
    ```
    

1 个答案:

答案 0 :(得分:2)

您在参数中获得settings对象,因此您可以过滤掉具有特定属性的列:

$.fn.dataTable.ext.search.push(
  function( settings, searchData, index, rowData, counter ) {

    var searchColumns = settings.aoColumns.filter(function(c) {
      return c.customSearch && c.customSearch != undefined
    });
    console.log(searchColumns);

现在您有一个具有customSearch属性的列数组。您可以按idx

获取列的索引
var index = searchColumns[0].idx;

或数据名称属性by(surprise)data:

var dataProp = searchColumns[0].data;

所以你可以做例如

var age = parseFloat(rowData[column[dataProp]])