ExtJS中get、getDom、getCmp、getBody、getDoc使用 javascript

Ext中包含了几个以get开头的方法,这些方法可以用来得到文档中DOM、得到当前文档中的组件、得到Ext元素等,在使用中要注意区别使用。

1、get方法
get方法用来得到一个Ext元素,也就是类型为Ext.Element的对象, Ext.Element类是Ext对DOM的封装,代表DOM的元素,可以为每一个DOM创建一个对应的Element对象,可以通过Element对象上的方法来实现对DOM指定的操作,比如用hide方法可以隐藏元素、initDD方法可以让指定的DOM具有拖放特性等。get方法其实是 Ext.Element.get的简写形式。
get方法中只有一个参数,这个参数是混合参数,可以是DOM节点的id、也可以是一个Element、或者是一个DOM节点对象等。
代码:
<div id="EXTJS">aaa</div>
Ext.onReady(function(){
       var e=new Ext.Element("EXTJS");
       alert(Ext.get("EXTJS"));
       alert(Ext.get(document.getElementById("EXTJS")));
       alert(Ext.get(e));
}); 三个方法都可以得到一个与DOM节点 EXTJS对应的Ext元素。

2、getCmp方法-获得Ext组件。
getCmp方法用来获得一个Ext组件,也就是一个已经在页面中初始化了的Component或其子类的对象,getCmp方法其实是Ext.ComponentMgr.get方法的简写形式。getCmp方法中只有一个参数,也就是组件的id。
代码:
<div id="EXTJS">aaa</div>
Ext.onReady(function(){
       var h=new Ext.Panel({
            id:"h2",
            title:"",
            renderTo:"EXTJS",
            width:300,
            height:200
       });
       Ext.getCmp("h2").setTitle("新的标题");

});在代码中,我们使用Ext.getCmp("h2").来得到id为h2的组件,并调用其setTitle方法来设置该面板的标题。

3、getDom方法-获得DOM节点
getDom方法能够得到文档中的DOM节点,该方法中包含一个参数,该参数可以是DOM节点的id、DOM节点对象或DOM节点对应的Ext元素(Element)等。
代码:
<div id="EXTJS">tttt</div>
Ext.onReady(function(){
       var e=new Ext.Element("EXTJS");
       Ext.getDom("EXTJS");
       Ext.getDom(e);
       Ext.getDom(e.dom);
});三个语句返回都是同一个DOM节点对象。

4、getBody方法-得到文档的body节点元素(Element)。
该方法直接得到文档中与document.body这个DOM节点对应的ExtJS元素(Element),实质就是把document.body对象封装成ExtJS元素对象返回,该方法不带任何参数。比如下面的代码把面板h直接渲染到文档的body元素中。
Ext.onReady(function(){
       var h=new Ext.Panel({title:"测试",width:300,height:200});
       h.render(Ext.getBody());
});

5、getDoc方法-获得与document对应的Ext元素(Element)
getDoc方法实质上就是得到当前html文档对象,也就是把document对象封装成ExtJS的Element对象返回,该方法不带任何参数,即:
getDoc = function(){
    return Ext.get(document);
}

 

================================我是分隔线==================================

原文地址:http://hi.baidu.com/_ollie/blog/item/3739c55210185f060df3e375.html

以下是一个使用ExtJS 6实现模糊查询的完整例子: 1. 创建一个包含表格和搜索框的页面: ```javascript Ext.create('Ext.panel.Panel', { title: 'Search Example', width: 500, height: 400, layout: 'border', items: [{ region: 'center', xtype: 'grid', id: 'dataGrid', columns: [{ text: 'Name', dataIndex: 'name', flex: 1 }, { text: 'Email', dataIndex: 'email', flex: 1 }, { text: 'Phone', dataIndex: 'phone', flex: 1 }], store: Ext.create('Ext.data.Store', { fields: ['name', 'email', 'phone'], data: [{ name: 'John', email: 'john@example.com', phone: '555-1234' }, { name: 'Jane', email: 'jane@example.com', phone: '555-5678' }, { name: 'Bob', email: 'bob@example.com', phone: '555-9012' }] }) }, { region: 'north', xtype: 'textfield', id: 'searchField', fieldLabel: 'Search', labelWidth: 50, margin: 5, listeners: { change: function(field, newValue) { var grid = Ext.getCmp('dataGrid'); var store = grid.getStore(); store.clearFilter(); if (newValue) { var matcher = new RegExp(Ext.String.escapeRegex(newValue), 'i'); store.filter({ filterFn: function(record) { return matcher.test(record.get('name')) || matcher.test(record.get('email')) || matcher.test(record.get('phone')); } }); } } } }], renderTo: Ext.getBody() }); ``` 2. 在搜索框的change事件监听器添加代码,以便根据输入的文本过滤表格的数据。使用正则表达式进行模糊匹配。如果搜索框为空,则清除所有过滤器。 3. 运行代码并输入搜索条件,可以看到表格只显示与搜索条件匹配的记录。 完整的代码如下: ```javascript Ext.create('Ext.panel.Panel', { title: 'Search Example', width: 500, height: 400, layout: 'border', items: [{ region: 'center', xtype: 'grid', id: 'dataGrid', columns: [{ text: 'Name', dataIndex: 'name', flex: 1 }, { text: 'Email', dataIndex: 'email', flex: 1 }, { text: 'Phone', dataIndex: 'phone', flex: 1 }], store: Ext.create('Ext.data.Store', { fields: ['name', 'email', 'phone'], data: [{ name: 'John', email: 'john@example.com', phone: '555-1234' }, { name: 'Jane', email: 'jane@example.com', phone: '555-5678' }, { name: 'Bob', email: 'bob@example.com', phone: '555-9012' }] }) }, { region: 'north', xtype: 'textfield', id: 'searchField', fieldLabel: 'Search', labelWidth: 50, margin: 5, listeners: { change: function(field, newValue) { var grid = Ext.getCmp('dataGrid'); var store = grid.getStore(); store.clearFilter(); if (newValue) { var matcher = new RegExp(Ext.String.escapeRegex(newValue), 'i'); store.filter({ filterFn: function(record) { return matcher.test(record.get('name')) || matcher.test(record.get('email')) || matcher.test(record.get('phone')); } }); } } } }], renderTo: Ext.getBody() }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值