PDA

View Full Version : Add records to combobox



ShatyUT
3 Feb 2010, 11:58 AM
I hope this specific question hasn't already been answered...I searched and searched.

I have a grid and an empty combobox. When I select something in the grid, I want that record added to the combobox. My code doesn't seem to be doing it. Here's how I'm trying with the key line in bold:



{
fieldLabel : 'Responses',
xtype: 'grid',
id: 'responsesgrid',
hideHeaders: true,
store: new Ext.data.ArrayStore({
fields: [{name:'id'},{name:'response'}],
data: [[1,'YES'],[2,'NO'],[3,'WAIVED'],[4,'NOT REQUIRED']]
}),
sm: new Ext.grid.RowSelectionModel({
listeners: {
rowselect: function(sm, index, record) {
Ext.getCmp('comboDefaultResponse').getStore().add(new Array(record.copy()));
},
rowdeselect: function(sm, index, record) {
Ext.getCmp('comboDefaultResponse').getStore().remove(record.copy());
}
}
}),
cls: 'responseRow',
stripeRows: true,
height: 60,
columns: [{id:'response',dataIndex:'response'}],
autoExpandColumn: 'response'
},{
fieldLabel: 'Default Response',
xtype: 'combo',
id: 'comboDefaultResponse',
store: new Ext.data.ArrayStore({
fields: [{name:'id'},{name:'response'}]
}),
valueField: 'id',
displayField: 'response'
}


Do I have to reload the data store of the combobox? It is my understanding that ArrayStore::reload() is used when a proxy or url is specified so it wouldn't work here. Am I wrong?

tobiu
3 Feb 2010, 1:10 PM
just out of curiosity: why don't you just add / remove the record instead of copying it?

like:



Ext.getCmp('comboDefaultResponse').getStore().add(record);


kind regards,
tobiu

ShatyUT
3 Feb 2010, 1:21 PM
just out of curiosity: why don't you just add / remove the record instead of copying it?

Well, I don't have a good reason other than, I wasn't sure whether it would effectively be the same object in both components so if I deselect a row in the grid which causes the record to be removed from the combobox, would it also remove the record from the grid?

I'm getting more and more familiar with this library but a lot of the behind the scenes stuff is still a black box to me.

tobiu
3 Feb 2010, 1:34 PM
as long as the combo does not use the same store, you can add / remove items without any effects to the other component.

since the add-method ( http://www.extjs.com/deploy/ext-3.0.0/docs/source/Store.html#method-Ext.data.Store-add ) uses



records = [].concat(records);


you can add single records without wrapping them into an array.
so, i would try out:



Ext.getCmp('comboDefaultResponse').getStore().add(record);


and



Ext.getCmp('comboDefaultResponse').getStore().remove(record);


kind regards,
tobiu

ShatyUT
3 Feb 2010, 1:43 PM
Thanks for the tip. I'm pretty sure I did it that way the first time but I switched it but still no options available after clicking a row in the grid and then clicking the combobox.

The docs say that add() takes an array of records while they say remove() takes either a record or an array of records. I didn't understand why they didn't do both the same way but that's why I was wrapping the record in an array when adding.

tobiu
3 Feb 2010, 2:00 PM
use



triggerAction : 'all',
mode:'local',


in your combo and say thanks ;)

though i wonder what sense it makes to use combos, that can contain only one element (at least in this example).

kind regards,
tobiu

ShatyUT
3 Feb 2010, 2:09 PM
DOH! That triggerAction config option tripped me up once before. I should've caught that. Thanks! That did the trick.

The grid actually allows multiple selections so each row selected will add to the drop down. The drop down as you may have guessed, will allow the user to choose one of those as a default.

Thanks again for the help!