我想对JSON数组中的多个选择应用AND过滤。
'Classification' Data:
Row 0: [Cats, Dogs]
Row 1: [Cats]
Row 2: [Birds, Cats, Dogs]
Filter selection: Cats and Dogs
Result: Display Rows 0 and 2
但是(1)这会导致OR筛选,(2)筛选选择是每行的全部内容。所以选择器中的行看起来像:
Cats, Dogs
Cats
Birds, Cats, Dogs
选择应该是什么样子:
Birds
Cats
Dogs
这是代码:
var call = $.ajax({
url: "https://.../_vti_bin/listdata.svc/DocLib?$expand=Classification",
type: "GET",
dataType: "json",
headers: {Accept: "application/json;odata=verbose"}
});
call.done(function (data,textStatus, jqXHR){
myData = data.d.results;
var dtTable = $('#example').DataTable({
data: myData,
columns:[
{data: "Classification.results[, ].Value"}
],
stateSave: true
});
yadcf.init(dtTable, [
{column_number:0,
filter_type: "multi_select"
}]);
});
注意——我读过这篇文章,想补充一点,行中的数据选择总是按照相同的顺序。因此,不存在一行的问题:
[Dogs, Birds, Cats]
此外,当我尝试使用时
text_data_delimiter:","
我得到错误:
SCRIPT5007: Unable to get property 'Value' of undefined or null reference
jquery.dataTables.yadcf.js, line 412 character 4
谢谢,Daniel!
工作JS Fiddle答案。
$(document).ready(function() {
var dtTable= $('#example').DataTable({});
function myCustomFilterFunction(filterVal, columnVal) {
if (filterVal === null) {return true;}
if (filterVal){
var found;
var myElement;
var foundTout = 0;
var nbElemSelected = filterVal.length;
for (i=0; i<nbElemSelected; i++)
{
myElement = filterVal[i];
switch (myElement) {
case 'Starch':found = columnVal.search(/Starch/g);
break;
case 'Fruit':found = columnVal.search(/Fruit/g);
break;
default:found = 1;
break;
}
if (found !== -1) {foundTout++;}
}
if (foundTout == filterVal.length) {return true;}
else {return false;}
}
}
yadcf.init(dtTable, [
{column_number:1,
select_type: "select2",
filter_type: 'multi_select_custom_func',
custom_func: myCustomFilterFunction,
filter_reset_button_text:"Clear",
filter_default_label:"select",
data: [
{value: 'Starch', label: 'Starch'},
{value: 'Fruit', label: 'Fruit'}
]
}]);
});