从此处https://datatables.net/manual/plug-ins/search 我想写一个搜索插件但是
columns:[]
中定义的键进行操作,在我的示例中customSearch
searchData[3] ) || 0; // using the data from the 4th column
目的:
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
}
);
```
答案 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]])