Tutorial:Linked Combos Tutorial for Ext 2 (Chinese) (Legacy)

This version of our Learning Center is unmaintained.
This article may be out-of-date or contain incorrect information.
Please visit the new Sencha Learning Center for up-to-date material.

Go to the new Sencha Learning Center

From Sencha - Learn

Jump to: navigation, search
Summary: 非常简单的Ext 2 联动Combos例子
Author: Jozef Sakalos(译者:Frank Cheung)
Published: October 20, 2007
Ext Version: 2.0
Languages: en.png English cn.png Chinese

fr.png French br.png Portuguese

Contents

序言

与逐步(Step by Step)式引导的教学不同,本教程着重例子的演示而非由浅入深式的描述,只有摘要式的解释。整个例子由两份文件组成:lcombo.html 以及 lcombo.js

lcombo.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="../extjs-2.0/resources/css/ext-all.css">
    <script type="text/javascript" src="../extjs-2.0/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../extjs-2.0/ext-all-debug.js"></script>
    <script type="text/javascript" src="lcombo.js"></script>
    <!-- 这里可添置本地化的文件 -->
    <script type="text/javascript">
        Ext.onReady(LCombo.app.init, LCombo.app);
    </script>
    <title>联动Combos例子</title>
</head>
<body>
</body>
</html>

这就是运行Ext最基本的html结构。其中要改变的只是引用Ext库(JS文件)的src地址。

lcombo.js

/**
  * Ext 2.0 Linked Combos Tutorial
  * by Jozef Sakalos, aka Saki
  * http://extjs.com/learn/Tutorial:Linked_Combos_Tutorial_for_Ext_2
  */
 
// reference local blank image
Ext.BLANK_IMAGE_URL = '../extjs-2.0/resources/images/default/s.gif';
 
Ext.namespace('LCombo', 'LCombo.countries', 'LCombo.cities');
 
LCombo.countries = [
     ['USA', 'United States of America']
    ,['D', 'Germany']
    ,['F', 'France']
    ,['GB', 'Great Britain']
];
 
LCombo.cities = [
     [1, 'USA', 'New York']
    ,[2, 'USA', 'Cleveland']
    ,[3, 'USA', 'Austin']
    ,[4, 'USA', 'Los Angeles']
    ,[5, 'D', 'Berlin']
    ,[6, 'D', 'Bonn']
    ,[7, 'F', 'Paris']
    ,[8, 'F', 'Nice']
    ,[9, 'GB', 'London']
    ,[10, 'GB', 'Glasgow']
    ,[11, 'GB', 'Liverpool']
];
 
// create application
LCombo.app = function() {
    // do NOT access DOM from here; elements don't exist yet
 
    // private variables
 
    // private functions
 
    // public space
    return {
 
        // public methods
        init: function() {
            var form = new Ext.FormPanel({
                 renderTo:document.body
                ,width: 400
                ,height: 300
                ,style:'margin:16px'
                ,bodyStyle:'padding:10px'
                ,title:'Linked Combos'
                ,defaults: {xtype:'combo'}
                ,items:[{
                     fieldLabel:'Select Country'
                    ,displayField:'country'
                    ,valueField:'cid'
                    ,store: new Ext.data.SimpleStore({
                         fields:['cid', 'country']
                        ,data:LCombo.countries
                    })
                    ,triggerAction:'all'
                    ,mode:'local'
                    ,listeners:{select:{fn:function(combo, value) {
                        var comboCity = Ext.getCmp('combo-city');        
                        comboCity.clearValue();
                        comboCity.store.filter('cid', combo.getValue());
                        }}
                    }
 
                },{
                     fieldLabel:'Select City'
                    ,displayField:'city'
                    ,valueField:'id'
                    ,id:'combo-city'
                    ,store: new Ext.data.SimpleStore({
                         fields:['id', 'cid', 'city']
                        ,data:LCombo.cities
                    })
                    ,triggerAction:'all'
                    ,mode:'local'
                    ,lastQuery:''
                }]
            });
        }
    };
}(); // end of app
 
// end of file

包含两个combos的form表单被创建在这份文件中,我们再略施技巧:

  • 国家的那combo(父combo)拥有select(选择)事件侦听器,当选中后,城市的那个combo(子combo)就会选出对应国家下面的多个城市。
  • 城市的那个combo有lastQuery:""。这是人为地让combo进行路由过滤,使得当第一个combox加载时,城市combo认为已经是加载过一次的了。

要点

本例不算太复杂,例如:ComboBox的内置设计就没有考滤过到内部Store筛选的问题,这就是说,如果你开始键入城市的Combo组件输入框,筛选器就会清除父Combo指派的条件,从而在所有的范围中数据进行筛选。

如果你想把联动Combox设计得更趋于合理些,——有个不错的方法是,侦听combo的beforequery事件,在处理函数中编写自定义的筛选机制,适当地返回false,这样内置查询逻辑就不会随便筛选。最后再用Ext.ComboBox的doQuery方法建模。

This page was last modified on 15 May 2009, at 20:34. This page has been accessed 12,695 times.