分页没有移动到kendogrid中的新数据

时间:2015-07-22 16:45:23

标签: javascript jquery asp.net-mvc-4 kendo-grid

请注意,此时我是ASP.NET MVC,JavaScript和Kendo的新手。 Kendo网格分页适用于返回的前500条记录,但不允许分页从服务器下载超过500的更多数据。这是我的控制器:

    public ActionResult ExecuteTestRule(string ruleId, string ruleSql, string uwi, List<PdsMatchRuleParam> parameters = null)
    {
        if (Request.Url != null)
        {
            var query = PaginationQuery.Parse(Request.QueryString);
            var upperLimit = query.FromUpper;
            var lowerLimit = query.FromLower;
            var dataSource = new MatchDataSource();
            List<DataAccess.DbParameter> dbParameters = null;
            var generatedSql = dataSource.GenerateQuerySql(ruleId, ruleSql, uwi, parameters, out dbParameters);
            var results = dataSource.ExecuteTestRule(ruleId, generatedSql, dbParameters, upperLimit, lowerLimit).Select(u => new { UWI = u });
            var response = new Dictionary<string, object>();
            response["result"] = results;
            response["rowCount"] = MatchDataSource.GetRowCount(generatedSql, dbParameters);

            return Json(response, JsonRequestBehavior.AllowGet);
        }
        return null;
    }

以下是控制器中“rowCount”可用的总行数:

MatchDataSource.GetRowCount(generatedSql, dbParameters)
91637

这是控制器中的Request.QueryString:

Request.QueryString
{}
    [System.Web.HttpValueCollection]: {}
    base {System.Collections.Specialized.NameObjectCollectionBase}: {}
    AllKeys: {string[0]}

按此按钮无效:

enter image description here

这是我的JavaScript代码:

function bindTestRuleResults() {
    PageState.Selected.Old.TestRuleResult = PageState.Selected.TestRuleResult;

    var dataSource = new kendo.data.DataSource({
        pageSize: 500,
        data: PageState.Selected.TestRuleResult,
        serverPaging: true
    });
    Grids.TestRuleResultsGrid.setDataSource(dataSource);
    PageState.Selected.TestRuleResult = null;
}

function initTestRuleResultsGrid() {
    $(IDS.testRuleResultsGrid).kendoGrid({
        autoBind: true,
        filterable: false,
        navigatable: true,
        pageable: {
            refresh: true,
            pageSizes: [10, 50, 100, 500],
            buttonCount: 5
        },
        scrollable: true,
        selectable: true,
        serverFiltering: false,
        serverPaging: true,
        serverSorting: true,
        sortable: false,
        columns: [
            { field: "UWI", title: "UWI", width: "100%", attributes: { tabindex: "1" } }
        ],

        change: function() {
            var selectedDataItem = this.dataItem(this.select());
            if (PageState.Selected.TestRuleResult !== selectedDataItem.TestRuleResult) {
                PageState.Selected.TestRuleResult = selectedDataItem.TestRuleResult;
                testRuleResultsSelectionChanged();
            }
        },
        editable: false
    });
    // Add vertical scroll bars
    $(IDS.testRuleResultsGrid + " .k-grid-content").css({
        "overflow-y": "scroll"
    });
    Grids.TestRuleResultsGrid = $(IDS.testRuleResultsGrid).data('kendoGrid');
}

function execTestRule(uwi) {
    $.ajax({
        type: 'POST',
        url: "ExecuteTestRule",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({
            ruleId: PageState.Selected.RuleId,
            ruleSql: PageState.SqlEditor.RuleSql.getValue(),
            uwi: "'" + uwi + "'",
            parameters: PageState.RuleParameters
        }),
        schema: {
            errors: function(response) {
                return response.error;
            },
            data: function(response) {
                return response.result;
            },
            total: function(response) {
                return response.rowCount;
            }
        },
        success: function(matchedUwiList) {
            PageState.TestRuleResult = matchedUwiList.result;

            var dataSource = new kendo.data.DataSource({
                pageSize: 500,
                data: matchedUwiList.result,
                serverPaging: true
            });
            Grids.TestRuleResultsGrid.setDataSource(dataSource);
            PageState.Selected.ChildUwi = null;
            updateButtonStates();
        },
        error: function(e) {
            var errorObject = JSON.parse(e.xhr.responseText);
            var errorMessage = errorObject.Message;

            //clear old error message 
            Grids.TestRuleResultsGrid.clearErrorMessage("error-message");

            // add new error message
            Grids.TestRuleResultsGrid.addErrorMessage("error-message", errorMessage);
        }
    });
}

数据源中显然有serverPaging = true。我错过了什么?我是否需要以某种方式在我的JavaScript代码中使pageSize动态化? TIA。

更新

感谢您的反馈,@ Brett。这就是我按照你的建议简化代码的方法。如何删除成功:ajax部分之外的函数?

function execTestRule(uwi) {
    $.ajax({
        type: 'POST',
        url: "ExecuteTestRule",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({
            ruleId: PageState.Selected.RuleId,
            ruleSql: PageState.SqlEditor.RuleSql.getValue(),
            uwi: "'" + uwi + "'",
            parameters: PageState.RuleParameters
        }),
        success: function(matchedUwiList) {
            PageState.TestRuleResult = matchedUwiList.result;

            var dataSource = new kendo.data.DataSource({
                schema: {
                    data: 'results',
                    total: 'rowCount'
                },
                pageSize: 500,
                serverPaging: true
            });
            Grids.TestRuleResultsGrid.setDataSource(dataSource);
            PageState.Selected.ChildUwi = null;
            updateButtonStates();
        }
    });
}

运行execTestRule函数时,这是我得到的错误:

enter image description here

2 个答案:

答案 0 :(得分:1)

你的代码让我感到困惑,但我确实看到了一个特殊的问题。您没有告诉Kendo UI DataSource 您的数据和行计数属性位于控制器返回的对象中。

在控制器中,指定数据位于response["results"]属性中,而行计数位于response["rowCount"]属性中。因此,您返回的对象如下所示:

{
  results: [...],
  rowCount: 91637
}

Kendo UI DataSource 对象的schema默认情况下,期望数据位于"data"属性和行数(也称为项目数)在数据中)位于"total"属性中。由于您的对象不符合约定,因此您需要告诉数据源您的属性名称不同。

var dataSource = new kendo.data.DataSource({
  schema: {
    data: 'results',
    total: 'rowCount'
  },
  pageSize: 500,
  serverPaging: true
});

所以,你可能会说你已经这样做了,但看看你定义它的地方。您是在$.ajax()电话上定义的。这是不正确的。 $.ajax()函数不关心schema。 Kendo UI DataSource可以。

Kendo UI DataSource API reference

jQuery $.ajax API reference

答案 1 :(得分:1)

正如@Brett解释的那样,通过从$.ajax()调用中取出模式来解决问题。此外,开始在dataSource中使用传输:

$.widget("myViewGrid", {
        // ...
        var dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "{0}/ViewDataById".format(options.controllerUri),
                    type: "get",
                    dataType: "json",
                    data: {
                        viewId: view.viewId,
                        Id: options.Id
                    }
                }
            },
            schema: {
                data: "mydata",
                total: "total",
                model:dataSourceModel
            },
            pageSize: view.pageSize,
            serverPaging: true,
            serverSorting: true,
            serverFiltering: true
        });
        // ...
 });