【问题标题】:YADCF Apply AND Filterring to Multiple Selections From a JSON ArrayYADCF 对 JSON 数组中的多个选择应用 AND 过滤
【发布时间】:2015-12-04 13:10:52
【问题描述】:

我想对 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

When the choices should look like:

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"                     
                    }]);
            });

注意 - I read this 并希望添加行中的数据选择始终保持相同的顺序。所以不存在一行的问题:

[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

【问题讨论】:

  • 你会在下面的示例codepen.io/Dravenrip/pen/OPJdKz中找到你的答案,你必须使用filter_type: 'multi_select_custom_func'学习那个示例,你应该没问题,如果没有,请告诉我
  • 嗨,Daniel,这是一个很棒的演示 - 我能够修改并让 AND 过滤正常工作。两个问题(1)在清除选择时不会重置该表,返回exGetColumnFilterVal error: no such filter_type: multi_select_custom_func和(2)说您有很多过滤器值 - 除了hand jammingvalue', label@bailel, 98765434@cases`case` cases`和found 项目?
  • 1) 是一个错误,您可以尝试自己修复它并发送拉取请求(也许在custom_func 上方添加另一个multi_select_custom_func 案例就足够了。2)请详细说明它,因为我不了解你
  • 修复时遇到问题 - 还有其他想法吗?我在 yadcf.js 的第 3424 行添加了:case multi_select_custom_func': retVal = $('#yadcf-filter-' + table_selector_jq_friendly + '-' + column_number).val(); console.log("retVal: "+retVal); if (retVal === null) { retVal = ''; } break;
  • 完成。 Here is a link。再次感谢。

标签: json sharepoint-2010 datatables yadcf


【解决方案1】:

谢谢,丹尼尔!

Working JS Fiddle answer.

$(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'}
        ]
   }]);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多