【问题标题】:Angular-Kendo grid - server side paginationAngular-Kendo 网格 - 服务器端分页
【发布时间】:2013-11-11 17:35:19
【问题描述】:

我需要在 Angular-Kendo 网格中实现服务器端分页。我无法从 Angular 方面清楚地了解如何做到这一点。

有人可以帮忙吗?

【问题讨论】:

  • 撞!我也有兴趣研究这个。
  • 我仍然无法通过剑道正确地做到这一点。所以暂时我已经实现了分页的自定义逻辑并关闭了剑道分页。
  • 很高兴听到您至少在自己方面取得了一些进展。如果您弄清楚直接的剑道做事方式,请发布您的解决方案。
  • 各位看我的回答。

标签: angularjs kendo-grid


【解决方案1】:

Kendo 网格本质上支持服务器端分页,至少它有一个方便的内置 API 来提供帮助,因此您只需将所有部分连接在一起。这是我想出的,我的网格的数据源:

$scope.myGrid.dataSource = new kendo.data.DataSource({
    transport:{
               read:{
                      url: '/api/mygridapi?orderId=113',
                      dataType: 'json'
               }
            },
    pageSize: 5,
    serverPaging: true,
    serverSorting: true,
    serverFiltering: true,
    serverGrouping: true,
    serverAggregates: true,
    schema:{
            total: function(response) {
                    return 13; // call some function, or some scope variable that know the total items count
                },
                model: {
                    id: "id",
                    fields: {
                        'id': { type: "number", editable: false },
                        'name': { type: "string", editable: true, nullable: false, validation: { required: true } },
                        'price': { type: "number", editable: true, nullable: false, validation: { required: true } },
                    }
                }
            }
        });

还有我的网格标记:

<div kendo-grid k-pageable='{ "pageSize": 5, "refresh": true, "pageSizes": false }'
     k-height="'250px'" k-column-menu="false" k-filterable="true" k-sortable="true" k-groupable="true"
     k-data-source="myGrid.dataSource" k-options="{{myGrid.gridOpts}}" k-on-change="onSelectHandler(kendoEvent)">

还有我的 web api 控制器:

[System.Web.Http.HttpGet]
public IEnumerable<ProductsDTO> Get(int orderId)
{
     NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
          //the name value captures the paging info that kendo automatically appends to the query string when it requests data
              //it has info such as teh current page, page size etc....
     int take = int.Parse(nvc["take"]);
     int skip = int.Parse(nvc["skip"]);

     return productsSvc.GetProductsOfOrder(orderId,skip,take);
}

我的服务返回一个 IQueryable,但它也可以返回一个具体的列表,因为返回一个 IQueryable 并没有帮助 Kendo 弄清楚总共有多少项。对我来说主要问题是网格没有正确计算总项目数,例如将显示第一页(前 5 个项目)但是没有注意到其余项目,因此网格分页按钮被禁用所以我有点解决这个问题,但手动设置项目总数,即这些代码行:

schema:{
        total: function(response) {
                return 13; // call some function, or some scope variable that know the total items count
        },.........

困扰我的一件事是必须手动设置总项目数。值得一提的是,在设置数据源时,您可以将一个函数传递给传输对象的读取属性,该函数将具有一个包含当前分页/过滤信息的对象作为参数,因此您可以使用它来构建查询字符串手动而不是依赖默认的剑道服务器请求:

transport: {
           read: function (options) {
                 console.log(options);//see whats inside
                 //we can use the pageNo and pageSize property to create a query string manually
           }
}

【讨论】:

    【解决方案2】:

    随着最新版本的 Kendo UI (in Beta right now) 发布,我们可以使用另一种方法来实现服务器端分页,使用 Angular 提供的带有 Kendo Grid 读取功能的 $http.post 方法。

    这是一个使用 MVC 5 控制器作为您从数据源获取数据的端点的示例。它通过将pagepageSize 发送到控制器来模拟服务器分页,如果需要,您还可以发送takeskip 并根据需要进行处理。

    HTML 标记

    <div ng-controller="MyCtrl">
        <kendo-grid k-options="mainGridOptions"></kendo-grid>
    </div>
    

    JavaScript

    function MyCtrl($scope, $http) {
        $scope.mainGridOptions = {
            dataSource: {
                schema: {
                    data: "Data",
                    total: "Total"
                },
                transport: {
                    read: function (e) {//You can get the current page, pageSize etc off `e`.
                        var requestData = {
                            page: e.data.page,
                            pageSize: e.data.pageSize,
                            type: "hello"
                        };
                        console.log(e);
                        $http({ method: 'POST', url: 'Home/DataSourceResult', data: requestData }).
                        success(function (data, status, headers, config) {
                            e.success(data);
                            //console.log(data.Data);
                        }).
                        error(function (data, status, headers, config) {
                            alert('something went wrong');
                            console.log(status);
                        });
                    }
                },
                pageSize: 1,
                serverPaging: true,
                serverSorting: true
            },
            selectable: "row",
            pageable: true,
            sortable: true,
            groupable: true
        }
    }
    

    您可以从 read: function(e){} 声明中的参数 e 获取当前 pageSize、page、take、skip 和更多内容。

    因为 post 值引用了 read 函数中的参数,所以每次在网格上更新页面时,它们都会更新。每次网格进行更改时,您都可以使用它来更新您的帖子值。然后网格会重新绑定自己。

    Home/DataSourceResult 控制器

    [HttpPost]
        public ActionResult DataSourceResult(int page, string type, int pageSize)
        {
            ResponseData resultData = new ResponseData();
            string tempData = "";
            if (page == 1)
            {
                tempData = "[{\"NAME\": \"Example Name 1\", \"DESCRIPTION\": \"Example Description 1\"},{\"NAME\": \"Example Name 2\",\"DESCRIPTION\": null}]";
            }
            else if (page == 2)
            {
                tempData = "[{\"NAME\": \"Example Name 3\", \"DESCRIPTION\": \"Example Description 3\"},{\"NAME\": \"Example Name 4\",\"DESCRIPTION\": \"Example Description 4\"}]";
            }
            resultData.Data = tempData;
            resultData.Total = "4";
            string json = JsonConvert.SerializeObject(resultData);
            json = json.Replace(@"\", "");
            json = json.Replace("\"[{", "[{");
            json = json.Replace("}]\"", "}]");
            return Content(json, "application/json");
        }
    

    非常基本,但正是我所需要的,也可能对您有所帮助。这使用了原生 Angular http.get 功能,同时仍然允许 Kendo Grid 完成大部分繁重的工作。

    【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-07-08
    • 2014-02-18
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多