combobox 动态绑定数据源,利用EasyUI的 combobox插件实现类似百度下拉提示信息

 前台

 <script type="text/javascript">
         var vID = "cc";
         $(function () {
             $('#' + vID).combobox({
                 valueField: 'TPrice', //TPrice
                 textField: 'typeName',
                 //注册事件
                 onChange: function (newValue, oldValue) {
                     if (newValue != null) {
                         var thisKey = encodeURIComponent($('#' + vID).combobox('getText')); //搜索词
                         var thisType = encodeURIComponent(document.getElementById("TextBox12").value); //车辆类型
                         var urlStr = "AutoComplete.ashx?objType=" + thisType + "&objStr=" + thisKey;
                         var v = $("#" + vID).combobox("reload", urlStr);
                     }
                 },
                 onSelect: function (record) {
                     setValue(record.typeName);
                     //document.getElementById("TextBox4").value = record.TPrice;
                     $("#TextBox4").val(record.TPrice);
                 
                 }
             });
         });
        function setValue(vTxt) {
            $('#' + vID).combobox('setValue', vTxt);
        }

    </script>

二、后台是利用一般处理程序来返回JSON就可以了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Text;

 

namespace used_car.web
{
    /// <summary>
    /// AutoComplete 的摘要说明
    /// </summary>
    public class AutoComplete : IHttpHandler
    {
        protected DataTable dt = null;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            ClearClientPageCache();
            context.Response.ContentType = "text/plain";       
            string strObjTypee = "", strObjStr = "";
             if (context.Request.QueryString["objType"] != null && context.Request.QueryString["objStr"]!=null)
             {
                 strObjTypee = context.Server.UrlDecode(context.Request.QueryString["objType"].ToString());
                 strObjStr = context.Server.UrlDecode(context.Request.QueryString["objStr"].ToString());
                 dt = linkeMaterials(strObjTypee, strObjStr);
                 if (dt != null)
                 {
                     string data2 = JsonConvert.SerializeObject(dt);                   
                     context.Response.Write(data2);
                     context.Response.Flush();
                     context.Response.End();
                 }
             }
        }

        public DataTable linkeMaterials(object objType, object objStr)
        {
            DataTable dt = new DataTable();
            if (objStr != null)
            {
                if (!string.IsNullOrWhiteSpace(objStr.ToString()))
                {
                    //left(T11,2)='" + objType + "' or 
                   string strSql = "select top 15  C.T46 as typeName, C.T45 as ID,C.T47 as TPrice from [dbo].[JC79] as C where  T46 like'%" + objStr + "%'";
                    DataSet dsJC97 = Maticsoft.DBUtility.DbHelperSQL.Query(strSql);
                    dt = dsJC97.Tables[0];
                }
            }
            return dt;
        }
        StringBuilder sbJC97 = new StringBuilder("");
        public string linkeMaterials2(object objType, object objStr)
        {
            if (objStr != null)
            {
                if (!string.IsNullOrWhiteSpace(objStr.ToString()))
                {
                    //left(T11,2)='" + objType + "' or
                    string strSql = "select top 20  C.T46 as 型号名称, C.T47 as 现行价格, C.T45 as ID,C.T11 as 种类编号 from [dbo].[JC79] as C where T46 like'%" + objStr + "%'";
                    DataSet dsJC97 = Maticsoft.DBUtility.DbHelperSQL.Query(strSql);
                    if (dsJC97 != null)
                    {
                        DataTable dtJC97 = dsJC97.Tables[0];
                        int dtCount = dtJC97.Rows.Count;
                        if (dtCount > 0)
                        {
                            for (int i = 0; i < dtCount; i++)
                            {
                                sbJC97.Append("{ typeName: \"" + dtJC97.Rows[i]["型号名称"] + "\",ID: \"" + dtJC97.Rows[i]["ID"] + "\",Price: \"" + dtJC97.Rows[i]["现行价格"] + "\",ZL: \"" + dtJC97.Rows[i]["种类编号"] + "\"}");
                                if (i != (dtCount - 1))//如果不是最后一个
                                {
                                    sbJC97.Append(",");
                                }
                            }
                        }

                    }
                }
            }

            return sbJC97.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public static void ClearClientPageCache()
        {
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.AddHeader("pragma", "no-cache"); HttpContext.Current.Response.AddHeader("cache-control", "private"); HttpContext.Current.Response.CacheControl = "no-cache";
        }
    }
}

 

你可能感兴趣的:(easyui,combobox,动态绑定数据源)