|
概述
很多地方都存在以Tab頁來呈現(xiàn)數(shù)據(jù)的方式,比如網(wǎng)易、新浪、搜狐、QQ等知名的門戶網(wǎng)站的首頁,還有大家熟知的博客園首頁,都是用了tab頁來顯示數(shù)據(jù)。大家之所以喜歡用Tab,因為它能大大的增加顯示數(shù)據(jù)的空間,能在固定的空間中顯示更多的數(shù)據(jù)。分頁也是為了方便數(shù)據(jù)的顯示,在應(yīng)用系統(tǒng)中必不可少。這篇文章使用Jquery在ASP.NET MVC中使用Tab頁,以及在Tab頁中實現(xiàn)異步無刷新的分頁功能。估計這個大家都會用得到的。
在ASP.NET MVC中實現(xiàn)分頁,在之前的一篇博文:ASP.NET MVC2右鍵菜單和簡單分頁中已經(jīng)實現(xiàn)了。實現(xiàn)的方式很簡單,在table下面加上一段<a/><a/><a/>...的html就行了。
先介紹一個Jquery插件:Tab,可以到http://jqueryui.com/download上下載。看下它自帶一個例子的截圖:
效果圖:
實現(xiàn)
按照它的Demo,在ASP.NET mvc項目中引入js和css,以及Jquery。我在ASP.NET MVC的母板頁中引入這些文件:
<link href="http://www.cnblogs.com/Content/base/ui.all.css" rel="stylesheet" type="text/css" />
<script src="http://www.cnblogs.com/Scripts/jquery-1.4.1.js" type="text/Javascript"></script>
<script src="http://www.cnblogs.com/Scripts/ui.core.js" type="text/Javascript"></script>
<script src="http://www.cnblogs.com/Scripts/ui.tabs.js" type="text/Javascript"></script>
引入之后,參考它的Demo在MVC項目中View中使用Tab。 可以看到比tab自帶的demo多來了一個getContentTab函數(shù)。他有兩個參數(shù),用于表示你要顯示
哪個tab頁的第幾頁數(shù)據(jù)。這里默認加載的時候,顯示tabs-Shoes的第一頁數(shù)據(jù)。
<script type="text/Javascript">
$(document).ready(function () {
$("#tabs").tabs();
getContentTab('Shoes', 1);
});
</script>
<div id="tabs">
<ul>
<li><a href="#tabs-Shoes" onclick="getContentTab('Shoes',1);">Shoes</a></li>
<li><a href="#tabs-Electronics" onclick="getContentTab('Electronics',1);">Electronics</a></li>
<li><a href="#tabs-Food" onclick="getContentTab('Food',1);">Food</a></li>
</ul>
<div id="tabs-Shoes">
</div>
<div id="tabs-Electronics">
</div>
<div id="tabs-Food">
</div>
</div>
當(dāng)然在定義View之前要先寫好控制器的代碼,很簡單,基本上沒有代碼:
public ActionResult ViewCategory()
{
return View();
}
好了,下面開始我們重要的幾步。顯示table以及實現(xiàn)table的分頁。這個demo的tab定義了三個tab頁,每一頁的table結(jié)構(gòu)是一樣的,所以我定義一個用戶控件來實現(xiàn)table和分頁。代碼如下:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcAjaxPaging.Models.ProductViewModel>" %>
<%@ Import Namespace="MvcAjaxPaging.HtmlHelpers"%>
<table class="grid">
<thead>
<tr>
<th>Product name</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<% foreach (var product in ViewData.Model.Products) { %>
<tr>
<td><%= product.Name %></td>
<td><%= product.Category %></td>
</tr>
<% } %>
</tbody>
</table>
<div class="pager">
<%= Html.Pager(ViewData.Model.PagingInfo.CurrentPage, ViewData.Model.PagingInfo.ItemsPerPage, ViewData.Model.PagingInfo.TotalItems, "", ViewData["CategoryDisplayName"] as string)%>
</div>
我們再通過一個ajax調(diào)用來將這個控件顯示在ViewCategory對應(yīng)的View上,定義一個js函數(shù):
function getContentTab(categoryName, page) {
var url = '<%= Url.Content("~/MyPaging/ViewByCategory/") %>' + categoryName + "/" + page;
var targetDiv = "#tabs-" + categoryName;
$.get(url, null, function (result) {
$(targetDiv).html(result);
});
}
我們看上面代碼,我們?nèi)フ埱蠓?wù)端的ViewByCategory方法,獲取table中的數(shù)據(jù)。看ViewByCategory的代碼:
public ActionResult ViewByCategory(string categoryName, int? page)
{
categoryName = categoryName ?? this.categories[0];
int currentPageIndex = page.HasValue ? page.Value : 0;
ProductViewModel productViewModel = new ProductViewModel();
IList<Product> productsByCategory = this.allProducts.Where(p => p.Category.Equals(categoryName)).ToList();
productViewModel.Products = productsByCategory.Skip((currentPageIndex - 1) * 10).Take(10).ToList();
productViewModel.PagingInfo.CurrentPage = currentPageIndex;
productViewModel.PagingInfo.ItemsPerPage = 10;
productViewModel.PagingInfo.TotalItems = productsByCategory.Count;
return View("ViewByCategoryTable", productViewModel);
}
為了簡單起見數(shù)據(jù)來來自內(nèi)存,使用list的take來實現(xiàn)分頁。你可以很方便的改成從DB獲取數(shù)據(jù)。在看下如何生成分頁的html,其實很簡單,我們只要在生成的分頁的HTML中使用getContentTab函數(shù)就行了。
public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords,string urlPrefix,string status)
{
StringBuilder sb1 = new StringBuilder();
int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);
if (currentPage > 0)
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >Previous</a>", status, currentPage));
if (currentPage - currentPageSize >= 0)
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >...</a>", status, (currentPage - currentPageSize) + 1));
for (int i = seed; i < Math.Round((totalRecords / currentPageSize) + 0.5) && i < seed + currentPageSize; i++)
{
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >{1}</a>", status, i + 1));
}
if (currentPage + currentPageSize <= (Math.Round((totalRecords / currentPageSize) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >...</a>", status, (currentPage + currentPageSize) + 1));
if (currentPage < (Math.Round((totalRecords / currentPageSize) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href='#' onclick=getContentTab(/"{0}/",/"{1}/") >Next</a>", status, currentPage + 2));
return sb1.ToString();
}
效果
總結(jié):在ASP.NET mvc中實現(xiàn)了在tab頁中的異步無刷新分頁。這東西太常用了,放在這里,希望對你有所幫助。
代碼:http://cid-aef1e64945224a20.office.live.com/self.ASPx/.Public/MvcAjaxPaging.rar
NET技術(shù):ASP.NET MVC2.0在Tab頁中實現(xiàn)異步無刷新分頁,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。