Home >Web Front-end >JS Tutorial >How Extjs4.0 ComboBox implements three-level linkage_extjs
Many netizens are asking how to implement Extjs4.0 ComboBox. Fortunately, a three-level linkage was implemented before using 3.x, and now Extjs4.0 is used to achieve the same linkage effect. One thing to note is that model:'local' in 3.x is represented by queryMode: 'local' in Extjs4.0, and reload is used when loading data in 3.x, but it needs to be used in extjs4.0 load to get the data. As shown below:
Code part
Look at the HTML code first:
<html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MHZG.NET-城市三级联动实例</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>
It’s very simple, just load the basic CSS files and JS files, and load the customized combobox.js file.
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'} ] }); //加载省数据源 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 }); //加载市数据源 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 }); //加载区数据源 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:"城市三级联动", 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:'请选择省', blankText : '请选择省', 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("错误","数据加载失败。"); } } } }, { 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:'请选择市', blankText : '请选择市', 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("错误","数据加载失败。"); } } } }, { xtype:"combo", name:'qu', id : 'qu', fieldLabel:'选择区', displayField:'cname', valueField:'id', store:store2, triggerAction:'all', queryMode: 'local', selectOnFocus:true, forceSelection: true, allowBlank:false, editable: true, emptyText:'请选择区', blankText : '请选择区', } ] }) });
In the above code, if you directly define the store data source in the ComboBox, there will be a situation where when you select the first province and click on the second city, it will flash for a moment, and then click again. City data appears. To solve this situation, we must first define the data sources of provinces, cities, and districts. Then when you click again, the above situation will not occur.
In the code, use the store as the province's data and set its autoLoad to true. Then when the page is loaded for the first time, the province's data will be automatically loaded, and then a monitoring select will be added to the province and city. The effect is that when the page is clicked When selecting a province, the city and district data must be cleared, and the corresponding data must be loaded into the city data source based on the currently selected values. Of course, the principles of store1 and store2 are the same.
Finally, the server needs to retrieve data and return the correct data based on the passed value. There is no data query from the database here, but simply writing some test code. I believe there are no problems with the extjs code, then the server Returning data is not a very important thing.
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"":""内蒙古自治区"",""id"":""150000""}") Response.Write("]") End If If act = "shi" Then If param = "110000" Then Response.Write("[") Response.Write("{""cname"":""市辖区"",""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"":""朝阳区"",""id"":""110101""},") Response.Write("{""cname"":""昌平区"",""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"":""回民区"",""id"":""150101""},") Response.Write("{""cname"":""新城区"",""id"":""150102""}") Response.Write("]") ElseIf param = "150200" Then Response.Write("[") Response.Write("{""cname"":""青山区"",""id"":""150201""},") Response.Write("{""cname"":""东河区"",""id"":""150202""}") Response.Write("]") End If End If %>
The above is the entire content of this article, I hope it will be helpful to everyone’s study.