Javascript 在不修改数据存储的情况下将选项追加到combox
有没有一种方法可以在不修改基础数据存储的情况下向组合框添加选项?我的表单中有2个combobox元素。两个组合框之间的唯一区别是,其中一个组合框有一个附加的“全部”选项。例如:Javascript 在不修改数据存储的情况下将选项追加到combox,javascript,extjs,extjs4.1,extjs4.2,Javascript,Extjs,Extjs4.1,Extjs4.2,有没有一种方法可以在不修改基础数据存储的情况下向组合框添加选项?我的表单中有2个combobox元素。两个组合框之间的唯一区别是,其中一个组合框有一个附加的“全部”选项。例如: var states = Ext.create('Ext.data.Store', { fields: ['abbr', 'name'], data : [{"abbr":"AL", "name":"Alabama"}, {"abbr":"AK", "name":"Alaska"}, {
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [{"abbr":"AL", "name":"Alabama"},
{"abbr":"AK", "name":"Alaska"},
{"abbr":"AZ", "name":"Arizona"}]
});
//The first combobox should load the data as is from the store above
Ext.create('Ext.form.ComboBox', {
fieldLabel: 'Choose State',
store: states,
queryMode: 'local',
displayField: 'name',
valueField: 'abbr',
renderTo: Ext.getBody()
});
//There should be a second combobox here that is almost identical
//to the one above, just appending an additional option (that says "All")
//to the top of the list
我知道有多种方法可以改变商店,例如:
states.insert(0, [{
abbr: 'All',
name: 'All'
}])
但是,我希望在不改变存储的情况下实现这一点,而只是将选项添加到组合框本身。这可能吗
更新
我知道我可以将组合框“emptyText”配置选项设置为“All”,但这不是我想要的。我希望将“All”作为一个实际选项。Combo是一个简单的HTML选择字段。如果没有选项,则无法填充下拉列表
如果不添加到存储中,就不可能实现它。您在组合框中看到的数据来自存储
根据文件:
一个组合框控件,支持自动完成、远程加载和许多其他功能
组合框类似于传统HTML文本字段和字段的组合;用户可以在字段中自由键入,和/或从下拉选择列表中选择值。默认情况下,用户可以输入任何值,即使该值未出现在选择列表中;要阻止自由格式值并将其限制为列表中的项目,请将forceSelection设置为true
选择列表的选项从任何Ext.data.Store(包括远程存储)填充。存储区中的数据项分别通过valueField和displayField配置映射到每个选项的显示文本和支持值
我已经制作了带有“All”的组合框,我希望我做对了,现在还没有可用的源代码,但是要为其中一个组合框创建一个“All”选项,在ExtJS 6中,您必须执行以下操作:
var storeWithAll = Ext.create('Ext.data.Store',{
url:
proxy: etc.
listeners:{
load:function(store) {
// Add the "All" record after every load!
store.insert(0, [{
abbr: 'All',
name: 'All',
special:true
}]);
}
}
});
var storeWithoutAll = Ext.create('Ext.data.ChainedStore',{
// ChainedStore has the same data as the source:
source: storeWithAll,
filters:[{
// Filter away the "All" record
filterFn:function(item) {return !item.get("special");}
}]
});
Ext.create('Ext.form.field.ComboBox',{
store: storeWithAll, // Use the store with "All" record
...
});
Ext.create('Ext.form.field.ComboBox',{
store: storeWithoutAll, // Use the store without "All" record
...
});
简单的回答是,如果不创建一个新店,你就不能。