简体   繁体   English

kendodropdownlist 过滤具有字段的 json 响应

[英]kendodropdownlist filter json response that has field

I am doing the below currently;我目前正在做以下工作; all my JSON entries have styleName , however some have the field txt , I want to display only the styleName data that has the txt field in my dropdown list.我所有的 JSON 条目都有styleName ,但是有些条目有字段txt ,我只想显示下拉列表中包含txt字段的styleName数据。

var dataSourceJSON = new kendo.data.DataSource({
    transport: {
        read: {
            url: dataURL,
            dataType: "json",
            type: 'GET'
        }
    }});dataSourceJSON.read();

    $("#dropdown").kendoDropDownList({
        dataSource    : dataSourceJSON,
        dataTextField : "styleName"
    });

Before using kendo, I was achieving this with the below, within a fetch on classic dropdowns.使用剑道之前,我是用下面实现这个,内fetch经典的下拉列表。 But I am unsure how to incorporate such logic with kendoDropDownList但我不确定如何将这种逻辑与kendoDropDownList

if (data[i].txt) {
     option.text = data[i].styleName;
     dropdown.add(option);
}..../

Update , this is where I am currently at;更新,这是我目前所在的位置; I can console.log the correct data in my schema parse but am still having a hard time getting the filtered data into the dropdown.我可以在我的模式解析中console.log正确的数据,但仍然很难将过滤后的数据放入下拉列表中。

  var ctemp;
  var dataSourceJSON = new kendo.data.DataSource({
    transport: {
        read: {
            url: dataURL,
            dataType: "json",
            type: 'GET'
        }
    },
    schema: {
        parse: function(datC) {
            console.log(datC);
            for (let i = 0; i < datC.length; i++) {
                if (datC[i].txt) {
                    ctemp = datC; // <-- This works and what I need
                    console.log(ctemp);
                }
            }
        }
    }
    });dataSourceJSON.read();

    $("#dropdown").kendoDropDownList({
        optionLabel: "Choose",
        dataSource    : dataSourceC,
        dataTextField : ctemp // <--- does nothing
    });

You can use kendo dataSource filter for this case.在这种情况下,您可以使用 kendo dataSource过滤器。 DataSource filter 数据源过滤器

Just check if a field is notnull and that's it.只需检查一个字段是否为非空就可以了。

var dataSource = new kendo.data.DataSource({
  data: [
    { styleName: "Style 1", txt: 'test' },
    { styleName: "Style 2" },
    { styleName: "Style 3", txt: 'test' },
  ],
  filter: { field: "txt", operator: "isnotnull" }
});

I made an example: Filter by field我做了一个例子:按字段过滤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM