【问题标题】:Kendo Grid does not update the grid with newly created id after creationKendo Grid 在创建后不会使用新创建的 id 更新网格
【发布时间】: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: "&nbsp;", 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


【解决方案1】:

有一个很好的干净的方式来做到这一点......

如果您的网格是在脚本块中创建的:

dataSource: {
    transport: {
        read: {
            url: "..."
        },
        create: {
            url: "...",
            type: "POST",
            complete: function(e) {
                $("#grid").data("kendoGrid").dataSource.read(); 
        }
    },
}...

HTML 中的或

@(Html.Kendo().Grid<ViewModel>()
  .Name("grid")
  .DataSource(dataSource => dataSource
      .Ajax()
      .PageSize(10)
      .Model(model =>
        {
            model.Id(i => i.Cde);
            model.Field(i => i.Description).Editable(true);
        })
      .Read(read => read.Action("EditingInline_Read", "UserGroups"))
      .Update(update => update.Action("EditingInline_Update", "UserGroups")).Read("EditingInline_Read", "UserGroups")
      .Destroy(delete => delete.Action("EditingInline_Delete", "UserGroups"))
      .Create(create => create.Action("EditingInline_Create", "UserGroups")).Read("EditingInline_Read", "UserGroups")
   )
  .Columns(columns =>
  {
      columns.Bound(s => s.Decription);
      columns.Bound(s => s.enabled);
      columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
  })
  .Pageable()
  .Sortable()
  .Selectable(selectable => selectable
        .Mode(GridSelectionMode.Single))
  .ToolBar(toolbar => toolbar.Create()))

查看 CRUD 调用,更具体地说,更新和创建。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我想我可能已经找到了答案。如果在架构中定义了保存结果的对象,则必须在同一对象中返回创建链接的结果。示例:

    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 以支持其他一些框架。文档和示例非常浅薄,简单的事情需要很长时间才能弄清楚。
    • 我能感受到你的痛苦——它们在工作时很棒,但如果你有问题......谷歌启动你的引擎。
    • 我试过了,但结果还是一样。我将 id 添加到同一个对象并返回,但保存后仍未填充 id。
    • 杰克,谢谢你的帮助。请查看有关问题的更新。我发布了该问题的最终解决方案和结果。
    • 谢谢!对 KENDOS 数据源感到非常愤怒。当架构不匹配时,请给我一些该死的提示,让我生气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2013-11-12
    相关资源
    最近更新 更多