右鍵菜單非常方便,很多時(shí)候會(huì)用到。這篇文章將使用一個(gè)JQUERY的插件在asp.net mvc中實(shí)現(xiàn)右鍵菜單。本文還將介紹一下在asp.net mvc中如何實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)。效果如下圖:
查看原圖(大圖)
首先,下載此插件。
新建一個(gè)asp.net mvc應(yīng)用程序。將此插件放入Scripts文件夾。并在頁(yè)面上引用。
這個(gè)demo使用到NORTHWND數(shù)據(jù)庫(kù)的Product表。
定義右鍵菜單:
1 <div class="contextMenu" id="myMenu1">
2 <ul>
3 <li id="detail"><img src="http://www.cnblogs.com/Content/detail.ico" />detail</li>
4 <li id="new"><img src="http://www.cnblogs.com/Content/new.ico" />new</li>
5 <li id="delete"> <img src="http://www.cnblogs.com/Content/delete.ico"/>delete</li>
6 <li id="modify"><img src="http://www.cnblogs.com/Content/modify.ico"/>modify</li>
7 </ul>
8 </div>
將此菜單定義在產(chǎn)品名上,故在在產(chǎn)品名上添加一個(gè)class供jquery選擇。
<td class="showContext" id="<%= item.ProductID %>"><%: item.ProductName %></td>
在頁(yè)面上插入下面腳本。用于綁定菜單項(xiàng)的行為。為了簡(jiǎn)單起見(jiàn),將所以的菜單項(xiàng)的行為都定義成導(dǎo)航到詳情頁(yè)面。
1 <script type="text/javascript">
2
3 $(document).ready(function () {
4 $('td.showContext').contextMenu('myMenu1', {
5 bindings: {
6 'detail': function (t) {
7 document.location.href = '/Products/Detail/'+t.id;
8 },
9 'new': function (t) {
10 document.location.href = '/Products/Detail/' + t.id;
11 },
12 'delete': function (t) {
13 confirm("你確定刪除嗎?");
14 document.location.href = '/Products/Detail/' + t.id;
15 },
16 'modify': function (t) {
17 document.location.href = '/Products/Detail/' + t.id;
18 }
19 }
20 });
21 });
22
23 </script>
這樣就非常簡(jiǎn)單的實(shí)現(xiàn)了右鍵菜單的功能。
下面說(shuō)下實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)。asp.net mvc中分頁(yè)非常簡(jiǎn)單。
看下面定義的table的html代碼:
1 <table>
2 <tr>
3 <th>
4 ProductName
5 </th>
6 <th>
7 SupplierID
8 </th>
9 <th>
10 CategoryID
11 </th>
12 <th>
13 QuantityPerUnit
14 </th>
15 <th>
16 UnitPrice
17 </th>
18 <th>
19 UnitsInStock
20 </th>
21 <th>
22 UnitsOnOrder
23 </th>
24 <th>
25 ReorderLevel
26 </th>
27 <th>
28 Discontinued
29 </th>
30 </tr>
31
32 <% foreach (var item in Model.Products)
33 { %>
34 <tr>
35 <td class="showContext" id="<%= item.ProductID %>"><%: item.ProductName %></td>
36 <td>
37 <%: item.SupplierID %>
38 </td>
39 <td>
40 <%: item.CategoryID %>
41 </td>
42 <td>
43 <%: item.QuantityPerUnit %>
44 </td>
45 <td>
46 <%: String.Format("{0:F}", item.UnitPrice) %>
47 </td>
48 <td>
49 <%: item.UnitsInStock %>
50 </td>
51 <td>
52 <%: item.UnitsOnOrder %>
53 </td>
54 <td>
55 <%: item.ReorderLevel %>
56 </td>
57 <td>
58 <%: item.Discontinued %>
59 </td>
60 </tr>
61
62 <% } %>
63
64 </table>
我們只要在這個(gè)table下面插入一段分頁(yè)的HTML腳本就行了。分頁(yè)的腳本當(dāng)然要生成,使用Htmlhelper的擴(kuò)展方法去生成這個(gè)腳本。看下面的擴(kuò)展方法,非常的簡(jiǎn)單的生成了分頁(yè)的html代碼:
1 public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords, string urlPrefix)
2 {
3 StringBuilder sb1 = new StringBuilder();
4
5 int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);
6
7 if (currentPage > 0)
8 sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Previous</a>", urlPrefix, currentPage));
9
10 if (currentPage - currentPageSize >= 0)
11 sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));
12
13 for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)
14 {
15 sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", urlPrefix, i + 1));
16 }
17
18 if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
19 sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));
20
21 if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
22 sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", urlPrefix, currentPage + 2));
23
24 return sb1.ToString();
25 }
然后在table后面添加下面的代碼,在table下面輸出分頁(yè)的html代碼:
<div class="pager">
<%=Html.Pager(Model.CurrentPage, Model.TotalPages,Model.TotalItems ,"/Products/List")%>
</div>
這樣就完成分頁(yè)和右鍵菜單的功能了。是不是非常的簡(jiǎn)單呢。:)
效果:
查看原圖(大圖)
顯示:
查看原圖(大圖)
如果有興趣可以下載代碼。
總結(jié):在asp.net mvc中實(shí)現(xiàn)右鍵菜單和簡(jiǎn)單的分頁(yè)。
出處:http://zhuqil.cnblogs.com
本文示例源代碼或素材下載
聯(lián)系客服