【问题标题】:Kendo DataSource requestEnd event gets fired multiple timesKendo DataSource requestEnd 事件被多次触发
【发布时间】:2017-09-19 21:12:32
【问题描述】:

我正在为 ASP.NET MVC 使用 Telerik UI,并且我的网格定义如下

@(Html.Kendo().Grid<GridModel>()
        .Name("grid")
        .Columns(col =>
        {
            col.Bound(o => o.ID)
                .ClientTemplate("<input class = 't-checkbox-selectrow' type='checkbox' value='#=ID#'/><label></label>")
                .HeaderTemplate("<input class = 't-checkbox-selectallrows' type='checkbox' id='selectAll'/><label></label>")
                .Sortable(false)
                .Filterable(false)
                .HtmlAttributes(new { @class = "t-gridcol-selectrow" })
                .Width(40)
                .Locked(true).Lockable(false);            
            col.Bound(o => o.StatusName).Width(150);
            col.Bound(o => o.Deadline).Width(120);
            col.Bound(o => o.Cost).Width(150);                        
        })        
        .AutoBind(false)
        .Pageable(x => x.PageSizes(UIConstants.PageSizes))
        .Sortable(x => x.AllowUnsort(false))
        .Resizable(resizing => resizing.Columns(true))
        .Reorderable(reorder => reorder.Columns(true))
        .Scrollable(s => s.Height("Auto"))
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(UIConstants.DefaultPageSizeMax)
            .Read(read => read
            .Action("GetData", "DataProvider"))
            .ServerOperation(false))
    )

然后在 JS 文件中,我将网格的页面重置为 1 onRequestEnd,因此每当我从远程服务获取数据时,用户总是返回第一页

 $(function(){
     var ds = $("#grid").data("kendoGrid").dataSource;
     ds.bind("requestEnd", function (e){      
       e.sender.page(1);
     })
    })

根据kendo's documentation

请求结束
当远程服务请求完成时触发。

不过,requestEnd 事件也会在页面更改(和排序)时触发。因此,当我以编程方式更改页面时,它会再次触发requestEnd 事件并进入循环。 (当我在 UI 上手动更改页面或排序时,注意 requestEnd 也会被触发)

这是设计使然还是文档错误?

【问题讨论】:

  • 我已经为这个问题苦苦挣扎了一段时间,但我还没有找到答案,因为剑道网格似乎没有仅在第一次加载时触发的事件,而且我看起来相当很多。在我遇到这个问题的大多数情况下,我会在重新获取数据后立即重置页面。按需点击任一按钮。

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


【解决方案1】:

我做了类似下面的事情。 e.response 仅在 requestEnds 作为服务器响应的一部分时才会填充。在所有其他情况下,它将为空。

dataSource.bind("requestEnd", function (e) {
        if (e.status != "error" && e.response != null) {                             
               // do soemething here                
        }

我真的希望剑道团队解决问题或创建新活动

【讨论】:

    【解决方案2】:

    这个问题是 3 年前提出的,但我在查找其他信息时遇到了这个问题。我在这里,我会为其他人回答,因为LP13可能不再关心这个了。

    在 HTML Razer 代码中,将 RequestEnd 事件添加到 DataSource。这是一个例子:

    @(Html.Kendo().Grid<GridModel>()
        .Name("grid")
        // (snip)
        .DataSource(dataSource => dataSource
            .Ajax()
            .Events(o => o.DataBound("onDataBound"))
            .PageSize(UIConstants.DefaultPageSizeMax)
            .Read(read => read
            .Action("GetData", "DataProvider"))
            .ServerOperation(false))
    )
    

    现在在您的 jquery 中,您将编写事件处理程序:

    function onRequestEnd(e) {
        console.log('onRequestEnd: e.response Object(The raw remote service response) = ', e.response);
        console.log('onRequestEnd: e.sender kendo.data.DataSource(The data source instance which fired the event) = ', e.sender);
        console.log('onRequestEnd: e.type String (The type of the request) = ', e.type);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多