This is a migrated thread and some comments may be shown as answers.

Limiting number of items in a grid

1 Answer 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
UPG plc
Top achievements
Rank 1
UPG plc asked on 14 Aug 2014, 09:01 AM
Hi,

I have a dataset with millions of entries in it which I am displaying in the grid using the following code:

@(Html.Kendo().Grid<LogEntryViewModel>()
    .Name("logsGrid")
    .Columns(columns =>
    {
        columns.Bound(c => c.Id).Width(70);
        columns.Bound(c => c.Date).Format("{0:dd/MM/yy H:mm:ss}").Width(140);
        columns.Bound(c => c.IpAddress).Width(120);
        columns.Bound(c => c.Message);
     })
     .Scrollable( )
         .Sortable()
         .Filterable()
         .Pageable(pager => pager
             .Input(true)
             .Numeric(true)
             .Info(true)
             .Refresh(true))
         .Resizable(resize => resize.Columns(true))
            .HtmlAttributes(new {style = "height: 600px"})
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(50)
                .Read(read => read.Action("LogEntries_Read", "Home", new { maxResults = Model.MaxResults,  }))
        )
)

I would like to limit the amount of items returned (maxResults), say 1000 entries. I need this to work with server side filtering:

public ActionResult LogEntries_Read([DataSourceRequest] DataSourceRequest request, int maxResults)
{
    try
    {
        using (var db = new SysLogContext())
        {
            IQueryable<LogEntry> logs = db.LogEntries.Take(maxResults);
 
            try
            {
                var result = logs.ToDataSourceResult(request, logEntry => new LogEntryViewModel()
                {
                    Id = logEntry.Id,
                    IpAddress = logEntry.IpAddress,
                    Date = logEntry.Date.DateTime,
                    Message = logEntry.Message,
                });
 
                return Json(result);
            }
        }
    }
}

The problem with my code is that the .Take(maxResults) is performed before the filtering and sorting. This means that my grid does not display the correct results as expected, since the items are truncated before the filtering.

Is there a way of specifying a maximum number of returned entries in the ToDataSourceResult function? I need the filtering/sorting to be applied (server side) and then the returned results limited to <maxResults>, after the filtering and sorting is applied. I absolutely do not want millions of entries to be sent and then post-filtered, the filtering has to be done server side and then a limited number of results returned from the ToDataSourceResult function.

Thanks,

Michael

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 18 Aug 2014, 07:34 AM
Hi Michael,

In your case you can use custom binding and apply all sorting, filtering and paging parameters to the initial database query that you are executing. For this purpose, you will need to extract those parameters from the request argument of the action method. Our documentation provides more information about how to do this, and about custom binding in general:

http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/custom-binding

Regards,
Dimo
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
UPG plc
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or