在很多时候,combobox的选择项已经下载到本地,只是存储方式不同,我们就需要动态修改combobox的选择项。例如有一颗树,树的节点就是combobox的选项,在Ext2.0中树的存储格式不是使用store的,而combobox必须使用store,这就需要进行数据转换。本文就以此作为例子,研究一下如何动态修改combobox的选择项。
- var root = new Ext.tree.TreeNode({
- text: '选项',
- allowDrag:false,
- allowDrop:false
- });
- root.appendChild(
- new Ext.tree.TreeNode({id:1,text:'选项一',allowDrag:false}),
- new Ext.tree.TreeNode({id:2,text:'选项二',allowDrag:false}),
- new Ext.tree.TreeNode({id:3,text:'选项三',allowDrag:false}),
- new Ext.tree.TreeNode({id:4,text:'选项四',allowDrag:false})
- );
- var tree=new Ext.tree.TreePanel({
- style:'margin:20px 0 0 20px',
- title:'选项列表',
- width: 200,
- height:300,
- root:root
- })
- tree.render(document.body);
- root.expand();
代码里先创建了一个树节点root,这个将作为树的根节点,然后加入了id分别为1、2、3、4的子节点,这4个子节点就是combobox选择项,id是combobox的提交值,text是显示值。
- var combo=new Ext.form.ComboBox({
- valueField :"id",
- displayField: "text",
- store:new Ext.data.SimpleStore({
- fields: ["id", "text"],
- data: []
- }),
- mode: 'local',
- blankText:'请选择',
- emptyText:'请选择',
- hiddenName:'testhide',
- fieldLabel: '测试',
- name: 'test',
- anchor:'90%'
- })
- var simpleForm = new Ext.FormPanel({
- labelAlign: 'left',
- title: '动态修改combobox选择项例子',
- buttonAlign:'center',
- bodyStyle:'padding:5px',
- style:'margin:20px 0 0 20px;',
- width: 600,
- frame:true,
- labelWidth:80,
- items: [combo],
- buttons:[{
- text:'改变选项',
- handler:function(){
- var data=[];
- combo.clearValue();
- for(var i=0;i<root.childNodes.length;i++){
- var node=root.childNodes[i];
- data.push([node.id,node.text]);
- }
- combo.store.loadData(data);
- }
- }]
- });
- simpleForm.render(document.body);
- var data=[];
- combo.clearValue();
- for(var i=0;i<root.childNodes.length;i++){
- var node=root.childNodes[i];
- data.push([node.id,node.text]);
- }
- combo.store.loadData(data);
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <title>动态修改combobox选择项例子</title>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css">
- </head>
- <body>
- <script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
- <script type="text/javascript" src="../../ext-all.js"></script>
- <script type="text/javascript" src="../../ext-lang-zh_CN.js"></script>
- <script>
- Ext.onReady(function(){
- Ext.QuickTips.init();
- var root = new Ext.tree.TreeNode({
- text: '选项',
- allowDrag:false,
- allowDrop:false
- });
- root.appendChild(
- new Ext.tree.TreeNode({id:1,text:'选项一',allowDrag:false}),
- new Ext.tree.TreeNode({id:2,text:'选项二',allowDrag:false}),
- new Ext.tree.TreeNode({id:3,text:'选项三',allowDrag:false}),
- new Ext.tree.TreeNode({id:4,text:'选项四',allowDrag:false})
- );
- var tree=new Ext.tree.TreePanel({
- style:'margin:20px 0 0 20px',
- title:'选项列表',
- width: 200,
- height:300,
- root:root
- })
- tree.render(document.body);
- root.expand();
- var combo=new Ext.form.ComboBox({
- valueField :"id",
- displayField: "text",
- store:new Ext.data.SimpleStore({
- fields: ["id", "text"],
- data: []
- }),
- mode: 'local',
- blankText:'请选择',
- emptyText:'请选择',
- hiddenName:'testhide',
- fieldLabel: '测试',
- name: 'test',
- anchor:'90%'
- })
- var simpleForm = new Ext.FormPanel({
- labelAlign: 'left',
- title: '动态修改combobox选择项例子',
- buttonAlign:'center',
- bodyStyle:'padding:5px',
- style:'margin:20px 0 0 20px;',
- width: 600,
- frame:true,
- labelWidth:80,
- items: [combo],
- buttons:[{
- text:'改变选项',
- handler:function(){
- var data=[];
- combo.clearValue();
- for(var i=0;i<root.childNodes.length;i++){
- var node=root.childNodes[i];
- data.push([node.id,node.text]);
- }
- combo.store.loadData(data);
- }
- }]
- });
- simpleForm.render(document.body);
- });
- </script>
- </body>
- </html>