我一直在搜索表上的data-search属性时遇到问题。
这是我通过Datatable()添加属性的方式:
select
*,
case when TransactionAmount > 0 then 'Db' else 'Cr' end AS [Db/Cr]
from tablename
union all
select
TransactionDate,
TransactionRef,
-1.0 * TransactionAmount,
case when TransactionAmount > 0 then 'Cr' else 'Db' end
from tablename
order by TransactionDate, TransactionRef, TransactionAmount desc
我想使用下拉列表根据属性值进行搜索,我尝试通过这种方式进行操作:
createdRow: function( row, data, dataIndex ) {
//Check if merchant has psp and add it to the cell
if(data.parent_id) {
$( row ).find('td:eq(0)').attr('data-search', 'merchant');
} else {
$( row ).find('td:eq(0)').attr('data-search', 'psp');
}
}
但是它似乎没有用。
请在此处找到带有模拟数据https://jsfiddle.net/designtocode/mdfeL7tp/14/的小提琴
任何帮助或帮助将不胜感激。
答案 0 :(得分:2)
您可以利用搜索插件,直接检查是否根据下拉菜单中选择的值设置了行parent_id
。
这将是搜索插件:
$.fn.dataTable.ext.search.push(function (settings, searchData, index, rowData, counter) {
let shouldDisplay = true;
let searchValue = $('#dropdown1').val();
if (searchValue == 'merchant') {
shouldDisplay = rowData.parent_id ? true : false;
} else if (searchValue == 'psp') {
shouldDisplay = rowData.parent_id ? false : true;
}
return shouldDisplay;
});
如果下拉菜单发生变化,您的下拉菜单只需要在桌面上调用平局即可。
$('#dropdown1').on('change', function() {
table.draw();
});