19

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

这是我的代码:

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 个)。你对我的问题有任何想法吗?!非常感谢!

4

6 回答 6

14

您可以在开头添加“空”记录:

 listeners: {
     load: function(store, records) {
          store.insert(0, [{
              fullName: '&nbsp;',
              id: null
          }]);
     }
  }
于 2013-04-06T16:49:27.173 回答
11

既然您稍后添加了组合框值,为什么不使用一个空白值初始化存储:

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

稍后当你这样做时store.add(theRestOfThem),那个空白的仍然会在那里。

今天(2017 年 4 月 15 日)必须重新访问 ExtJS 4.2:

最简单的方法是在上面的 store 中包含一个空字符串,也可以使用 store 上的负载侦听器来完成:

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

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

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

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

于 2012-02-22T17:47:06.827 回答
3

这就是我们如何在组合框中添加一个空白字段

在 java Map 或任何其他集合中,像这样放置键值

fuelMap.put("","&nbsp;"); // we need to add "&nbsp;" not ""," " or null 
                          // because these will add a fine blank line in Combobox 
                          // which will be hardly noticeable.

js 文件中,它应该是这样的:

组合框的监听器

listeners: {
    select: function (comp, record, index) {
        if (comp.getValue() === "" || comp.getValue() === "&nbsp;") {
            comp.setValue(null);
        }
    }
}
于 2012-12-06T20:19:10.197 回答
3
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>'

    //make sure to add this
    //if not added, empty item height is very small
    listConfig : {
        getInnerTpl: function () {
            return '<table><tr><td height="12">{fullName}</td></tr></table>';
        }
    }

});

在初始化组件时,您可以这样做(在控制器上):

populateMyComboBox([yourComboBoxComponent], true);

关于填充功能:

populateMyComboBox : function(comboBox, insertEmpty) {
    var list;
    if (insertEmpty) {
        list.push({id : 0, fullName : ''});
    }

    var mStore = Ext.create('Ext.data.Store', {
        fields: ['data', 'label'],
        data : list.concat([real_data])
    }),

    comboBox.bindStore(mStore);
}
于 2013-05-23T04:30:11.147 回答
2

在 Ext 4.2.1(可能是其他版本)中,只需添加到组合框配置:

tpl : '<tpl for="."><div class="x-boundlist-item">{fullName}&nbsp;</div></tpl>'
于 2016-02-10T00:05:52.423 回答
1

如果商店使用内联数据,那么store.load甚至不会触发。也许有更好的解决方案,但我最终在以下位置插入了商店记录combobox.render

{
    xtype: 'combo',
    displayField: 'name',
    valueField: 'value',
    store: {
        type: 'MyInlineStore',
    },
    listeners: {
        render: function(el){
            el.getStore().insert(0, [{name: '[Any]', value: ''}]);
            el.setValue(''); //select [Any] by default
        }
    },
}
于 2017-05-08T22:59:36.903 回答