官术网_书友最值得收藏!

  • Ext JS 3.0 Cookbook
  • Jorge Ramon
  • 324字
  • 2021-04-01 13:43:43

Acquiring references to Ext JS components

As Ext JS is all about working with components, it's essential to learn how to acquire a reference to any component in your code. For example, this recipe shows how easy it is to reference a ComboBox component.

How to do it...

You can reference a ComboBox component as shown in the following code:

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" 
href="../ext/css/ext-all.css"/>
<script type="text/javascript" src="../ext/ext-base.js"></script>
<script type="text/javascript" 
src="../ext/ext-all-debug.js"></script>
<script type="text/javascript"> 
Ext.BLANK_IMAGE_URL = '../ext/images/default/s.gif';
Ext.onReady(function() {
var colorsStore = new Ext.data.SimpleStore({
fields: ['name'],
data: [['Blue'],['Red'],['White']]
});
var combo = new Ext.form.ComboBox({
store: colorsStore,
displayField: 'name',
typeAhead: true,
mode: 'local',
forceSelection: true,
triggerAction: 'all',
emptyText: 'Select a color...',
selectOnFocus: true,
applyTo: 'colors-combo',
id: 'colors-combo'
});
// Get a reference to the combobox using Ext.getCmp(id).
var combo = Ext.getCmp("colors-combo");
// Using the reference to the combo, add a handler to the
//'select' event.
combo.on('select', function() {
Ext.Msg.alert('Using Ext.getCmp(id)', 
The selected color is ' + combo.getValue();
});
});
</script>
</head>
<body>
<input type="text" id="colors-combo"/>
</body>
</html>

How it works...

References to components are obtained using the Ext.getCmp(id) function, where id is the ID of the component. Keeping track of components is possible, thanks to the ComponentMgr class. It provides for easy registration, un-registration and retrieval, as well as notifications when components are added or removed.

There's more...

This method is particularly useful when explicit component references do not already exist in your code, for example when components are defined as part of the items collection of a container. (Think of a tab panel and its tabs, or a border layout and its contained panels.)

There are other DOM and component utilities provided by Ext JS:

  • Ext.getBody() returns the body of the document as an Ext.Element
  • Ext.getDoc() returns the current HTML document as an Ext.Element
  • Ext.getDom(id) returns the DOM node for the supplied ID, DOM node, or element

See also...

  • The Retrieving DOM nodes and elements recipe, covered earlier in this chapter, explains how to get a handle on any DOM element.
  • The next recipe, Running high-performance DOM queries, teaches you about running queries against the DOM.
主站蜘蛛池模板: 凌海市| 凤城市| 曲水县| 灵丘县| 乐清市| 榆社县| 利川市| 岫岩| 安义县| 新乐市| 白玉县| 南阳市| 房山区| 金寨县| 张家界市| 嘉荫县| 专栏| 长海县| 叶城县| 东兴市| 漾濞| 弥渡县| 绥宁县| 泰来县| 英山县| 文安县| 宿松县| 张北县| 平阳县| 朝阳区| 娱乐| 南陵县| 格尔木市| 乌拉特前旗| 峡江县| 清苑县| 时尚| 江北区| 德清县| 鄄城县| 茌平县|