我正在尝试使用只有两列的表构建一个非常简单的 Kendo CRUD 网格:ID 和名称。我已经使用服务器端分页和服务器端过滤配置了网格。
一切似乎都按预期运行,但在创建新记录后,网格显示新记录但没有 ID 字段。在创建时,请求的 ID 为 null,但我在创建后发送 id 的值和完整对象。在示例网格中,网格会更新为新值。我需要更改/添加什么以确保新创建的记录的 ID 也显示在网格中?
以下是 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 中的回调似乎希望数据返回到“data:”属性中。我没有在回复中将结果集命名为“数据:”。
湾回调还需要 data: 属性中的对象的 JSONArray。我正在发送一个 JSONObject,因为我只创建了一个对象。
在我将响应更改为包含数据:属性和 JSONArray 后,它运行良好。
来自客户端的请求参数如下所示:
_ 1386350196768
callback jQuery19101285024500179227_1386350196765
grid_options {"id":null,"name":"Ghana"}
编辑后的响应如下所示:
jQuery19101285024500179227_1386350196765( {"data":[{"id":"2c9323ca42c8df630142c944450b0002","name":"Ghana"}]})
希望它可以帮助其他人,因为官方文档中没有明确记录这一点。
最佳答案
我遇到了同样的问题,我想我可能已经找到了答案。如果在架构中定义了保存结果的对象,则必须返回在同一对象中创建的链接的结果。例子:
schema: {
data: "data",
total: "total", ....
}
示例MVC方法:
public JsonResult CreateNewRow(RowModel rowModel)
{
// rowModel.id will be defaulted to 0
// save row to server and get new id back
var newId = SaveRowToServer(rowModel);
// set new id to model
rowModel.id = newId;
return Json(new {data= new[] {rowModel}});
}
关于kendo-ui - Kendo Grid 在创建后不会使用新创建的 id 更新网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171339/