2012-04-04 37 views
3

Helo, 我正在開發ASP.NET MVC 3應用程序。ASP.NET MVC 3使用升序,降序選項自定義排序WebGrid

我有一個觀點

var grid = new WebGrid(rowsPerPage: 10, ajaxUpdateContainerId: "GridDiv",canPage: true,canSort: true); 
grid.Bind(source: Model); 
grid.Pager(WebGridPagerModes.All); 
@grid.GetHtml(htmlAttributes: new { id="grid" }, 
    columns: grid.Columns(
    grid.Column("Name"), 
    grid.Column("Age"), 
    grid.Column("Sex") 
) 

在控制器我有一個自定義排序算法對數據進行排序。 我有一個自定義升序排序和自定義降序排序。

我希望當用戶點擊列標題來按照我自定義的排序算法對行進行排序而不是在一個版本中進行排序。

對於我嘗試以下(我走「sortdir」,並相應地處理它)

控制器

public ActionResult Persons(string sortdir) 
{  
    PersonsListModel = GetAllPersonsList(); 
    if(sortdir=="ASC") 
     return View(MyAscendingCustomSortAlgorithm(PersonsListModel)); 
    else 
     return View(MyDescendingCustomSortAlgorithm(PersonsListModel)); 
} 

MyAscendingCustomSortAlgorithmMyDescendingCustomSortAlgorithm是返回我的自定義算法排序列表功能。

當頁面加載列表排序正確,但是當我點擊標題的排序是亂了up.I調試,一切工作正常。

我的問題是我怎麼能作出這樣的工作,並且仍然保持正確的尋呼

我也試着設置canSort: false但我不能在標題點擊了。

非常感謝您的任何幫助

+0

http://stackoverflow.com/questions/5413069/header-format-for-webgrid這應該有助於初學者:) – Yasser 2012-04-09 06:45:52

回答

1

你應該尋找這樣:

  1. 禁用排序
  2. 添加自定義風格的列標題(:在.GetHtml「headerStyle customStyle」 html屬性)。檢查章Styling your grid
  3. 使用jQuery一個現場點擊處理程序添加到所有元素與這種風格,而當用戶點擊然後檢查它是塔,並相應地張貼到你的控制器,這將刷新頁面

另外,你可以在CSS中添加一些鏈接到你的customStype的樣式,例如「cursor:hand」,這樣用戶就可以看到標題是可點擊的。

1

我會爲您的方法參數添加頁碼,行數,排序列和排序方向。它也應該只返回json數據。您應該在JavaScript中處理這些數據以更新您的視圖。

您可以將自己的排序css類添加到標題並鏈接到ajax操作。它可能會解決你的網格在JavaScript中,只是改變數據行。

public JsonResult Persons(int pagenumber,int rows, string sortcol, string sortdir) 
{  

if(sortdir=="ASC") 
    return DataSource(pagenumber,rows,sortcol,MyAscendingCustomSortAlgorithm); 
else 
    return DataSource(pagenumber,rows,sortcol,MyDescendingCustomSortAlgorithm); 

} 

你可以通過你的排序功能,爲數據源調用並用它來返回JSON數據。您可以使用linq查詢來排列

2

您是否嘗試關閉Bind()方法中的autoSortAndPage參數?

grid.Bind(source: Model, autoSortAndPage : false); 

我知道你想保持現有的呼叫,但我不知道有辦法兼得。您可能需要在您的操作方法中手動進行分頁。