Kendo UI 网格 - 更新不持久

发布于 2025-01-05 01:50:11 字数 3213 浏览 6 评论 0原文

一直在尝试对剑道网格进行更新,但遇到了问题。

我使用 Rails 作为后端,当我进行更新时,服务器似乎显示一切正常:

      Started PUT "/" for 127.0.0.1 at 2012-02-12 17:28:19 -0600
          Processing by HomeController#index as
          Parameters: {"models"=>"[{\"created_at\":\"2012-02-08T17:34:50Z\",
         \"first_name\":\"Milla\",\"id\":2,\"last_name\":\"sfasfsdf\",\"password\":\"\",
        \"updated_at\":\"2012-02-08T17:34:50Z\",\"user_name\"
        :\"\"}]"}
        Rendered home/index.html.erb within layouts/application (3.0ms)
        Completed 200 OK in 89ms (Views: 88.0ms | ActiveRecord: 0.0ms)

但是,当我刷新视图时,没有任何变化。当我检查数据库时,当然也没有发生任何变化。

我浏览了这里有关如何在网格中进行编辑的文档:http://demos。 kendoui.c​​om/web/grid/editing.html

我观看了 Burke Hollands 的视频,了解如何设置网格以与 Rails 配合使用:http://www.youtube.com/watch?v=FhHMOjN0Bjc&context=C3f358ceADOEgsToPDskKlwC22A9IkOjYnQhYyY9HI

一定有什么地方我没做对,但我只是没有看到。

这是我与 Kendo 相关的代码:

           var User = kendo.data.Model.define({
                id: "id",
                fields: {
                    first_name: { validation: { required: true } },
                    last_name: { validation: { required: true } }
                }
            });

            var UsersData = new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: "/users.json"
                        }, 

                        create: {
                            url: "/users/create.json",
                            type: "POST"
                        }, 

                        update: {

                            type: "PUT"
                        },

                        destroy: {
                            type: "DELETE"
                        },

                        parameterMap: function(options, operation) {
                            if (operation !== "read" && options.models) {
                                return {models: kendo.stringify(options.models)};
                            }
                        }


                    },

                    batch: true,

                    pageSize: 5,

                    schema: {
                        model: User
                    }


                });             

                $("#users-grid").kendoGrid({

                    dataSource: UsersData,
                    navigatable: true,
                    editable: true,                    
                    selectable: true,
                    pageable: true,
                    sortable: true, 
                    toolbar: ["create", "save", "cancel"],

                    columns: [
                    {
                        field: "first_name",
                        title: "First Name"
                    },
                    {
                        field: "last_name",
                        title: "Last Name"
                    },



                    ]


                });  

Been trying to do an update on a Kendo grid and I'm having issues.

I'm using Rails as the back-end and when I do the update, the server seems to be showing that everything worked:

      Started PUT "/" for 127.0.0.1 at 2012-02-12 17:28:19 -0600
          Processing by HomeController#index as
          Parameters: {"models"=>"[{\"created_at\":\"2012-02-08T17:34:50Z\",
         \"first_name\":\"Milla\",\"id\":2,\"last_name\":\"sfasfsdf\",\"password\":\"\",
        \"updated_at\":\"2012-02-08T17:34:50Z\",\"user_name\"
        :\"\"}]"}
        Rendered home/index.html.erb within layouts/application (3.0ms)
        Completed 200 OK in 89ms (Views: 88.0ms | ActiveRecord: 0.0ms)

However, when I refresh the view, nothing has changed. When I checked the database, of course no changes had taken place there either.

I went through the documentation here about how to do edits in the grid: http://demos.kendoui.com/web/grid/editing.html

And I watched Burke Hollands video about how to set up the grid to work with Rails: http://www.youtube.com/watch?v=FhHMOjN0Bjc&context=C3f358ceADOEgsToPDskKlwC22A9IkOjYnQhYyY9HI

There must be something that I haven't done right, but I'm just not seeing it.

Here's my code that works with the Kendo stuff:

           var User = kendo.data.Model.define({
                id: "id",
                fields: {
                    first_name: { validation: { required: true } },
                    last_name: { validation: { required: true } }
                }
            });

            var UsersData = new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: "/users.json"
                        }, 

                        create: {
                            url: "/users/create.json",
                            type: "POST"
                        }, 

                        update: {

                            type: "PUT"
                        },

                        destroy: {
                            type: "DELETE"
                        },

                        parameterMap: function(options, operation) {
                            if (operation !== "read" && options.models) {
                                return {models: kendo.stringify(options.models)};
                            }
                        }


                    },

                    batch: true,

                    pageSize: 5,

                    schema: {
                        model: User
                    }


                });             

                $("#users-grid").kendoGrid({

                    dataSource: UsersData,
                    navigatable: true,
                    editable: true,                    
                    selectable: true,
                    pageable: true,
                    sortable: true, 
                    toolbar: ["create", "save", "cancel"],

                    columns: [
                    {
                        field: "first_name",
                        title: "First Name"
                    },
                    {
                        field: "last_name",
                        title: "Last Name"
                    },



                    ]


                });  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2025-01-12 01:50:11

经过更多研究,我已经让它像这样工作了...

我添加了一条路由来覆盖 Rails 默认为您提供的 7 条 RESTful 路由。在您的 Routes.rb 文件中,添加此行...

match 'users' => 'users#update', :via => :put

这基本上表示我们将通过转到控制器上的更新定义来处理所有放置。

现在,在控制器定义中,您希望以不同的方式处理更新,因为它不是 RESTful。您需要首先解析通过parameterMap发送的JSON,然后迭代使用对象属性更新的对象...

def update
  respond_to do |format|
    @users = JSON.parse(params[:models])
    @users.each do |u|
      user = User.find(u["id"])    
      unless user.nil?
          user.update_attributes u
      end
    end
    format.json { head :no_content }
  end
end

Some more research and I've got it working like this...

I added a route to override the 7 RESTful routes that Rails gives you by default. In your Routes.rb files, add this line...

match 'users' => 'users#update', :via => :put

Which basically says we are going to handle all puts by going to the update definition on the controller.

Now in the controller definition, you want to handle the update a bit differently since it's not RESTful. You need to first parse the JSON that you are sending via the parameterMap and then iterate through the objects updating with the object attributes...

def update
  respond_to do |format|
    @users = JSON.parse(params[:models])
    @users.each do |u|
      user = User.find(u["id"])    
      unless user.nil?
          user.update_attributes u
      end
    end
    format.json { head :no_content }
  end
end
十秒萌定你 2025-01-12 01:50:11

您还可以修改数据源,因为 url 键可以采用一个函数:

var UsersData = new kendo.data.DataSource({
    transport: {
        read:  {
          url: '/users.json',
          dataType: 'json'
        },
        update: {
          url: function (o) {
            return '/users/' + o.id + '.json'
          },  
          dataType: 'json',
          type: 'PUT'
        },  
        destroy: {
          url: function (o) {
            return '/users/' + o.id + '.json'
          },
          dataType: 'json',
          type: 'DELETE',
        },  
        create: {
          url: '/users.json',
          dataType: 'json',
          type: 'POST'
        },
        parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
                return {models: kendo.stringify(options.models)};
            }
        }
    },
    batch: true,
    pageSize: 5,
    schema: {
        model: User
    }
});

You can also modify your datasource because the url key can take a function:

var UsersData = new kendo.data.DataSource({
    transport: {
        read:  {
          url: '/users.json',
          dataType: 'json'
        },
        update: {
          url: function (o) {
            return '/users/' + o.id + '.json'
          },  
          dataType: 'json',
          type: 'PUT'
        },  
        destroy: {
          url: function (o) {
            return '/users/' + o.id + '.json'
          },
          dataType: 'json',
          type: 'DELETE',
        },  
        create: {
          url: '/users.json',
          dataType: 'json',
          type: 'POST'
        },
        parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
                return {models: kendo.stringify(options.models)};
            }
        }
    },
    batch: true,
    pageSize: 5,
    schema: {
        model: User
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文