input输入框查询,ajax搜索数据列表,json转码数组,选中展示指定内容
这是一个双输入框的一个复合查询,第一个textbox是为了展示搜索出来的内容,第二个input输入框是提供条件输入,得到对应的查询数据,然后点击下面的条件数据,给第一个textbox展示内容,然后存储到数据库中;
前端代码:
<span class="cg_bj_info_tit"><span>*</span>客户:</span>
<span class="cg_bj_info_nr">
<asp:TextBox ID="TxtCustomer" CssClass="cgdlb_filter_input" placeholder="请填写客户" runat="server"></asp:TextBox>
<input class="cgd_input cgdlb_filter_input" type="text" id="ddlCustomer" placeholder="请填写客户"
oninput="BusinessCls.BusinessAutoComplate(this);" onpropertychange="BusinessCls.BusinessAutoComplate(this);" />
<div class="cgcp_search_info_box fbPro_ss_info" style="display: none;"></div>
</span>
jquery代码:
//出差相关的客户搜索
var BusinessCls = {
//客户搜索自动完成
BusinessAutoComplate: function (txtobj) {
var keyword = $(txtobj).val();
if (keyword == "") {
return;
}
$.ajax({
type: "POST", url: "Serv.ashx?op=businessTripApplicationSearch",
data: "keyword=" + keyword + "",
success: function (res) {
$(".cgcp_search_info_box").empty();
var data = eval(res).message;
//data输出内容: "[{"Key":45,"Value":"XX轨道工程有限公司"},{"Key":24,"Value":"XX纤维有限公司"},{"Key":41,"Value":"XX轨道交通"},{"Key":39,"Value":"XX集团有限公司"}]"
if (data && data != null && data.length > 0) {
$.each($.parseJSON(data), function (i, item) {
$(".cgcp_search_info_box").append("<div class='cgcp_search_info' data-id='" + item.Key + "' data-name='" + item.Value + "'>" + item.Value + "</div>");
});
$(".cgcp_search_info_box div").unbind("click");
$(".cgcp_search_info_box div").on("click", function () {
$("#TxtCustomer").val($(this).attr("data-name"));
$(".cgcp_search_info_box").hide();
});
} else {
$(".cgcp_search_info_box").text("没有找到任何产品");
}
$(".cgcp_search_info_box").show();
}
});
}
}
ashx代码:
public class Serv : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
protected string ConnStr = Utility.GetConnectionString("XXX");
public void ProcessRequest(HttpContext context)
{
context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
if (DataConvert.ToInt(context.Session["LoginID"]) == 0)
{
context.Response.Write("{\"code\":1,\"message\":\"您尚未登录或登录已超时,请先登录!\",\"data\":\"\"}");
}
else
{
string op = DataConvert.ToString(context.Request.QueryString["op"]);
switch (op)
{
case "businessTripApplicationSearch":
BusinessTripApplicationSearch(context);
break;
}
}
context.Response.End();
}
public void BusinessTripApplicationSearch(HttpContext context)
{
string keyword = DataConvert.ToString(context.Request["keyword"]);
if (string.IsNullOrEmpty(keyword))
{
Write(context, -1, "搜索内容不能为空", string.Empty);
}
keyword = Utility.EncodeTitleText(keyword);
List<object> resultdata = new List<object>();
string querysql = @"select top 10 Id,CustomerName from GYL_CG_OA_Customer where IsDeleted = 0 and CustomerName like '%" + keyword + @"%'";
DataSet resultds = DB.DataSet(Common.KeyWordB2C, querysql);
if (DataHelper.ExistsDataSet(resultds))
{
foreach (DataRow row in resultds.Tables[0].Rows)
{
int CustomerId = DataConvert.ToInt(row["Id"]);
string CustomerName = Utility.DecodeTitleText(DataConvert.ToString(row["CustomerName"]));
resultdata.Add(new
{
Key = CustomerId,
Value = CustomerName
});
}
}
Write(context, 0, Newtonsoft.Json.JsonConvert.SerializeObject(resultdata), string.Empty);
}
}