服务器端操作仅将数据返回到特定页面的网格

本文关键字:网格 操作 数据 返回 服务器端 | 更新日期: 2023-09-27 18:34:11

我正在使用telrik mvc网格,我可以让分页工作,但想知道我如何只能返回用户可以查看的数据。

例如,用户共有 50 条记录的页面大小设置为 5,当前页面为 1。在初始加载时,我只想返回前 5 行。然后,如果用户单击第 2 页,则接下来的 5 页依此类推。

我可以看到数据源请求具有页面和页面大小等属性。

我是通过控制器配置此客户端还是通过控制器配置?

我是否从请求页面和页面大小中获取值并将其传递给我的实体,然后执行 linq 查询,或者是否有更简单的解决方案?

有什么想法吗?谢谢

@model IEnumerable<TelerikChecklist.Models.ProductViewModel>
@(Html.Kendo().Grid(Model)
.Name("gridPaging")
.Columns(columns =>
{
    columns.Bound(p => p.ProductName);
    columns.Bound(p => p.UnitsInStock);
    columns.Bound(p => p.UnitPrice);
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:250px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .PageSize(5)
    .ServerOperation(true)
    .Events(events => events.Error("errorHandler"))
    .Model(model =>
    {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).Editable(false);
        model.Field(p => p.CategoryID).DefaultValue(1);
    })
    .Read(read => read.Action("ServerPaging_Read", "Home"))
)
)
<script type="text/javascript">

    function errorHandler(e) {
        if (e.errors) {
            var message = "Errors:'n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "'n";
                    });
                }
            });
            alert(message);
        }
    }


</script>

服务器端操作仅将数据返回到特定页面的网格

控制器需要以下控制器代码:

 public ActionResult CustomAjaxBinding_Read([DataSourceRequest] DataSourceRequest request)
    {
        var dataContext = new SampleEntities();
        // Convert to view model to avoid JSON serialization problems due to circular references.
        IQueryable<OrderViewModel> orders = dataContext.Orders.Select(o => new OrderViewModel
        {
            OrderID = o.OrderID,
            ShipCity = o.ShipCity,
            ShipCountry = o.ShipCountry,
            ShipName = o.ShipName
        });
        orders = orders.ApplyOrdersFiltering(request.Filters);
        var total = orders.Count();
        orders = orders.ApplyOrdersSorting(request.Groups, request.Sorts);
        if (!request.Sorts.Any())
        {
            // Entity Framework doesn't support paging on unsorted data.
            orders = orders.OrderBy(o => o.OrderID);
        }
        orders = orders.ApplyOrdersPaging(request.Page, request.PageSize);
        IEnumerable data = orders.ApplyOrdersGrouping(request.Groups);
        var result = new DataSourceResult()
        {
            Data = data,
            Total = total
        };
        return Json(result);
    }

观点:

@(Html.Kendo().Grid<TelerikChecklist.Models.Order>()
.Name("Grid")
.Columns(columns =>
{
    columns.Bound(o => o.OrderID).Groupable(false);
    columns.Bound(o => o.ShipCity);
    columns.Bound(o => o.ShipCountry);
    columns.Bound(o => o.ShipName);
})
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()
.DataSource(dataSource => dataSource
    .Ajax()
    .Read(read => read.Action("CustomAjaxBinding_Read", "Home"))
)
)