如何将空项目添加到 ExtJS 组合框? [英] How to add an empty item to ExtJS combobox?

查看:26
本文介绍了如何将空项目添加到 ExtJS 组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向 Ext.form.ComboBox 添加和空项目(显示值为空白,项目高度保持正常).我参考了下面的 2 个链接来配置我的组合框,但它仍然没有显示空项目:

I want to add and empty item (display value is blank, item height is kept as normal) to an Ext.form.ComboBox. I refered 2 links below to configure my combobox, but it still not display the empty item:

这是我的代码:

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}&nbsp;</div></tpl>'
});

组合框存储的数据将在 Ajax 请求后加载(即数据项中的 3 个项).并且组合框只有 3 个项目(不是我预期的 4 个).你知道我的问题吗?!非常感谢!

The combobox store's data will be loaded after an Ajax request (i.e 3 items in data items). And the combobox has only 3 item (not 4 as I expected). Do you have any idea about my problem?! Thank you so much!

推荐答案

既然你后来添加了组合框值,为什么不直接用一个空值初始化商店:

Since your adding the combobox values later, why not just initialize the store with one blank value:

store : new Ext.data.JsonStore({
    fields : ['id', 'fullName'],
    data : [{id: 0, fullName: ''}]
}),

稍后当你执行 store.add(theRestOfThem) 时,那个空白的仍然会存在.

Later when you do store.add(theRestOfThem), that blank one will still be there.

今天(2017 年 4 月 15 日)不得不为 ExtJS 4.2 重新审视这个:

最简单的方法是在 store 中包含一个空字符串,如上所示,也可以通过 store 上的加载侦听器来完成:

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:

listeners: 
{
    load: function(store, records) 
    {
        store.insert(0, [{
            fullName: '',
            id: null
        }]);
    }
}

然后在显示字段后使用 &nbsp;tpl 配置添加到组合框:

Then add a tpl config to the combobox with &nbsp; after the display field:

tpl: '<tpl for="."><div class="x-boundlist-item">{fullName}&nbsp;</div></tpl>',

(OPs案例中显示字段为fullName)

(the display field is fullName in the OPs case)

这篇关于如何将空项目添加到 ExtJS 组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆