如何在一行或 kendo 网格中添加数据

how to add data in a row or kendo Grid

我有一个场景,我想通过程序填充一些 kendo 网格列。所以我假设我必须捕获行并在列中填充数据。

我能够根据某些事件(例如单击)获取行 ID。但是我不知道如何实用地更新基于行 ID 的列的值。

http://jsfiddle.net/xojke83s/4/

上面是 JS fiddle,我可以在其中获取特定行的行 ID。我想知道通过程序在任何列中填充一些数据的方式。在上面的示例中,该列应该是 operationContext.

以下是相同的代码 -

<div id="grid"></div>

$("#grid").kendoGrid({
                    "dataSource": {
                      "schema": {
                        "model": {
                          "id": "id",   
                          "fields": {       
                            "OperationContext": {
                              "type": "string",                              
                              "editable": "false"
                            }
                          }
                        }
                      }                     
                    },
                    "editable": "popup",
                    "toolbar": [
                      {
                        "name": "create",
                        "text": "Add a New Record"
                      }
                    ],
                    "columns": [
                      {

                        "field": "Name",
                        "title": "Name"
                      },
                      {
                        "field": "Age",
                        "title": "Age"

                      },
                      {
                        "field": "OperationContext",
                        "title": "Operation Context"
                      },
                      { command: ["edit", "destroy"], title: "&nbsp;", width: "250px" }
                         ]
                        });

          $(".k-grid-add").on("click", function () {

              var grid = $("#grid").data("kendoGrid").dataSource.data([{OperationContext: "IsAdded"}]);

           });

//bind click event to the checkbox
var grid = $("#grid").data("kendoGrid"); 
grid.bind("edit", grid_edit); 

function grid_edit(e){

    console.log(e.model.uid);
}

提前致谢。

根据要求回复评论

我已将您的 fiddle 更新为:updated js fiddle

我修改了编辑代码来做到这一点:

function grid_edit(e){

    console.log(e.model);

    if(!e.model.isNew() || e.model.id === 0){
        e.model.set("OperationContext","I am being updated");
    }

}

它只会为新项目或 id 大于 0 的地方添加插入 (defaultValue) 和更新文本。

如果值为空或它被用作状态跟踪器,我可以看到新创建项目的逻辑,也可能看到编辑项目的逻辑。

但是,如果您删除了一个项目,那么它肯定会从您的 datasource 中删除,并且您将无法再访问该项目,所以为什么要存储一个 update/indicate 对值的更新呢?待删除。