Extjs4.0 ComboBox如何實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
來(lái)源:易賢網(wǎng) 閱讀:730 次 日期:2016-07-01 14:28:38
溫馨提示:易賢網(wǎng)小編為您整理了“Extjs4.0 ComboBox如何實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)”,方便廣大網(wǎng)友查閱!

很多網(wǎng)友在問(wèn),Extjs4.0 ComboBox如何實(shí)現(xiàn),好在之前用3.x實(shí)現(xiàn)過(guò)一個(gè)三級(jí)聯(lián)動(dòng),如今用Extjs4.0來(lái)實(shí)現(xiàn)同樣的聯(lián)動(dòng)效果。其中注意的一點(diǎn)就是,3.x中的model:'local'在Extjs4.0中用queryMode: 'local'來(lái)表示,而且在3.x中Load數(shù)據(jù)時(shí)用reload,但是在extjs4.0中要使用load來(lái)獲取數(shù)據(jù)。如下圖:

名單

代碼部分

先看HTML代碼:

<html >

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>MHZG.NET-城市三級(jí)聯(lián)動(dòng)實(shí)例</title>

<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />

<script type="text/javascript" src="../../bootstrap.js"></script>

<script type="text/javascript" src="../../locale/ext-lang-zh_CN.js"></script>

<script type="text/javascript" src="combobox.js"></script>

</head>

<body>

</body>

</html>

簡(jiǎn)單的很,就是加載了基本的CSS文件和JS文件,并且加載自定義的combobox.js文件。

combobox.js:

Ext.require('Ext.*');

Ext.onReady(function(){

 //定義ComboBox模型

 Ext.define('State', {

   extend: 'Ext.data.Model',

   fields: [

     {type: 'int', name: 'id'},

     {type: 'string', name: 'cname'}

   ]

 });

 //加載省數(shù)據(jù)源

 var store = Ext.create('Ext.data.Store', {

   model: 'State',

   proxy: {

     type: 'ajax',

     url: 'city.asp?act=sheng&n='+new Date().getTime()+''

   },

   autoLoad: true,

   remoteSort:true

 });

 //加載市數(shù)據(jù)源

 var store1 = Ext.create('Ext.data.Store', {

   model: 'State',

   proxy: {

     type: 'ajax',

     url: 'city.asp?act=shi&n='+new Date().getTime()+''

   },

   autoLoad: false,

   remoteSort:true

 });

 //加載區(qū)數(shù)據(jù)源

 var store2 = Ext.create('Ext.data.Store', {

   model: 'State',

   proxy: {

     type: 'ajax',

     url: 'city.asp?act=qu&n='+new Date().getTime()+''

   },

   autoLoad: false,

   remoteSort:true

 });

 Ext.create("Ext.panel.Panel",{

  renderTo: document.body,

  width:290,

  height:220,

  title:"城市三級(jí)聯(lián)動(dòng)",

  plain: true,

  margin:'30 10 0 80',

  bodyStyle: "padding: 45px 15px 15px 15px;",

  defaults :{

    autoScroll: true,

    bodyPadding: 10

  },

  items:[{

    xtype:"combo",

    name:'sheng',

    id : 'sheng',

    fieldLabel:'選擇省',

    displayField:'cname',

    valueField:'id',

    store:store,

    triggerAction:'all',

    queryMode: 'local', 

    selectOnFocus:true,

    forceSelection: true,

    allowBlank:false,

    editable: true,

    emptyText:'請(qǐng)選擇省',

    blankText : '請(qǐng)選擇省',

    listeners:{  

      select:function(combo, record,index){

         try{

           //userAdd = record.data.name;

           var parent=Ext.getCmp('shi');

           var parent1 = Ext.getCmp("qu");

           parent.clearValue();

           parent1.clearValue();

           parent.store.load({params:{param:this.value}});

         }

         catch(ex){

           Ext.MessageBox.alert("錯(cuò)誤","數(shù)據(jù)加載失敗。");

         }

      }

    }

    },

    {

    xtype:"combo",

    name:'shi',

    id : 'shi',

    fieldLabel:'選擇市',

    displayField:'cname',

    valueField:'id',

    store:store1,

    triggerAction:'all',

    queryMode: 'local',

    selectOnFocus:true,

    forceSelection: true,

    allowBlank:false,

    editable: true,

    emptyText:'請(qǐng)選擇市',

    blankText : '請(qǐng)選擇市',

    listeners:{  

      select:function(combo, record,index){

         try{

           //userAdd = record.data.name;

           var parent = Ext.getCmp("qu");

           parent.clearValue();

           parent.store.load({params:{param:this.value}});

         }

         catch(ex){

           Ext.MessageBox.alert("錯(cuò)誤","數(shù)據(jù)加載失敗。");

         }

      }

    }

    },

    {

    xtype:"combo",

    name:'qu',

    id : 'qu',

    fieldLabel:'選擇區(qū)',

    displayField:'cname',

    valueField:'id',

    store:store2,

    triggerAction:'all',

    queryMode: 'local',

    selectOnFocus:true,

    forceSelection: true,

    allowBlank:false,

    editable: true,

    emptyText:'請(qǐng)選擇區(qū)',

    blankText : '請(qǐng)選擇區(qū)',

    }

  ]

 })

});

上述代碼中,如果在ComboBox直接定義store數(shù)據(jù)源,會(huì)出現(xiàn)這樣一種情況,那就是當(dāng)選擇完第一個(gè)省,點(diǎn)擊第二個(gè)市的時(shí)候,會(huì)閃一下,再點(diǎn)擊,才會(huì)出現(xiàn)市的數(shù)據(jù)。那么要解決這樣的情況,那么必須先要定義好省、市、區(qū)的數(shù)據(jù)源。那么再點(diǎn)擊的時(shí)候,就不會(huì)出現(xiàn)上述情況了。

代碼中,使用store為省的數(shù)據(jù),設(shè)置其autoLoad為true,那么頁(yè)面第一次加載的時(shí)候,就會(huì)自動(dòng)加載省的數(shù)據(jù),然后給省和市添加了監(jiān)聽(tīng)select,作用在于當(dāng)點(diǎn)擊省的時(shí)候,要清空市和區(qū)的數(shù)據(jù),并根據(jù)當(dāng)前選定的值去加載對(duì)應(yīng)的數(shù)據(jù)到市的數(shù)據(jù)源中。當(dāng)然store1和store2原理是一樣的。

最后,服務(wù)端要根據(jù)傳的值進(jìn)行數(shù)據(jù)檢索及返回正確數(shù)據(jù),這里沒(méi)有從數(shù)據(jù)庫(kù)中查詢(xún)數(shù)據(jù),而只是簡(jiǎn)單的寫(xiě)了一些測(cè)試代碼,相信extjs代碼沒(méi)有任何的問(wèn)題了,那么服務(wù)端返回?cái)?shù)據(jù),就不是一件很重要的事情了。

City.asp:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<%

  Response.ContentType = "text/html"

  Response.Charset = "UTF-8"

%>

<%

  Dim act:act = Request("act")

  Dim param : param = Request("param")

  If act = "sheng" Then

    Response.Write("[")

    Response.Write("{""cname"":""北京市"",""id"":""110000""},")

    Response.Write("{""cname"":""內(nèi)蒙古自治區(qū)"",""id"":""150000""}")

    Response.Write("]")

  End If

  If act = "shi" Then

    If param = "110000" Then

      Response.Write("[")

      Response.Write("{""cname"":""市轄區(qū)"",""id"":""110100""},")

      Response.Write("{""cname"":""市轄縣"",""id"":""110200""}")

      Response.Write("]")

    ElseIf param = "150000" Then

      Response.Write("[")

      Response.Write("{""cname"":""呼和浩特市"",""id"":""150100""},")

      Response.Write("{""cname"":""包頭市"",""id"":""150200""}")

      Response.Write("]")

    End If

  End If

  If act = "qu" Then

    If param = "110100" Then

      Response.Write("[")

      Response.Write("{""cname"":""朝陽(yáng)區(qū)"",""id"":""110101""},")

      Response.Write("{""cname"":""昌平區(qū)"",""id"":""110102""}")

      Response.Write("]")

    ElseIf param = "110200" Then

      Response.Write("[")

      Response.Write("{""cname"":""密云縣"",""id"":""110201""},")

      Response.Write("{""cname"":""房山縣"",""id"":""110202""}")

      Response.Write("]")

    ElseIf param = "150100" Then

      Response.Write("[")

      Response.Write("{""cname"":""回民區(qū)"",""id"":""150101""},")

      Response.Write("{""cname"":""新城區(qū)"",""id"":""150102""}")

      Response.Write("]")

    ElseIf param = "150200" Then

      Response.Write("[")

      Response.Write("{""cname"":""青山區(qū)"",""id"":""150201""},")

      Response.Write("{""cname"":""東河區(qū)"",""id"":""150202""}")

      Response.Write("]")

    End If

  End If

%>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:Extjs4.0 ComboBox如何實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)