我正在使用Kendo MVC网格控件,所有工作正常。我们在网格中加载记录(使用AJAX),并且已成功使用虚拟滚动和分页。我们注意到的事情是(包括在线的所有Kendo网格示例),每次要加载新数据(即滚动或页面更改)时,请求都会转到服务器,导致从中检索数据(所有记录)数据源。
到目前为止,我们已经在网格中显示了最多500条记录的限制。但是现在我们需要无限制的记录,最高可达10K,在这种情况下我们不想从数据库中获取所有记录。
我的实施:
视图看起来像这样(不包括所有内容):
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@(Html.Kendo().Grid<ProspectEquipmentGridModel>()
.Name("ProspectEquipmentGridModel")
.Columns(columns =>
{
columns.Bound(p => p.VinSn).Title("VIN #");
columns.Bound(p => p.VehicleBrand).Title("Vehicle Brand");
columns.Bound(p => p.Model).Title("Model");
columns.Bound(p => p.Year).Title("Year").Width(50);
columns.Bound(p => p.GvwrClass).Title("GVWR").Width(65);
columns.Bound(p => p.EqtType).Title("Type");
columns.Bound(p => p.EngineBrand).Title("Engine Brand");
columns.Bound(p => p.SeriesCode).Title("Series Code");
columns.Bound(p => p.EngineType).Title("Type");
columns.Bound(p => p.FleetOutOfService).Title("Fleet Out of service");
})
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.Sortable(sortable => sortable.AllowUnsort(false))
.Scrollable(scrollable => scrollable.Height("400px"))
//.Scrollable(scrollable => scrollable.Virtual(true))
.Pageable(pageable => pageable
.Refresh(false)
.PageSizes(false)
.ButtonCount(10)
)
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Model(model => model.Id(p => p.Rrid))
.Read(read => read.Action("GetProspectEquipmentGridData", "ProspectProfile", new {data = Model.AuthenticationToken, showFiltered = true}))
.Events(e => e.Change("UpdateCount"))
)
.Events(e => e.DataBound("ListViewDataBound"))
)
}
初始请求转到服务器(控制器方法)。
public ActionResult _ProspectProfileEquipmentTab(string data)
{
var totalVehicleCounts = _factRepository.TotalVehiclesOwned(Rrid, null);
var filteredVehicleCounts = _factRepository.TotalVehiclesOwned(Rrid, _searchCriteriaService.GetSearchCriteria(SearchId));
var model = new ProspectProfileEquipmentViewModel()
{
AuthenticationToken = data,
SearchId=SearchId,
Rrid = Rrid,
UserType = UserType,
TotalVehicles = totalVehicleCounts,
FilteredVehicles = filteredVehicleCounts
};
return View(model);
}
服务器返回初始数据,并且kendo网格加载自己调用DataSource.Read方法:
public ActionResult GetProspectEquipmentGridData([DataSourceRequest] DataSourceRequest request, string data, bool showFiltered)
{
IEnumerable<ProspectEquipmentGridModel> model = new List<ProspectEquipmentGridModel>();
int startIndex = request.PageSize * (request.Page - 1);
if (showFiltered && !UserType.Equals("QuickSearchUser"))
{
var searchCriteria = _searchCriteriaService.GetSearchCriteria(SearchId);
model = _factRepository.GetDataForProspectEquipmentGrid(searchCriteria, Rrid, startIndex, request.PageSize);
}
else
{
model = _factRepository.GetDataForProspectEquipmentGrid(null, Rrid, startIndex, request.PageSize);
}
// Commenting following line which works but I want only limited number of record to go from
// server to client
//return Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
// Try 1 - Sending only 20 records
return Json(model, JsonRequestBehavior.AllowGet);
// Try 2:
var gridData = model.ToDataSourceResult(request);
gridData.Total = _factRepository.TotalVehiclesOwned(Rrid, null);return Json(gridData, JsonRequestBehavior.AllowGet);
}
如上面的代码所示(请参阅注释),我们需要获取所有记录,然后使用ToDataSourceResult()来使网格正常工作。
我尝试将startIndex和count计数发送到数据库,一次只获取20条记录(PageSize)但如果我只发送20条记录,则网格无法正常显示。
我理解网格需要知道有多少记录可用于计算页数,但我相信我缺少一些可以让我优化代码的方法。
我的目标是
要么不为每个页面更改请求一次又一次地访问数据库
OR
仅提取记录= PageSize。
如果您知道实现目标,请分享。