Kendo Grid 在创建后不会使用新创建的 id 更新网格 [英] Kendo Grid does not update the grid with newly created id after creation

查看:16
本文介绍了Kendo Grid 在创建后不会使用新创建的 id 更新网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用只有两列的表构建一个非常简单的 Kendo CRUD 网格:ID 和名称.我已经用服务器端分页和服务器端过滤配置了网格.

I am trying to build a very simple Kendo CRUD grid with a table that has only two columns: ID and Name. I have configured the grid with serverside pagination and serverside filtering.

一切似乎都按预期运行,但在创建新记录后,网格显示新记录但没有 ID 字段.在创建时,请求的 ID 为 null,但我在创建后发送 id 的值和完整对象.在示例网格中,网格会更新为新值.我需要更改/添加什么以确保新创建的记录的 ID 也显示在网格中?

Everything seems to function as expected but after the new record is created, the grid shows the new record but without the ID field. On creation, the request has ID null but I am sending the value of id and the full object after creation. In example grids, the grid is updated with new values. What do I need to change / add to ensure that the ID of the newly created record shows up in Grid as well?

以下是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_1380846899052grid_options {"id":null,"name":"testing"}

Parameter Sent to the server on create: _ 1380846899054 callback jQuery19108827040256333442_1380846899052 grid_options {"id":null,"name":"testing"}

从服务器返回的参数作为响应:jQuery19108827040256333442_1380846899052([ {"id":"4028828f4180d64a014180e3bda20002","name":"testing"}])

Parameter sent back from server as response: jQuery19108827040256333442_1380846899052([ {"id":"4028828f4180d64a014180e3bda20002","name":"testing"}])

我期望服务器发回的 ID 应该显示在网格中.我已经在这个论坛、Kendo 文档和 Google 上搜索过答案,但我找不到解决方案.

I am expecting that the ID sent back by server should be displayed in the Grid. I have searched this forum, Kendo documentation and Google for an answer but I am unable to find a solution.

我错过了什么?

更新解决方案:

Jack 的回答 提供了寻找解决方案的线索.我犯了两个错误:

Jack's answer provided the clue to finding the solution. I was making two mistakes:

一个.Kendo Grid 中的回调似乎希望数据返回到data:"属性中.我没有在回复中将结果集命名为数据:".湾回调还需要 data: 属性中的对象的 JSONArray.我正在发送一个 JSONObject,因为我只创建了一个对象.

a. The callback in Kendo Grid seems to expect the data back in a "data:" attribute. I was not naming the result set as "data:" in my response. b. The callback also expects a JSONArray of objects in the data: attribute. I was sending a JSONObject since I was only creating one object.

在我将响应更改为包含数据:属性和 JSONArray 后,它完美运行.来自客户端的请求参数如下所示:

After I changed the response to include data: attribute and JSONArray, it works perfectly. The request parameter from the client looks like this:

_   1386350196768
callback    jQuery19101285024500179227_1386350196765
grid_options    {"id":null,"name":"Ghana"}

编辑后的回复如下所示:

The edited response looks like this:

jQuery19101285024500179227_1386350196765( {"data":[{"id":"2c9323ca42c8df630142c944450b0002","name":"Ghana"}]})

希望它可以帮助其他人,因为官方文档中没有明确记录.

Hope it helps someone else as this is not clearly documented in the official documentation.

推荐答案

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

I've had the same problem and think I may have found the answer. If in the schema you define the object that holds the results, you must return the result of the created link in that same object. Example:

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 Grid 在创建后不会使用新创建的 id 更新网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆