【问题标题】:Kendo Grid Custom Flter Set Filter Value Programmatically剑道网格自定义过滤器以编程方式设置过滤器值
【发布时间】:2018-05-18 02:16:07
【问题描述】:

我正在尝试以编程方式设置 Kendo Grid 自定义过滤器的过滤器值。我正在应用我的新过滤器值,例如:

gridOptions.dataSource.filter = [
    {
      field: 'MyField',
      operator: 'eq',
      value: newTextValue
    }
];

我在网格选项中的字段定义如下:

{
     width: '140px',
     title: 'MyFieldTitle',
     field: 'MyField',
     filterable: getFieldFilter()
}

使用以下过滤器:

function getFieldFilter() {
  return {
    cell: {
      template: function (args) {
        var element = args.element;

        element.kendoComboBox({
          dataSource: {
            transport: {
              read: 'api/Items'
            }
          },
          valuePrimitive: true,
          dataTextField: 'Description',
          dataValueField: 'Code'
        });
      },
      showOperators: false
    }
  };
}

如果我如上所示应用过滤器,它只有在我单击列中的 kendoComboBox 并再次单击它外部后才有效。 我最初的想法是剑道网格不会应用dataValueField,而只是设置dataTextField。当我检查 kendo 网格发送到服务器的请求时,我看到它正在发送到存储在 kendoComboBox 本身(文本)中的值,而不是它背后的值。

如果我从 UI 中选择 kendoComboBox 中的某些内容,则一切正常。但是如果我像上面那样以编程方式设置它,它不会。

我是否需要刷新某种状态以使 kendoComboBox 刷新其内部值或如何解决此问题?

编辑: 我要做的是从网格中获取kendoCombobox 的值。

var currentlyAppliedFilters = grid.dataSource.filter().filters;
for (var filter of currentlyAppliedFilters) {
    if (filter.field === 'MyField') {
      var currentlyApplied = filter.value;
    }
}

所以上面的代码会给我kendoCombobox 中项目的Description 属性,但我真正想要的是Code 属性。

【问题讨论】:

    标签: kendo-ui telerik kendo-grid kendo-combobox


    【解决方案1】:

    最初当数据源没有过滤器时,单元格过滤器需要一个选项标签。这样第一个值就不会显示为选中状态。

    例如:dojo example with optionLabel

     args.element.kendoDropDownList({
                                    dataSource: args.dataSource,
                                    optionLabel: "Select a color...",
                                    dataTextField: "color",
                                    dataValueField: "color",
                                    valuePrimitive: true
                                });
    

    如果在绑定时需要网格进行过滤,则需要在dataSource中添加一个默认过滤器

    例如:dojo example with DataSource Filter

     dataSource: { 
              data:[ { color: "#ff0000", size: 30 }, { color: "#000000", size: 33 }] ,
              filter: { field: "color", operator: "eq", value: "#ff0000" }
                        }
    

    希望这会有所帮助。

    【讨论】:

    • 就我而言,它似乎有所不同,要么是因为我使用了不同的 dataTextFielddataValueField,要么是因为我有一个带有文本输入的 kendoDropDownList
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多