PDA

View Full Version : 4.1.1 - setValue on Combobox (with valueField) works only if forceSelection is false



Dafram
20 Jul 2012, 4:13 AM
Hi all!

I have a problem with ExtJs 4.1.1 and a Combobox with "displayField" param different from "valueField" param and with forceSelection:true.

Here is my code:



Ext.define('modelCaratteristica', {
extend: 'Ext.data.Model',
fields: [
{name: 'idcara', type: 'int'},
{name: 'cadesc', type: 'string'}
]
});

var storeCaratteristica = Ext.create('Ext.data.Store', {
model: 'modelCaratteristica',
proxy: {
type: 'ajax',
url : 'storeCaratteristica.php',
reader: {
type: 'json'
}
},
autoLoad: true
});

var cbCaratteristica1 = Ext.create('Ext.form.field.ComboBox', {
name: 'cbCaratteristica1',
id: 'id_cbCaratteristica1',
fieldLabel: 'Se Caratteristica',
displayField: 'cadesc',
valueField: 'idcara',
width: 750,
labelWidth: 100,
store: storeCaratteristica,
queryMode: 'local',
allowBlank: false,
forceSelection: true
});


My JSON store returns a JSON array like this:



[{"idcara":"1","cadesc":"AAAA"},{"idcara":"2","cadesc":"BBBB"},{"idcara":"3","cadesc":"CCCC"}]


Now, I have displayField and valueField separated because I need to display description and to save to database the id.

Now, if I make...



cbCaratteristica1.setValue(rec.idcara1); // where rec.idcara1 is numeric (for example "3")


... it only works (set combobox value to "CCCC") if my combobox has forceSelection: false.

But I need forceSelection: true to avoid users to insert bad values. :-(

PS: I have the same problem with ExtJs 4.0.7

Ex_Soft
20 Jul 2012, 4:43 AM
Try to use Ext.form.field.ComboBox.reset() before (http://www.sencha.com/forum/showthread.php?230993-Ext.form.field.ComboBox.setValue%28%29-doesn-t-work-after-input-of-non-existent-value) Ext.form.field.ComboBox.setValue()

Dafram
20 Jul 2012, 4:58 AM
Try to use Ext.form.field.ComboBox.reset() before (http://www.sencha.com/forum/showthread.php?230993-Ext.form.field.ComboBox.setValue%28%29-doesn-t-work-after-input-of-non-existent-value) Ext.form.field.ComboBox.setValue()
Unfortunately it doesn't work :-(

Ex_Soft
20 Jul 2012, 5:06 AM
Unfortunately it doesn't work
This (http://jsfiddle.net/5BJ4K/1/) works fine.

Dafram
20 Jul 2012, 7:06 AM
This (http://jsfiddle.net/5BJ4K/1/) works fine.
Uhm... I see.

Here I have my example: http://www.electricblueskies.com/smeraldo/es01.html

It shows the difference between forceSelection = true and forceSelection = false

PS: perhaps a delay problem? If I put an alert before the setValue instruction, sometimes, it works...

scottmartin
20 Jul 2012, 7:35 AM
I do not see reset() in your code as mention by Ex_Soft.

Scott.

Dafram
20 Jul 2012, 7:50 AM
I do not see reset() in your code as mention by Ex_Soft.

Scott.
Updated my code with reset(), but the same thing...

I think it is a delay problem, because after the window is opened, the instruction setValue() works:

http://www.electricblueskies.com/smeraldo/es02.html (see the button in the window)

But... how can I solve my problem?

scottmartin
21 Jul 2012, 10:57 AM
This is a timing issue. In the forceSelection, you are required to find a value in the store. In the other instance you are not.

You making an ajax call. It is most likely the incoming results are causing havoc.


type: 'ajax',
url : 'storeCaratteristica.php',
reader: {
type: 'json'
}


You can either load the table before hand, or make the update upon success of the ajax call.

Scott.

Dafram
22 Jul 2012, 4:38 AM
Thanks

Dafram
23 Jul 2012, 12:05 AM
I have solved with this:


storeCaratteristica.on("load",onLoadCbCaratteristica);

function onLoadCbCaratteristica()
{
var_caratteristica = 1;
cbCaratteristica.reset();
cbCaratteristica.setValue(var_caratteristica);
}

Example: http://www.electricblueskies.com/smeraldo/es02.html

But if in the form I have more than 1 store to wait to load, the problem returns...

ExTriqui
21 Jan 2013, 4:24 AM
I've found that adding
loading: true to the store's config when autoLoad is also true, fix this issue with comboboxes, and it doesn't seem to have any other side effect.

Still I think this should be fixed, and my best guess is that there is a line in the setValue function that should be moved:
Ext.define('Ext.form.field.ComboBox', {
...
setValue: function(value, doSelect) {
...
for (i = 0, len = value.length; i < len; i++) {
record = value[i];
if (!record || !record.isModel) {
record = me.findRecordByValue(record);
}

if (record) {
matchedRecords.push(record);
displayTplData.push(record.data);
processedValue.push(record.get(me.valueField));
}


else {


processedValue.push(value[i]); // ADDED
if (!me.forceSelection) {
// processedValue.push(value[i]); // REMOVED
dataObj = {};
dataObj[me.displayField] = value[i];
displayTplData.push(dataObj);

}

else if (Ext.isDefined(valueNotFoundText)) {
displayTplData.push(valueNotFoundText);
}
}
}
...
}
});