下面我們就來看一款如何利用jquery 實現表格的簡單篩選吧。
1:$("#<%=gridview1.clientid %> tr")選擇表格的所有行;
2:not(":first"):除去第一行表頭行;
3:filter(":contains('" + filtertext + "')"):從上面所選擇的行裡面篩選出行文本中包含filtertext 的行顯示出來;
4:最後加一句keyup()是為了在提交後重新觸發keyup事件。(但是在這裡沒有作用因為我用的用戶端控制項沒有viewstate
若是伺服器端控制項就會看見他的作用)。
jquery的選取器的強大之處,讓我們能救這麼簡單的實現用戶端的簡單篩選。最後加一句關於表格篩選有jquery外掛程式提供
下面我們就來看一款如何利用jquery 實現表格的簡單篩選吧。
$(function() {
$("#text1").keyup(function() {
var filtertext = $(this).val();
$("#<%=gridview1.clientid %> tr").not(":first").hide().filter(":contains('" + filtertext + "')").show();;
}).keyup();
});
前面代碼
<%@ page language="c#" autoeventwireup="true" codefile="jquerytablefilter.asp教程x.cs" inherits="jquerytablefilter" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://mb.111cn.net/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head runat="server">
<title></title>
<script src="script/jquery-1.3.2-vsdoc.網頁特效" type="text/網頁特效"></script>
<script type="text/網頁特效">
$(function() {
$("#text1").keyup(function() {
var filtertext = $(this).val();
$("#<%=gridview1.clientid %> tr").not(":first").hide().filter(":contains('" + filtertext + "')").show();;
}).keyup();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:60%;">
<input id="text1" type="text" />
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"
datakeynames="orderid" datasourceid="sqldatasource1"
horizontalalign="left" pagesize="50" >
<columns>
<asp:boundfield datafield="orderid" headertext="orderid" readonly="true"
sortexpression="orderid" insertvisible="false" />
<asp:boundfield datafield="customerid" headertext="customerid"
sortexpression="customerid" />
<asp:boundfield datafield="employeeid" headertext="employeeid"
sortexpression="employeeid" />
<asp:boundfield datafield="orderdate" headertext="orderdate"
sortexpression="orderdate" />
<asp:boundfield datafield="requireddate" headertext="requireddate"
sortexpression="requireddate" />
<asp:boundfield datafield="shippeddate" headertext="shippeddate"
sortexpression="shippeddate" />
<asp:boundfield datafield="shipvia" headertext="shipvia"
sortexpression="shipvia" />
<asp:boundfield datafield="freight" headertext="freight"
sortexpression="freight" />
</columns>
</asp:gridview>
<asp:sqldatasource id="sqldatasource1" runat="server"
connectionstring="<%$ connectionstrings:northwindconnectionstring %>"
selectcommand="select top 50 * from [orders]"></asp:sqldatasource>
</div>
</form>
</body>
</html>