为JS中的kendo网格创建新行时提供唯一ID

时间:2014-01-16 06:25:42

标签: kendo-ui kendo-grid

我有一个kendo网格..它运行正常..但是当我在网格中创建一个新行时,我需要提供一个唯一的ID(整数很好,但不是重复的,例如.0,1,2,3 ..)保存。当我从后端获取数据时,我会计算行数并将其保留在会话中。当用户请求新行时,我读取会话并找到那里的行数并添加1以创建唯一ID。是否有任何优雅的方式为JS中的网格提供唯一ID而不使用会话。提前谢谢。

$("#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" }
        ]
});

1 个答案:

答案 0 :(得分:0)

在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);
  }
}