PDA

View Full Version : Add a dynamic item into a combobox problem



kronox
16 Nov 2007, 10:34 AM
I have searched through the forums here and have founds some information regarding my issue, but yet to find any definite solution.

I have a combo box which is populated from an xml file converted to an array, everything loads fine and the listeners work well (on initial load). I can alter the store to add a new record to the store, and even have the combobox display the new item.

I am adding the new item at the bottom of the list, and when I select it, the combobox will select the item above it and not the new item.

Here is the code for the combobox


SelectCombo = new Ext.form.ComboBox({
cid: 'clientCombo',
fieldLabel: 'Client',
name: 'clientSelect',
store: new Ext.data.SimpleStore({
fields:['id', 'name'],
data: clientArray
}),
valueField: 'id',
autoShow: true,
displayField: 'name',
mode: 'local',
triggerAction: 'all',
listeners:{select:{fn:function(combo, value) {
var index = clientStore.find('id', combo.getValue());
alert(combo.getValue());
selectedClient = clientStore.getAt(index);
buildSelectedArray();
btnEditClient.enable();
btnDelClient.enable();
refreshClientProperties();
}}
}
});

and the code for adding the new record to the store and adding the new item to the combobox.


function updateClientSelect() {
rebuildArray(); // rebuilds the array with the new content
var info = clientArray[clientArray.length - 1];
clientSelectCombo.store.insert(info[0],new Ext.data.Record({id: info[0], name: info[1]}));
} /* end function updateClientSelect */

Any ideas on how to get this to work?

and just so as not to leave out anything, the code for the new record and the buildarray


function buildClientArray() {
clientArray = new Array();
for(var i = 0; i < clientStore.getCount(); i++) {
var info = clientStore.getAt(i);
clientArray[i] = [ info.data['id'],info.data['name']];
} /* end for */
} /* end function buildClientarray */

function addClient(form) {
var fp = form.find('cid', 'acf')[0].getForm().getValues();
fp['id'] = clientStore.getCount();
var newRecord = Ext.data.Record.create([
{name: 'id'},{name: 'name'},{name: 'company'},{name: 'url'},{name: 'registered'},
{name: 'contact'},{name: 'address'},{name: 'city'},{name: 'province'},{name: 'country'},
{name: 'postalCode'},{name: 'phone'},{name: 'cell'},{name: 'fax'},{name: 'email'}
]);

var myNewRecord = new newRecord({
id: fp['id'],name: fp['name'],company: fp['company'],url: fp['url'],
registered: fp['registered'],contact: fp['contact'],address: fp['address'],
city: fp['city'],province: fp['province'],country: fp['country'],
postalCode: fp['postalCode'],phone: fp['phone'],cell: fp['cell'],
fax: fp['fax'],email: fp['email']
});

clientStore.insert(fp['id'], myNewRecord);
clientStore.commitChanges();
updateClientSelect();
} /* end function addClient */

kronox
16 Nov 2007, 11:10 AM
Solved. Ended up being the insertion number I was using, I forgot to inc the getCount by one