刷新网格上的数据SetOptions()

时间:2017-10-18 09:25:47

标签: asp.net-mvc kendo-ui telerik kendo-grid

你好我有一个小问题。我保存了我的kendogrid状态,并通过ajax将其传递给参数,我将其存储在我的数据库中。

var grid = $("#grid").data("kendoGrid");
var storage = kendo.stringify(grid.getOptions());
$.ajax({
        type: 'POST',
        url: WebRootPath + '/Service/SaveGrid',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ gridData: storage }),
        dataType: 'json',
        success: function (result) {
            $('#grid').data('kendoGrid').dataSource.read();
            $('#grid').data('kendoGrid').refresh();
            //debugger;
            location.reload();
        }
    });

我将它保存为字符串,当我加载网格时,我得到我保存的设置。但问题是,如果我更新或删除数据库中的列,它会在我的网格上刷新,因为数据源保存在选项中。我不知道如何保存我的选项,但更新数据。我使用dynammicaly网格。

网格代码:

  var data; 
            $.ajax({


                url: WebRootPath + '/Service/Grid',
                type: 'POST',
                async: false,
                dataType: 'json',
                data: {

                },
                success: function (result) {
                    generateGrid(result);
                    data = result;
                },
                error: function (ex) {
                    alert('Error' + ex);
                }
            });


            function generateGrid(gridData) {
                var model = generateModel(gridData[0]);
                grid = $("#grid").kendoGrid({
                    dataSource: {
                        data: gridData,
                        schema:
                            {
                                type: "json",
                                model: model,
                                // parse: parseFunction
                            }

                    },
                    //    toolbar: ["create", "save"],
                    sortable: true,
                    resizable: true,
                    reorderable: true,
selectable: "row",
                change: function (e) {
                    debugger;
                    var selectedRows = this.select();
                    var dataItem = this.dataItem(selectedRows);
                    Edit(dataItem.ID);
                }
                });
            }

            function generateModel(gridData) {
                var model = {};
                model.id = "ID";
                var fields = {};
                for (var property in gridData) {
                    var propType = typeof gridData[property];

                    if (propType == "number") {
                        fields[property] = {
                            type: "number",
                            validation: {
                                required: true
                            }
                        };
                    } else if (propType == "boolean") {
                        fields[property] = {
                            type: "boolean",
                            validation: {
                                required: true
                            }
                        };
                    } else if (propType == "string") {

                        {
                            fields[property] = {
                                validation: {
                                    required: true
                                }
                            };
                        }
                    } else {
                        fields[property] = {
                            validation: {
                                required: true
                            }
                        };
                    }

                }
                model.fields = fields;

                return model;
            }

            var grid = $("#grid").data("kendoGrid");
            $(function (e) {

                var options;
           //     var store = kendo.stringify(grid.getOptions());
                $.ajax({
                    type: 'POST',
                    async: false,
                    url: WebRootPath + '/Service/LoadGrid',
                    contentType: "application/json; charset=utf-8",
             //       data: JSON.stringify({ olddata: store }),
                    dataType: 'json',
                    success: function (result) {
                        grid.dataSource.read();
                        // refreshes the grid
                        grid.refresh();
                        options = result;
                    }
                });

                if (options && options != "") {

                    grid.setOptions(JSON.parse(options));


                } else {
                    grid.dataSource.read();
                }
            });

我不知道如何在我保存的选项中获取新数据。

0 个答案:

没有答案