简体   繁体   English

Kendo UI延迟加载

[英]Kendo UI Lazy Loading

I am using the following code. 我正在使用以下代码。 Can anybody tell me how will I use the page number instead of scroll bar? 有人可以告诉我如何使用页码代替滚动条吗?

My Index.cshtml page will be like 我的Index.cshtml页面会像

<div id="CustomerProfile">
<div id="GridCusotmerProfile">
    @(Html.Kendo().Grid(Model)
                .Name("grdCustomerProfile")

                .Columns(coloumns =>
                    {
                        coloumns.Bound(p => p.CustomerID).Title("Customer ID");
                        coloumns.Bound(p => p.UserId).Title("User Id");
                        coloumns.Bound(p => p.ComapnyName).Title("Company Name");
                        coloumns.Bound(p => p.ContactPerson).Title("Contact Person");
                        coloumns.Bound(p => p.AccountNumber).Title("Account Number");
                    }
                )
                .Sortable()                    
                .Scrollable(scrollable => scrollable.Virtual(true))
                .HtmlAttributes(new { style = "height:430px;" })
                .DataSource(dataSource => dataSource
                                              .Ajax()
                                              .PageSize(10)
                                              .Read(read => read.Action("Virtualization_Read", "CustomerProfile"))

                                            )


                )
</div>

My Controller will be like the following 我的控制器将如下所示

public List<CustomerProfileModel> CustomerDataSource(int page, int pagesize, int skip, int take)
    {
        List<CustomerProfileModel> ModelData = new List<CustomerProfileModel>();

        take = skip + take + (page * 10);
        var CustomerData = (from cp in context.CustomerProfile select cp).OrderBy(x => x.ComapnyName).Take(take).Skip(skip).ToList();

        foreach (var items in CustomerData)
        {
            CustomerProfileModel Model = new CustomerProfileModel();
            Model.CustomerID = items.CustomerID;
            Model.AccountNumber = items.AccountNumber;
            Model.ComapnyName = items.ComapnyName;
            Model.ContactPerson = items.ContactPerson;
            Model.UserId = items.UserId;
            ModelData.Add(Model);
        }

        return ModelData;
    }
public ActionResult Virtualization_Read([DataSourceRequest] DataSourceRequest request, string page,string pagesize,string skip,string take)
    {
        return Json(CustomerDataSource(Convert.ToInt32(page),Convert.ToInt32(pagesize),Convert.ToInt32(skip),Convert.ToInt32(take)).ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
    }

public List<CustomerProfileModel> CustomerDataSource(int page, int pagesize, int skip, int take)
    {
        List<CustomerProfileModel> ModelData = new List<CustomerProfileModel>();

        take = skip + take + (page * 10);
        var CustomerData = (from cp in context.CustomerProfile select cp).OrderBy(x => x.ComapnyName).Take(take).Skip(skip).ToList();

        foreach (var items in CustomerData)
        {
            CustomerProfileModel Model = new CustomerProfileModel();
            Model.CustomerID = items.CustomerID;
            Model.AccountNumber = items.AccountNumber;
            Model.ComapnyName = items.ComapnyName;
            Model.ContactPerson = items.ContactPerson;
            Model.UserId = items.UserId;
            ModelData.Add(Model);
        }

        return ModelData;
    }

public ActionResult Virtualization_Read([DataSourceRequest] DataSourceRequest request, string page,string pagesize,string skip,string take)
    {
        return Json(CustomerDataSource(Convert.ToInt32(page),Convert.ToInt32(pagesize),Convert.ToInt32(skip),Convert.ToInt32(take)).ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
    }

Please let me know if I need something else to get data as lazy loading. 请让我知道是否需要其他东西来获取数据作为延迟加载。

Your on the right tracks, but it is actually a lot easier than you think. 您的步伐正确,但实际上比您想象的要容易得多。 Your trying to hand roll functionality that Kendo handles with the `ToDataSourceResult() extension method. 您尝试通过KenTo使用`ToDataSourceResult()扩展方法处理的功能。

The DataSourceRequest contains all the information needed for database operations, such as ordering, aggregates and paging. DataSourceRequest包含数据库操作所需的所有信息,例如排序,聚合和分页。 So you can simplfy your code down to pretty much the following (NOT TESTED) 因此,您可以将代码简化为以下几类(未测试)

public ActionResult Virtualization_Read([DataSourceRequest] DataSourceRequest request)
{
            var CustomerData = (from cp in context.CustomerProfile select cp); // don't call toList() this exectues the SQL and pulls data into memory, leave it as a Queryable object so we can pass it to kendo to add its expressions this will the be a Database operation

            DataSourceResult result = CustomerData.ToDataSourceResult(request, x => new CustomerProfileModel(){
                        CustomerID = x.CustomerID;
                        AccountNumber = x.AccountNumber;
                        ComapnyName = x.ComapnyName;
                        ContactPerson = x.ContactPerson;
                        UserId = x.UserId;
                });



            return Json(result);
}

For further reading take a look at this link: 要进一步阅读,请查看以下链接:

http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding http://docs.kendoui.c​​om/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding

From the Kendo Site: 从剑道站点:

How do I implement paging, sorting, filtering and grouping? 如何实现分页,排序,过滤和分组?

If your model supports the IQueryable interface or is DataTable the grid will do paging, sorting, filtering, grouping and aggregates automatically. 如果您的模型支持IQueryable接口或为DataTable,则网格将自动进行分页,排序,过滤,分组和聚合。 For server binding scenarios no additional steps are required - just pass the IQueryable to the Grid constructor. 对于服务器绑定方案,不需要其他步骤-只需将IQueryable传递给Grid构造函数即可。 Check the server binding help topic for additional info. 检查服务器绑定帮助主题以获取其他信息。

For ajax binding scenarios the ToDataSourceResult extension method must be used to perform the data processing. 对于Ajax绑定方案,必须使用ToDataSourceResult扩展方法来执行数据处理。 Check the ajax binding help topic for additional information. 检查ajax绑定帮助主题以获取更多信息。 If your model does not implement IQueryable custom binding should be implemented. 如果您的模型未实现,则应实现IQueryable自定义绑定。 This means that the developer is responsible for paging, sorting, filtering and grouping the data. 这意味着开发人员负责分页,排序,过滤和分组数据。 More info can be found in the custom binding help topic. 在自定义绑定帮助主题中可以找到更多信息。

Important: 重要:

All data operations will be performed at database server level if the underlying IQueryable provider supports translation of expression trees to SQL. 如果基础IQueryable提供程序支持将表达式树转换为SQL,则将在数据库服务器级别执行所有数据操作。 Kendo Grid for ASP.NET MVC has been tested with the following frameworks: Kendo Grid for ASP.NET MVC已通过以下框架进行了测试:

Entity Framework 实体框架

Linq to SQL Linq到SQL

Telerik OpenAccess Telerik OpenAccess

NHibernate NHibernate

    .Columns(columns =>
    {
        columns.Bound(p => p.ID).Title("ID").Width(100).Visible(false);
        columns.Bound(p => p.Apply).Title("Apply").Width(100);
        columns.Bound(p => p.TaxName).Title("Tax Name").Width(100);
        columns.Bound(p => p.TaxPercent).Title("Percent").Width(130);
        columns.Bound(p => p.OrderApplied).Title("Oreder Applied").Width(130);
        columns.Bound(p => p.Compund).Title("Compund").Width(130);


        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.InLine))
     .Pageable()
    .Sortable()
     .Scrollable(scr=>scr.Height(430)) 
    //.Scrollable(scrollable => scrollable.Virtual(true))
    .HtmlAttributes(new { style = "height:430px;" })
    .Filterable()
    .DataSource(dataSource => dataSource
    .Ajax()
     .PageSize(10)
    .Events(events => events.Error("error_handler"))
    .Model(model => model.Id(p => p.ID))
    .ServerOperation(false)
    .Create(update => update.Action("EditingInline_Create", "Taxes"))
    .Read(read => read.Action("EditingInline_Read", "Taxes"))
    .Update(update => update.Action("EditingInline_Update", "Taxes"))
    .Destroy(update => update.Action("EditingInline_Destroy", "Taxes"))
    )
 )  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM