Kendo UI Grid多检查按ID过滤

时间:2018-10-13 11:47:55

标签: jquery angular kendo-ui kendo-grid

假设要基于一组查找值在Kendo UI网格中过滤一列。

数据源是一个对象数组,例如

const dataSource = [{id:1,name:"IRL"},{id:2,name:"UK"}]

,我的网格的(简化)列布局为:

columns: [{
            field: "name",
            title: "Route",
            filterable: {
              multi: true,
              data: dataSource
            }
          }]

这将正确显示multicheck下拉过滤器,但是我发回我的api的过滤器field_name将是“ name”,过滤器值将是例如“ IRL”。

我需要的是过滤器field_name为“ id”,值为例如1,同时仍在下拉列表中显示名称,例如“ IRL”。

这可能吗?我遇到了一些有关重写过滤器功能的参考,但是我希望可以在数据源级别做到这一点。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

$(document).ready(function() {
  var firstNames = ["Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Laura", "Anne", "Nige"];
  var lastNames = ["Davolio", "Fuller", "Leverling", "Peacock", "Buchanan", "Suyama", "King", "Callahan", "Dodsworth", "White"];
  var cities = ["Seattle", "Tacoma", "Kirkland", "Redmond", "London", "Philadelphia", "New York", "Seattle", "London", "Boston"];
  var titles = ["Accountant", "Vice President, Sales", "Sales Representative", "Technical Support", "Sales Manager", "Web Designer",
    "Software Developer", "Inside Sales Coordinator", "Chief Techical Officer", "Chief Execute Officer"
  ];
  var birthDates = [new Date("1948/12/08"), new Date("1952/02/19"), new Date("1963/08/30"), new Date("1937/09/19"), new Date("1955/03/04"), new Date("1963/07/02"), new Date("1960/05/29"), new Date("1958/01/09"), new Date("1966/01/27"), new Date("1966/03/27")];

  function createRandomData(count) {
    var data = [],
      now = new Date();
    for (var i = 0; i < count; i++) {
      var BirthDater = birthDates[Math.floor(Math.random() * birthDates.length)];
      data.push({
        Id: i + 1,
        FirstName: firstNames[Math.floor(Math.random() * firstNames.length)],
        LastName: lastNames[Math.floor(Math.random() * lastNames.length)],
        City: cities[Math.floor(Math.random() * cities.length)],
        Title: titles[Math.floor(Math.random() * titles.length)],
        BirthDate: BirthDater,
        Age: now.getFullYear() - BirthDater.getFullYear()
      });
    }
    return data;
  }

  $("#grid").kendoGrid({
    dataSource: {
      data: createRandomData(50),
      schema: {
        model: {
          fields: {
            City: {
              type: "string"
            },
            Title: {
              type: "string"
            },
            BirthDate: {
              type: "date"
            }
          }
        }
      },
      pageSize: 15
    },
    height: 550,
    scrollable: true,
    filterable: {
      extra: false,
      operators: {
        string: {
          startswith: "Starts with",
          eq: "Is equal to",
          neq: "Is not equal to"
        }
      }
    },
    pageable: true,
    columns: [{
        title: "Name",
        width: 160,
        filterable: false,
        template: "#=FirstName# #=LastName#"
      },
      {
        field: "City",
        width: 130,
        filterable: {
          ui: cityFilter
        }
      },
      {
        field: "Title",
        filterable: {
          ui: titleFilter // Filter like this
        }
      },
      {
        field: "BirthDate",
        title: "Birth Date",
        format: "{0:MM/dd/yyyy HH:mm tt}",
        filterable: {
          ui: "datetimepicker"
        }
      }
    ]
  });
});

function titleFilter(element) {
  element.kendoDropDownList({
    dataSource: cities,
    optionLabel: "--Select Value--"
  });
}

function cityFilter(element) {
  element.kendoDropDownList({
    dataSource: cities,
    optionLabel: "--Select Value--"
  });
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2014.2.716/styles/kendo.default.min.css" />
  <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2014.2.716/styles/kendo.common.min.css" />
  <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
  <script src="http://kendo.cdn.telerik.com/2016.2.504/js/kendo.all.min.js"></script>

</head>

<body>

  <div id="example">
    <div id="grid"></div>
  </div>
</body>

</html>

在这里,ui属性是答案的重要部分,通过定义一个函数来处理过滤,您可以根据需要的值过滤给定的列。尝试使用此方法实现id过滤:)

答案 1 :(得分:0)

有点老的问题,但仍然有人会觉得这很有用 :)

上述解决方案有效,但不允许选择原始问题的多个选项。

Ofc 可以使用多选小部件,但如果选择了多个选项,它在菜单下拉列表中不适合/看起来不太好。

你只需要使用默认多重过滤器的“itemTemplate”选项,就像这样:

// this generates the filter UI
function gridCheckboxFilter(dataSource, idAsValue) {

    idAsValue = (idAsValue == true);

    var valueCallback = function (o) {
      return idAsValue ? o.id : o.label;
    };

    var labelCallback = function (o) {
      return o.label;
    };

    return {
      multi: true,
      search: dataSource.length > 10,
      dataSource: dataSource,
      itemTemplate: function (e) {
        if (e.field === 'all') {
          return '<li class="k-item"><label class="k-label">' +
            '<input class="k-check-all" value="Select All" type="checkbox">Select All</label></li>';
        } else {
          return function (o) {
            return '<li class="k-item"><label class="k-label"><input class="" ' +
              'value="' + valueCallback(o) + '" type="checkbox">' + labelCallback(o) + '</label></li>';
          };
        }
      }
    };

}

// and use like this:

var dataSource = [{id: 1, label: 'Lorem'}, {id: 2, label: 'Ipsum'}];

columns: [{
  field: "name",
  title: "Route",
  filterable: gridCheckboxFilter(dataSource, true)
}]