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

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

问题描述

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

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也显示在Grid中?

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_1380846899052 grid_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's answer provided the clue to finding the solution. I was making two mistakes:

a. Kendo Grid中的回调似乎期望数据返回到"data:"属性中.我没有在响应中将结果集命名为"data:". b.回调还需要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.

将响应更改为包括data:属性和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"}

编辑后的回复如下:

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天全站免登陆