『ExtJS』01 007. ExtJS 4 查找組件


Ext JS 4 使用一個新的幫助類(Ext.ComponentQuery)來使用類似CSSXPath風格的選擇器去獲取ExtJS組件。

在本文中,我們將展示如何使用Ext.ComponentQuery類來獲得幫助信息以指定一個小應用內的組件。

准備工作


我們將創建一個簡單的應用,由一個帶工具條、按鈕、表單、grid的Ext.panel.Panel。

Language: JavaScript

Framework: ExtJS 4.1.1a

IDE: Excplise J2EE + Spket

   1: var panel = Ext.create('Ext.panel.Panel', {
   2:     height : 500,
   3:     wight : 500,
   4:     renderTo : Ext.getBody(),
   5:     layout : {
   6:         type : 'vbox',
   7:         align : 'stretch'
   8:     },
   9:     items : [{
  10:         xtype : 'tabpanel',
  11:         itemId : 'mainTabPanel',
  12:         flex : 1,
  13:         items : [{
  14:                     xtype : 'panel',
  15:                     title : 'Users',
  16:                     id : 'userPanel',
  17:                     layout : {
  18:                         type : 'vbox',
  19:                         align : 'stretch'
  20:                     },
  21:                     tbar : [{
  22:                                 xtype : 'button',
  23:                                 text : 'Edit',
  24:                                 itemId : 'editButton'
  25:                             }],
  26:                     items : [{
  27:                                 xtype : 'form',
  28:                                 border : 0,
  29:                                 items : [{
  30:                                             xtype : 'textfield',
  31:                                             fieldLabel : 'Name',
  32:                                             allowBlank : false
  33:                                         }, {
  34:                                             xtype : 'textfield',
  35:                                             fieldLabel : 'Email',
  36:                                             allowBlank : false
  37:                                         }],
  38:                                 buttons : [{
  39:                                             xtype : 'button',
  40:                                             text : 'Save',
  41:                                             action : 'saveUser'
  42:                                         }]
  43:                             }, {
  44:                                 xtype : 'grid',
  45:                                 flex : 1,
  46:                                 border : 0,
  47:                                 columns : [{
  48:                                             header : 'Name',
  49:                                             dataIndex : 'Name',
  50:                                             flex : 1
  51:                                         }, {
  52:                                             header : 'Email',
  53:                                             dataIndex : 'Email'
  54:                                         }],
  55:                                 store : Ext.create('Ext.data.Store', {
  56:                                             fields : [],
  57:                                             data : [{
  58:                                                         Name : 'Joe Blogs',
  59:                                                         Email : 'joe@example.com'
  60:                                                     }, {
  61:                                                         Name : 'Jane Doe',
  62:                                                         Email : 'jane@example.com'
  63:                                                     }]
  64:                                         })
  65:                             }]
  66:                 }]
  67:     }, {
  68:         xtype : 'conponent',
  69:         itemId : 'footerComponent',
  70:         html : 'Footer Information',
  71:         extraOptions : {
  72:             option1 : 'test',
  73:             option2 : 'test'
  74:         },
  75:         height : 40
  76:     }]
  77: });

如何去做


Ext.ComponentQuery類的主方法是query()。它接收一個CSS/XPath類型的選擇器字符串,然后返回一個匹配Ext.Component(或其子類)數組實例。

  1. 基於xtype查找組件: var panels = Ext.ComponentQuery.query('panel');
  2. 查找二級xtype:var buttons = Ext.ComponentQuery.query('panel button');
  3. 基於屬性值檢索組件:var saveButton = Ext.ComponentQuery.query('button[action="saveUser"]');
  4. 混合查找組件:var buttonsAndTextfields = Ext.ComonentQuery.query('button, textfield');
  5. 基於ID查找組件:var usersPanel = Ext.ComponentQuery.query('#usersPanel');
  6. 基於組件的presence(不太明白這個應該怎么翻譯):var extraOptionsComponents = Ext.ComponentQuery.query('component[extraOptions]');
  7. 使用組件的成員方法查找:var validField = Ext.ComponentQuery.query('form > textfield{isValid()}');

   1: var panels = Ext.ComponentQuery.query('panel');
   2: var buttons = Ext.ComponentQuery.query('panel button'); 
   3: var saveButton = Ext.ComponentQuery.query('button[action="saveUser"]');
   4: var buttonsAndTextfields = Ext.ComonentQuery.query('button, textfield');
   5: var usersPanel = Ext.ComponentQuery.query('#usersPanel');
   6: var extraOptionsComponents = Ext.ComponentQuery.query('component[extraOptions]');
   7: var validField = Ext.ComponentQuery.query('form > textfield{isValid()}');


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM