Kendo ui 在JS中为剑道网格创建新行时提供唯一ID
我有剑道格网。。它很好用。。但是,当我在网格中创建新行时,我需要提供一个唯一的ID(整数可以,但不是重复的ID,例如0、1、2、3…)来保存。当我从后端获取数据时,我计算行数并将其保留在会话中。当用户请求新行时,我阅读会话并找到其中的行数,然后添加1以创建一个唯一的ID。是否有任何优雅的方法可以在JS中为网格提供一个唯一的ID而不使用会话。提前谢谢Kendo ui 在JS中为剑道网格创建新行时提供唯一ID,kendo-ui,kendo-grid,Kendo Ui,Kendo Grid,我有剑道格网。。它很好用。。但是,当我在网格中创建新行时,我需要提供一个唯一的ID(整数可以,但不是重复的ID,例如0、1、2、3…)来保存。当我从后端获取数据时,我计算行数并将其保留在会话中。当用户请求新行时,我阅读会话并找到其中的行数,然后添加1以创建一个唯一的ID。是否有任何优雅的方法可以在JS中为网格提供一个唯一的ID而不使用会话。提前谢谢 $("#TrainingDetailsGrid").kendoGrid({ dataSource: { transpor
$("#TrainingDetailsGrid").kendoGrid({
dataSource: {
transport: {
read: {
url: "GetTrainingDetailsData",
datatype: "json",
type: "POST",
contentType: "application/json"
},
create: {
url: "UpdateTrainingDetailsData",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json"
},
update: {
url: "UpdateTrainingDetailsData",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json"
},
parameterMap: function (data, operation) {
if (operation != "read") {
return kendo.stringify(data.models);
}
}
},
pageSize: 5,
serverPaging: false,
batch: true,
schema: {
model: {
id: "ID",
fields: {
ID: { editable: false },
Description: { editable: true, nullable: false, validation: { required: true } },
Instructor: { editable: true, nullable: false, validation: { required: { message: "Instructor name is required" } } },
Trainee: { editable: true },
TimeSpent: { editable: true, type: "number", validation: { required: { message: "Hours are required" }, min: 0, step: 0.25 } },
}
},
errors: "Errors"
},
error: function (e) {
alert(e.errors + "TrainingDetailsGrid");
}
},
groupable: false,
autoBind: false,
sortable: true,
toolbar: ["create"],
editable: "inline",
pageable: {
refresh: true,
pageSizes: true
},
dataBound: onDataBound,
columns:
[
{ field: "Description", width: 90, title: "Description" },
{ field: "Instructor", width: 90, title: "Instructor" },
{ field: "Trainee", width: 90, title: "Trainee" },
{ field: "TimeSpent", width: 90, title: "Hours" },
{ command: ["edit"], title: "Action", width: "175px" }
]
});
在parameterMap中,您可以计算网格中的行数,并在将值传递到服务器端之前将其放入模型中
parameterMap: function (data, operation)
{
if (operation != "read") {
if (operation == "create") {
if ($("#TrainingDetailsGrid").data("kendoGrid").dataSource.total() > 0) {
data.models[0].ID = $("#TrainingDetailsGrid").data("kendoGrid").dataSource.total() - 1;
}
else {
data.models[0].ID = 0;
}
}
return kendo.stringify(data.models);
}
}
如果我可以删除一个项目,然后添加另一个项目,会发生什么?数据源中的两个项不是具有相同的ID吗?是的,如果您考虑DB,它将具有相同的ID;根据问题,他们被用来计算行数。这意味着它与数据库无关,相反,它们对行进行计数并给出ID。此解决方案并非完美无瑕,如果您有4行(id 0、1、2、3),如果您删除第3行,然后创建新行,第3行和第4行将具有相同的id 3,则您是正确的。对于他们给出的场景,它是有效的;但我不得不承认,这不是100%正确的解决方案。