【发布时间】:2013-10-10 20:50:37
【问题描述】:
我正在尝试构建一个非常简单的 Kendo CRUD 网格,其中的表格只有两列:ID 和 Name。我已经使用服务器端分页和服务器端过滤配置了网格。
一切似乎都按预期运行,但在创建新记录后,网格显示新记录但没有 ID 字段。在创建时,请求的 ID 为 null,但我在创建后发送 id 的值和完整的对象。在示例网格中,网格会使用新值进行更新。我需要更改/添加什么以确保新创建记录的 ID 也显示在 Grid 中?
以下是 JSP:
<script>
$(function() {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url:"http://localhost:8181/baseweb/countrylist",
dataType: "jsonp"
},
update: {
url: "http://localhost:8181/baseweb/countryupdate",
dataType: "jsonp"
},
destroy: {
url: "http://localhost:8181/baseweb/countrydelete",
dataType: "jsonp"
},
create: {
url: "http://localhost:8181/baseweb/countrycreate",
dataType: "jsonp"
},
parameterMap: function(data, operation) {
if (operation !== "read" && data.models) {
return {grid_options: kendo.stringify(data.models)};
}
return {grid_options: kendo.stringify(data)};
}
},
serverPaging: true,
serverFiltering: true,
pageSize: 10,
schema: {
data: "data",
total: "total",
model: {
id: "id",
fields: {
id: { editable: false, nullable: true },
name: { validation: { required: true } }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
filterable: true,
height: 400,
toolbar: ["create"],
columns: [
{ field: "id", title:"Identification"},
{ field: "name", title:"Country Name" },
{ command: ["edit", "destroy"], title: " ", width: "160px" }
],
editable: "popup"
});
});
</script>
参数在创建时发送到服务器: _ 1380846899054 回调jQuery19108827040256333442_1380846899052 grid_options {"id":null,"name":"testing"}
从服务器返回的参数作为响应: jQuery19108827040256333442_1380846899052([ {"id":"4028828f4180d64a014180e3bda20002","name":"testing"}])
我希望服务器发回的 ID 应该显示在网格中。我已经在这个论坛、Kendo 文档和 Google 中搜索了答案,但我找不到解决方案。
我错过了什么?
更新解决方案:
Jack's answer 提供了寻找解决方案的线索。我犯了两个错误:
一个。 Kendo Grid 中的回调似乎期望将数据返回到“数据:”属性中。在我的回复中,我没有将结果集命名为“数据:”。 湾。回调还需要数据中的 JSONArray 对象:属性。因为我只创建一个对象,所以我发送了一个 JSONObject。
在我将响应更改为包含数据:属性和 JSONArray 后,它可以完美运行。 来自客户端的请求参数如下所示:
_ 1386350196768
callback jQuery19101285024500179227_1386350196765
grid_options {"id":null,"name":"Ghana"}
编辑后的响应如下所示:
jQuery19101285024500179227_1386350196765( {"data":[{"id":"2c9323ca42c8df630142c944450b0002","name":"Ghana"}]})
希望它对其他人有所帮助,因为官方文档中没有明确记录这一点。
【问题讨论】:
-
你有没有想过这个问题?我遇到了同样的问题,我返回的 ID 未在客户端数据集中设置,因此将其保留为“新”项目。搞砸之后的所有操作(创建、更新、删除)。
-
您的回答帮助我找到了解决方案。谢谢你。我将发布我自己的答案,以确保为其他人正确记录。
标签: kendo-ui kendo-grid