| 代碼如下 |
複製代碼 |
<link rel="stylesheet" href="Datatables/css/demo_table.css"type="text/css" /> <script type="text/javascript" src="Datatables/js/jquery.js"></script> <script type="text/javascript" src="Datatables/js/jquery.dataTables.js"></script> |
然後,讓我們添加 Repeater,記住,JQuery的DataTables神奇之處在於,他們可以操作任何HTML表額。事實上,他們的網站表明DataTables,“可以給任何HTML表添加進階互動控制”,因此,我們可以將一個表嵌入到我們的Repeater。我猜,大家之前應該都接觸(如果你閱讀了上一篇文章)過Repeaters和tables,所以我下面的代碼,是將表格結構嵌入到Repeater,你可以將下面的代碼放到form下,或者指定的 <div>內。
| 代碼如下 |
複製代碼 |
<asp:Repeater ID="rptOscarNominees" runat="server"> <HeaderTemplate> <table id="tblOscarNominees" cellpadding="0" cellspacing="0" border="0" class="display"> <thead> <tr> <th>Movie</th> <th>Lead Actor</th> <th>Lead Actress</th> <th>Director</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td><% # Eval("MovieTitle") %></td> <td><% # Eval("LeadingActor") %></td> <td><% # Eval("LeadingActress") %></td> <td><% # Eval("Director") %></td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> |
正如你所看到的,我們的列名放在了<th>標籤裡,我們的資料繫結元素放在<td>標籤裡。DataTables庫會很好的處理這一段代碼。說到DataTables,要在文檔的<head>部分添加JQuery代碼,事實上,我們已經在前幾分鐘添加了<script>的連結。(As you can see, our column names are within <th> tags, and our databound elements are within <td> tags. The DataTables library will take care of being the brains of this operation. Speaking of the DataTables, now would be a good time to add the actual JQuery code to the <head> section of this document. In fact, place the following code directly underneath our <script> links that we added a few minutes ago.)
| 代碼如下 |
複製代碼 |
<script type="text/javascript"> $(document).ready(function() { $('#tblOscarNominees').dataTable({ "oLanguage": { "sSearch": "Search the nominees:" }, "iDisplayLength": 10, "aaSorting": [[0, "asc"]]}); }); </script> |
上面的代碼可以讓tblOscarNominees表格成為DataTable,並設定一些屬性。
在這個例子裡面,你也許會想,這個“oLanguage and sSearch”屬性允許我們在指定搜尋欄文本,這個“iDisplayLength”屬性可以設定預設的顯示行數。如果你查看DataTables網站,這個外掛程式有許多可以設定的屬性。
好了,最後,我們需要把資料繫結到我們的Repeater上,將Default.aspx.cs的codebehind的class返回給,下面的代碼,我們對我們提名名單上增加了“Milk”,我們需要設定資料來源並綁定到repeater。
| 代碼如下 |
複製代碼 |
rptOscarNominees.DataSource = nominees; rptOscarNominees.DataBind(); |
運行這個網站,會彈出如下所示的頁面:
繼續,輸入搜尋的東西,沒有傳入伺服器,排序列表,方便快捷。分頁沒有自訂代碼,幾年前,這個外掛程式節省了我大量的時間,我超級喜愛它。