【发布时间】:2016-06-28 16:34:37
【问题描述】:
我在我的 Kendo ListView 中使用以下 JSON 数据:
[
{
"Id": 2,
"Name": "My Name",
"Address": "123 Sesame Street",
"City": "My City",
"State": "MO",
"ProductTypes": [
{
"Id": 2,
"Name": "Cage Free"
},
{
"Id": 3,
"Name": "Free-Trade"
},
{
"Id": 4,
"Name": "GAP"
},
{
"Id": 6,
"Name": "Grass Fed"
}
]
}
]
现在这是我的目标/问题。我想在选中复选框时过滤数据源,并且我想过滤的字段是 ProductTypes.Name 字段。
但是,我不确定如何使它正常工作。
这是我的DataSource:
profileDataSource: new kendo.data.DataSource({
transport: {
read: {
url: "/Profile/GetAllProfiles",
dataType: "json"
}
},
schema: {
model: {
fields: {
Id: { type: "number", editable: false, nullable: true },
Name: { type: "string" },
ProductTypes_Name: { type: "string", from: "ProductTypes.Name" }
}
}
}
})
这是我目前尝试过滤的方式,但它不起作用:
$("#profileCertificationsListView").on("click", "input[type=checkbox]", function() {
viewModel.profileDataSource.filter({
filters: [
{ field: "ProductTypes_Name", operator: "eq", value: $(this).attr("name") }
]
}
});
例如,如果我选中名称为“Cage Free”的复选框,则列表视图中的所有项目都将被隐藏。
----- 更新 -----
在@Suresh-c 的帮助下,我找到了解决问题的方法
这就是我现在的工作:
$("#profileCertificationsListView").on("click", "input[type=checkbox]", function() {
var name = $(this).attr("name");
var list = $("#profileDirectoryListView").data("kendoListView").dataSource.data();
var filtered = [];
for (var i = 0; i < list.length; i++) {
for (var j = 0; j < list[i].ProductTypes.length; j++) {
if (list[i].ProductTypes[j].Name === name) {
filtered.push(list[i]);
}
}
}
$("#profileDirectoryListView").data("kendoListView").dataSource.data(filtered);
});
【问题讨论】:
标签: javascript json kendo-ui