1. 程式人生 > >easyUI-combobox 動態繫結資料來源

easyUI-combobox 動態繫結資料來源

前臺

 <link rel="stylesheet" type="text/css" href="../css/easyui.css"/>
 <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
 <script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
     <script type="text/javascript">
         var vID = "DDLCC";
         $(function () {
             $('#' + vID).combobox({
                 valueField: 'TPrice', //TPrice
                 textField: 'typeName',
                 //註冊事件
                 onChange: function (newValue, oldValue) {
                     if (newValue != null) {
                         var thisKey = encodeURIComponent($('#' + vID).combobox('getValue')); //搜尋詞
                         var thisType = ""; //車輛型別
                         var urlStr = "AutoComplete.ashx?objType=" + thisType + "&objStr=" + thisKey;
                         $("#" + 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>
  <style type="text/css">
   .combo
   {
   height:15px;
   border:1px  solid #CECCCD;     
   overflow :hidden ;
   }
   .combo .combo-text{
   height:15px; 
   font-size:12px;
   line-height:15px;
   color :#000000;
   }
   .combo .combo-arrow{
 background:#E0ECF9 url('../css/images/combo_arrow.gif') no-repeat 0px 0px;
 width:14px; 
 height:15px;
 overflow:hidden; 
 vertical-align:middle;
 cursor:pointer;
 opacity:0.6;
 filter:alpha(opacity=60);
   }
 </style>

    <select id="DDLCC" class="easyui-combobox" name="DDLCC"  style="width:84px;" data-options="required:true" title="鍵入搜尋查詢" >                           
         </select>

後臺

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 15  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";
        }
    }
}