2013-12-10 168 views
0
@(Html.Kendo().Grid<RTM.WEB.MVC.ViewModels.SortingViewModel.ProductViewModel>() 
.Name("ProductsGrid").HtmlAttributes("height:430") 
.Columns(columns => 
{ 
columns.Bound(p => p.IsSelected).Width(50).Title(""); 
columns.Bound(p => p.ProductName); 
columns.Bound(p => p.Price).Format("{0:C}"); 
columns.Bound(p => p.GroupName); 
// columns.Bound(p => p.MeasurementId); 
// columns.Bound(p => p.BarcodeValue); 
columns.Command(comand => 
{ 
comand.Custom("Edit").Click("ViewEdit"); 
comand.Destroy(); 

}).Title("Commands").Width(180); 

}) 
.DataSource(dataSource => dataSource 

.Ajax() 
.PageSize(10) 
//.Events(events=>events.Sync("addGroupData")) 
.ServerOperation(true) 
.Batch(true) 

.Events(events => 
{ 
events.Error("errorHandler"); 
events.Sync("addDataToResponce"); 
}) 
.Model(model => 
{ 
model.Id(p => p.ProductId); 
model.Field(p => p.ProductId).Editable(false); 
}) 

//          .Aggregates(aggregates => 
//{ 
// //aggregates.Add(p => p.ProductId); 
// //aggregates.Add(p => p.ProductName); 
// aggregates.Add(p => p.Price).Sum(); 
//}) 


.Group(groups => groups.Add(p => p.GroupName)) 

.Create(create => create.Action("Products_Create", "Sorting")) 
.Read(read => read.Action("Products_Read", "Sorting")) 
.Update(update => update.Action("Products_Update", "Sorting")) 
.Destroy(destroy => destroy.Action("Products_Destroy", "Sorting")) 
) 
.ToolBar(toolbar => 
{ 
toolbar.Template(@<text> 
<div > 
<a class="k-button" onclick="AddProduct()">Add New Product</a> 
@item.SaveButton() 
<a class="k-button" onclick="NewGroup()">NewGroup</a> 
<a class="k-button" onclick="UnGroup()">UnGroup</a> 
<label>Show products by warehouse:</label> 
@(Html.Kendo().DropDownList() 
.Name("warehouses") 
.OptionLabel("All") 
.DataTextField("Name") 
.DataValueField("WarehouseId") 
.AutoBind(false) 
// .Events(e => e.Change("warehousesChange")) 
.DataSource(ds => 
{ 
ds.Read("Products_Warehouses", "Sorting"); 

}) 
) 
</div> 
</text>); 

}) 
.Events(events => 
{ 
events.DataBound("dataBound"); 
}) 
.Pageable(page => page.PageSizes(true).Numeric(false).Refresh(true).Input(true)) 
//.Navigatable() 
.Selectable() 
.ColumnMenu() 
.Filterable() 
.Sortable() 
.Scrollable() 
.Editable(editable => editable.Mode(GridEditMode.InCell).DisplayDeleteConfirmation(false)) 
) 
public JsonResult Products_Read([DataSourceRequest]DataSourceRequest request) 
{ 
int allcount=0; 
List<ProductModel> products= DataManager.ProductRepository.GetAllProducts(request.Page, request.PageSize,ref allcount); 
List<ProductViewModel> productViewModels=new List<ProductViewModel>(); 
foreach (var product in products) 
{ 
productViewModels.Add(ConvertProductViewModel(product)); 
} 
if (request.Sorts.Count != 0) 
{ 
string member = request.Sorts.First().Member; 
string sorttype = request.Sorts.First().SortDirection.ToString(); 
switch (member) 
{ 
case "IsSelected": 
if (sorttype == "Ascending") 
{ 
productViewModels = productViewModels.OrderBy(p => p.IsSelected).ToList(); 
} 
if (sorttype == "Descending") 
{ 
productViewModels = productViewModels.OrderByDescending(p => p.IsSelected).ToList(); 
} 
break; 
case "ProductName": 
if (sorttype == "Ascending") 
{ 
productViewModels = productViewModels.OrderBy(p => p.ProductName).ToList(); 
} 
if (sorttype == "Descending") 
{ 
productViewModels = productViewModels.OrderByDescending(p => p.ProductName).ToList(); 
} 
break; 
case "Price": 
if (sorttype == "Ascending") 
{ 
productViewModels = productViewModels.OrderBy(p => p.Price).ToList(); 
} 
if (sorttype == "Descending") 
{ 
productViewModels = productViewModels.OrderByDescending(p => p.Price).ToList(); 
} 
break; 
} 

} 


var result = new DataSourceResult() 
{ 
Data = productViewModels, 
Total = allcount, 
AggregateResults = null, 
Errors = null, 
}; 
return Json(result, JsonRequestBehavior.AllowGet); 
} 

這是我的代碼。當我從動作服務器返回數據時分頁不起作用。 我想做服務器分頁kendo ui組網格,但是響應數據類型爲undefined.I需要組網格的工作示例。它在基本網格中工作,但不適用於組網格。Asp.Net MVC KendoUI網格

回答

2

您應該只需要更新控制器方法。由於您正在進行的排序似乎非常基本,您可以使用提供的Kendo方法ToDataSourceResult(DataSourceRequest request)

以下添加到您的使用一節中的控制器:

using Kendo.Mvc.Extensions; 

然後簡單的返回:

return Json(productViewModels 
    .ToDataSourceResult(request, JsonRequestBehavior.AllowGet)); 

這應該照顧任何過濾,排序,分組和分頁爲您服務。

您可能還想看看AutoMapper,以更輕鬆地將Product中的對象映射到ProductViewModel

+0

怎麼發送總數? – Narek

+0

如果您更改代碼'DataManager.ProductRepository.GetAllProducts(request.Page,request.PageSize,ref allcount);'只返回所有對象,'ToDataSourceResult'方法將負責分頁和總計。如果你想將分頁傳遞迴數據庫,那麼我會建議看看AutoMapper,因爲這會讓你做'... ProductRepository.GetAllProducts()。Select(AutoMapper.Map )。ToDataSourceResult(... )'。 – niventc

+0

我不想從數據庫獲取所有數據,因爲如果數據太多,它的工作速度非常慢。 – Narek