首页 文章

如何编辑Kendo MVC Grid的本地数据

提问于
浏览
3

根据Telerik网站上有关本地数据绑定的文档:

服务器 - 小部件在进行分页,排序和过滤时执行服务器端请求(HTTP GET) .

Ajax - 小部件在进行分页,排序,过滤,分组或 saving data 时会发出Ajax请求 .

这是否意味着目前无法使用Kendo的MVC Grid在本地编辑数据?

我的目标是能够编辑网格,然后将整个页面与模型中的其他部分一起提交回服务器,并将所有数据保存在一起,而不是进行ajax调用以保存网格中的数据 .

使用下面的代码我可以加载网格,但是编辑单元格不会持续存在,当我回到页面时,数据不会绑定模型 .

@(Html.Kendo().Grid<LaborTimeViewModel>(Model.LaborTimes)
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.Procedure).ClientTemplate("#=Procedure ? Procedure.ProcedureDescription : ''#").EditorTemplateName("ProcedureEditor");
            columns.Bound(p => p.PerformedBy).ClientTemplate("#=PerformedBy ? PerformedBy.UserDescription : ''#").EditorTemplateName("UserEditor");
         columns.Bound(p => p.LaborTime).ClientTemplate("#if (LaborTime) {# #:kendo.toString(LaborTime.Hours, '00')#:#:kendo.toString(LaborTime.Minutes, '00')#:#:kendo.toString( '00')# #}#").EditorTemplateName("TimePickerEditor"); //.EditorTemplateName("NumericEditor");

        })
        .Editable(editable => editable.Mode(GridEditMode.InCell))
        .Pageable()
        .Sortable()
        .Scrollable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .ServerOperation(false) 
            .Model(model =>
                {
                    model.Id(p => p.WONumber);
                    model.Id(p => p.PerformedBy);
                    model.Id(p => p.TimerStart);
                })
        )
    )

1 回答

  • 1

    我找到了一种解决网格问题的方法,试图保存到服务器 .
    首先,为网格构建器上绑定的数据添加和事件 .

    .Events(events => events.DataBound("onGridDataBound"))
    

    然后,为onGridDataBound添加此javascript函数

    function onGridDataBound(e){
      //Prevent the grid from talking to the server
      var transport = e.sender.dataSource.transport;
      transport.create = function(e){e.success();}
      transport.destroy = function(e){e.success();}
      transport.update = function(e){e.success();}
    }
    

    通过覆盖数据源上的传输函数,网格将不会尝试进行Ajax调用,并且数据会保存到网格的本地模型中 .

    确保在网格脚本运行之前声明该函数,方法是使用“DeferredScripts”或在页面中的网格之前声明JS函数 .

相关问题