如何向 ExtJS 组合框添加一个空项目?
我想向 Ext.form.ComboBox 添加和清空项目(显示值为空白,项目高度保持正常)。我引用了下面的 2 个链接来配置我的组合框,但它仍然不显示空项目:
- http://www.ashlux.com/wordpress/2009/09/04/handling-empty-options-with-ext-js-combo-box/
- http://www.sencha.com/forum/showthread.php?52340-How-to-add-a-quot-blank-quot-entry-to-a-ComboBox
这里是我的代码:
this.abcCombo = new Ext.form.ComboBox({
name : 'abcCombo',
hiddenName : 'abcCombo-id',
fieldLabel : "My Combobox",
width : 250,
editable : false,
forceSelection : true,
mode : 'local',
triggerAction : 'all',
valueField : 'id',
displayField : 'fullName',
store : new Ext.data.JsonStore({
fields : ['id', 'fullName']
}),
tpl : '<tpl for="."><div class="x-combo-list-item">{fullName} </div></tpl>'
});
组合框存储的数据将在 Ajax 请求后加载(即数据项中的 3 项)。并且组合框只有 3 个项目(不是我预期的 4 个)。 你对我的问题有什么想法吗?! 太感谢了!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在开头添加一条“空”记录:
You can add an "empty" record at the beginning:
既然您稍后添加组合框值,为什么不直接使用一个空白值来初始化存储:
稍后当您执行
store.add(theRestOfThem)
时,该空白值仍将存在。今天(2017 年 4 月 15 日)必须重新审视 ExtJS 4.2:
最简单的方法是在存储中包含一个空字符串,如上所示,也可以在存储中使用加载侦听器来完成:
然后在显示字段后面添加一个
:(
tpl
配置到组合框,并使用显示字段是 OP 中的
fullName
案件)Since your adding the combobox values later, why not just initialize the store with one blank value:
Later when you do
store.add(theRestOfThem)
, that blank one will still be there.Had to revisit this today (15 Apr 2017) for ExtJS 4.2:
The easiest way is to include an empty string in the store as above, it can also be done with a load listener on the store:
Then add a
after the display field:
tpl
config to the combobox with(the display field is
fullName
in the OPs case)});
在初始化组件时,您可以(在控制器上)执行以下操作:
在填充函数上:
});
on initializing the component, you can do this (on controller):
on the populate function:
这就是我们如何在组合框中添加空白字段
在java Map或任何其他集合中放置像这样的键值
在js文件中,它应该像这样:
组合框的监听器
This is how we can add a blank field in Combo box
In java Map or any other collection put key value like this
In js file, it should be like this :
Listener for combo box
在 Ext 4.2.1(可能是其他版本)中,只需添加到组合框配置中:
In Ext 4.2.1 (probably others), just add to combobox config:
如果商店使用内联数据,则
store.load
甚至不会触发。也许有更好的解决方案,但我最终在combobox.render
上插入商店记录:If the store uses inline data then
store.load
even won't fire. Maybe there is a better solution, but I ended up inserting store records oncombobox.render
:这对我有用,我尝试将
' '
放入我设置为模型上组合框的显示字段的字段中,我想在该模型中显示空白组合框的下拉菜单但不在文本框中,选择它后会显示我不想要的' '
。因此,我在组合框中使用了 displayTpl 配置,它会影响在组合中选择某些内容后显示的文本框。这是我添加到附加到组合框的商店中的模型,并且显示字段是 contactName。
这是我的组合框上的 displayTpl 配置
这可能可以使用 if 语句而不是我所做的 switch 来完成,但老实说我从未使用过 XTemplates 并且我不知道我在做什么我也有switch 语句的示例并使其正常工作。我基本上只是说如果 contactName =
' '
则不要放置任何内容,如果有其他内容则显示 contactName。This is what worked for me I tried putting
' '
into the field that I had set as the display field for the combo box on the model that I wanted to show up blank that worked in the drop down of the combo box but not in the text box after it's been selected it would show' '
which I didn't want. So, I used the displayTpl config on my combo box which affects the text box shown after selecting something in a combo.This is my model that I added to the store attached to the combo box and the displayField is contactName.
This is the displayTpl config on my combobox
This probably could be done with an if statement instead of the switch thing I did but I'll be honest I have never worked with XTemplates and I don't know what I'm doing also I had an example of a switch statement and got it to work so. I'm basically just saying if the contactName =
' '
then don't put anything and if its anything else display the contactName.