Reputation: 5430
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:
Here is my code:
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>'
});
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!
Upvotes: 19
Views: 40460
Reputation: 1
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.
var blankModel = Ext.create('myefiles.jobs.pdd.model.Contact', {
id: null,
contactName: ' ',
tel: null,
cell: null,
email: null,
vendorUUID: null,
vendorName: null,
active: null,
deleted: null
});
This is the displayTpl config on my combobox
displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<tpl switch="contactName">',
'<tpl case=" ">',
'<tpl default>{contactName}',
'</tpl>',
'</tpl>'
)
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.
Upvotes: 0
Reputation: 669
This is how we can add a blank field in Combo box
In java Map or any other collection put key value like this
fuelMap.put(""," "); // we need to add " " not ""," " or null
// because these will add a fine blank line in Combobox
// which will be hardly noticeable.
In js file, it should be like this :
Listener for combo box
listeners: {
select: function (comp, record, index) {
if (comp.getValue() === "" || comp.getValue() === " ") {
comp.setValue(null);
}
}
}
Upvotes: 3
Reputation: 111265
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 on 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
}
},
}
Upvotes: 1
Reputation: 11486
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: ''}]
}),
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:
listeners:
{
load: function(store, records)
{
store.insert(0, [{
fullName: '',
id: null
}]);
}
}
Then add a tpl
config to the combobox with
after the display field:
tpl: '<tpl for="."><div class="x-boundlist-item">{fullName} </div></tpl>',
(the display field is fullName
in the OPs case)
Upvotes: 13
Reputation: 5367
In Ext 4.2.1 (probably others), just add to combobox config:
tpl : '<tpl for="."><div class="x-boundlist-item">{fullName} </div></tpl>'
Upvotes: 2
Reputation: 3411
You can add an "empty" record at the beginning:
listeners: {
load: function(store, records) {
store.insert(0, [{
fullName: ' ',
id: null
}]);
}
}
Upvotes: 14
Reputation: 3608
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>'
//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>';
}
}
});
on initializing the component, you can do this (on controller):
populateMyComboBox([yourComboBoxComponent], true);
on the populate function:
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);
}
Upvotes: 3