[英]Autocomplete textbox don't show result inside clicking of textbox
I have a autocomplete
textbox
which filters exactly what I want to search for. 我有一个
autocomplete
textbox
,它会完全过滤我要搜索的内容。 But now what I want is, 但是现在我想要的是
If user doesn't types anything into the
textbox
and just clicks thetextbox
.如果用户未在
textbox
任何内容,只需单击textbox
。 It should show all the result.它应该显示所有结果。 Is this possbile ?
这有可能吗?
Below is my code. 下面是我的代码。
$(document).ready(function () {
SearchText();
});
function SearchText() {
$("#txt712").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Frm_Agreement_Master.aspx/GetAutoCompleteData",
data: "{'username':'" + extractLast(request.term) + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
},
focus: function () {
return false;
},
select: function (event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
});
$("#txt712").bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
}
Also find the link from where I got the reference. 也可以从获得参考的地方找到链接。
UPDATE 更新
Server side code 服务器端代码
[WebMethod]
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (OracleConnection ObjPriCon = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString()))
{
using (OracleCommand cmd = new OracleCommand("select distinct survey_area_7_12 FROM xxcus.xxacl_pn_farming_mst WHERE survey_area_7_12 LIKE '%' || :searchtext || '%'", ObjPriCon))
{
ObjPriCon.Open();
cmd.Parameters.AddWithValue(":searchtext", username.ToLower());
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
result.Add(dr["survey_area_7_12"].ToString());
}
}
return result;
}
}
}
In Server code check if the search string is null or not and trigger the query accorduingly 在服务器代码中检查搜索字符串是否为null并相应触发查询
public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
**//check if user name is not null**
if(null != username){
using (SqlCommand cmd = new SqlCommand("select DISTINCT UserName from UserInformation where UserName LIKE '%'+@SearchText+'%'", con))}
else{
using (SqlCommand cmd = new SqlCommand("select UserName from UserInformation, con))}
}
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["UserName"].ToString());
}
You can use "Focus" event to do this. 您可以使用“焦点”事件来执行此操作。
.focus(function () {
$(this).autocomplete("search");
});
Full code as below. 完整代码如下。
$("#txt712").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Frm_Agreement_Master.aspx/GetAutoCompleteData",
data: "{'username':'" + extractLast(request.term) + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
},
focus: function () {
$(this).autocomplete("search"); //New Code
},
select: function (event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.