従業員を検索する目的で、c# asp.net アプリケーションに jquery テキストボックスの自動提案を実装したいと考えています。インデックスを使用せずに巨大なデータを高速に検索するための優れたアイデアがあれば、私と共有してください。
3 に答える
次の jQuery 自動検索の詳細: このコードを .aspx ファイルに書き留めます。ここで txtSearchBox は検索ボックス名です。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>AutoComplete Box with jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
SearchText();
});
function SearchText() {
$("#txtSearch").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("Error");
}
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel>
<ContentTemplate>
<div class="demo">
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
<asp:TextBox ID="txtSearch" runat="server" AutoCompleteType="Search"> </asp:TextBox>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
今 .cs ファイルの詳細:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=devserver;Initial Catalog=Catalog;Persist Security Info=True;User ID=userName;Password=Password"))
{
using (SqlCommand cmd = new SqlCommand("select (strEmployeeName + ',' + strEmployeeCode) as username from tblEmployee where strEmployeeName LIKE '%'+@SearchText+'%' ", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["username"].ToString());
}
return result;
}
}
}
}
必要に応じて、次のようなオブジェクト データ ソースを使用できます。この場合、オブジェクト メソッドは List タイプのデータを返す必要があります。
[WebMethod]
public static List<string> GetAutoCompleteData(string strSearchKey)
{
AutoSearch_BLL objAutoSearch_BLL = new AutoSearch_BLL();
List<string> result = new List<string>();
result = objAutoSearch_BLL.AutoSearchEmployeesData(strSearchKey);
return result;
}
jQuery UI は優れたオートコンプリートの実装を提供し、ドキュメントでは、非常に大規模なデータベースからオートコンプリートの提案を取得するために使用できることが示唆されています
ローカルおよび/またはリモート ソースからデータを取得できます。ローカルは小さなデータ セット (50 エントリのアドレス帳など) に適しています。から選択します。
http://jqueryui.com/demos/autocomplete/
ブラウザに 50K の提案を返している場合 (50K の可能性のプールから提案を引き出すのではなく)、何か間違ったことをしていることに注意してください(ネットワークを介して大量のデータをプッシュすることになります)。
jQuery UI オートコンプリートの実装は、次のように簡単です。
$(function(){
$( "#txtEmployee" ).autocomplete({
source: "employees.aspx",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
jQuery UI オートコンプリートは、一部のクライアント側のデータをサポートしcaching
ています。これにより、検索速度が確実に向上します。また、オートコンプリートが毎回データベースにクエリを実行しないように、従業員のリストを格納するアプリケーションにキャッシュ レイヤーを実装することを検討することもできます。代わりに、キャッシュ レイヤーからデータをフェッチします。