【问题标题】:Kendo datasource repopulate after calling update调用 update 后重新填充 Kendo 数据源
【发布时间】:2013-12-14 07:04:27
【问题描述】:

我正在使用 Kendo 数据源绑定到网格。当用户对行进行更改并调用保存时,它会调用数据源更新函数,该函数内部调用 webapi 来更新服务器。服务器返回相同的记录格式但有可能被其他用户更新相同的数据项,因此服务器将发送更新的记录并正在调用

options.success(result) 在数据源的成功调用中。但是网格和数据源仍然没有获取更新的数据。不确定是否遗漏了什么。

this.remoteUsersDataSource = new kendo.data.DataSource({
        transport: {
            read: function (options) {
                var contentType = "application/json; charset=utf-8";
                $.ajax({
                    url: "/api/UserManagementApi",
                    dataType: "json",
                    type: "GET",
                    contentType: contentType,
                    data: { dataSourceRequest: JSON.stringify(options), searchPhrase: that.SearchPhrase },
                    cache: false,
                    success: function (result) {
                        options.success(result);
                    }
                });
            },
            update: function (options) {
                console.log("inside update");
                console.log(options.data);
                var url = that.Globals.updateUserLink;
                var contentType = "application/json; charset=utf-8";
                var dataType = "json";

                that.Query({                        
                    type: "POST",
                    traditional: true,
                    url: url,
                    cache: false,
                    contentType: contentType,
                    dataType: dataType,
                    data:JSON.stringify(options.data)
                }).done(function (result) {
                    console.log("success");
                    console.log(result);
                    options.success(result);
                    that.saveUserCallSuccess(result);
                });

                //that.saveUser(options);
            }
        },
        page: this.Page,
        pageSize: this.PageSize,
        serverPaging: true,
        serverSorting: true,
        serverFiltering: true,
        schema: {                
            data: function (response) {
                console.log("inside data");
                return response.Users;
            },
            model: {
                id: "UserGuid",                    
                pickStatusOption: function (e) {
                    that.MVVM = this;
                    that.pickStatusOption(e);
                },
                unLockUser: function (e) {
                    console.log(e);
                    e.preventDefault();
                    //e.data.IsUserPasswordLocked = false;
                    that.unlockUser(e);
                }
            },
            total: function (response) {
                return response.TotalCount;
            }
        },
        change: function(e) {
            console.log("change");
            console.log(e);
        },
        error: function (e) {
            console.log(e);
        }
    });

更新 * 发现问题出在

  schema: {                
        data: function (response) {
            console.log("inside data");
            return response.Users;
        },

在初始加载传输期间,“读取”返回“响应”,其中包含另一个名为“用户”的对象。而当“更新”发生时,它会返回实际数据本身,并且没有称为“用户”的对象。所以我把我的“读”改成了-

success: function (result) {
                    options.success(result.Users);
                }

并从“模式”中删除了“数据”部分。

【问题讨论】:

    标签: c# kendo-ui kendo-grid


    【解决方案1】:

    这应该有效。数据源将尝试使用schema.model.id 选项将现有数据项与服务器返回的数据项进行匹配。我尝试了以下似乎可行的方法:

    $("#grid").kendoGrid({
      dataSource: {
        schema: { model: { id: "id" } },
        transport: {
          read: function(options) {
            setTimeout(function() {
              var data = { id: 1, foo: "foo" };
    
              options.success([ data ]);
    
            }, 100);
          },
          update: function(options) {
             setTimeout(function() {
              // simulate changes by other users
              var data = { id: 1, foo: "bar" };
    
              options.success([ data ]);
    
            }, 100);
          }
        }
      }
    });
    
    $("button").click(function() {
      var grid = $("#grid").data("kendoGrid");
      var item = grid.dataSource.get(1);
      item.dirty = true;
      grid.dataSource.sync();
    });
    

    这是一个现场演示:http://jsbin.com/uNaLilEs/1/edit

    【讨论】:

    • 我的 webapi 返回带有 ID 的模型,并且与数据源数据项匹配。但是,我认为问题出在不同的领域。我已经用我找到的解决方案更新了我的问题。谢谢
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多