PDA

View Full Version : ComboBox : display 'displayField' instead of 'valueField'



PacPac
4 May 2009, 7:18 AM
Hi all,

I have created an editor grid with cells that contain ComboBox.

However, I would like to display the 'displayField' of my ComboBox and to send to the server the 'valueField'.

I have a problem : the 'displayField' is displayed to the user only when editing the grid. The 'valueField' is always displayed when the cell is not selected. But I would like to always display the 'displayField'.

Can you help me ?

Thanks.

syscobra
4 May 2009, 7:29 AM
use a custom render on the cell in the grid.
in any part of the js file...

...
function yourCustomRender(value, p, record){
return String.format(
'{0}',
record.yourDisplayField); //change yourDisplayField to the displayField of your record definition
}
...
in your grid column...

...
renderer:yourCustomRenderer;
...

PacPac
4 May 2009, 8:39 AM
Thanks for your answer.

I think this would be perfect.

However, I tried to extract my displayField from the record in the function yourCustomRenderer, and it is impossible to get the field.

record.get("myField") or record.myField or any methods, it is not working.

If you see the problem, it would be great. But I'm going to investigate.

syscobra
4 May 2009, 9:15 AM
The displayField must be in the record data of the Store...
If it is not there you must put in there (in server side). Just don't put it too in the column model of the grid. It must be in the xml or Json you provide from the server (the valuefield and the displayfield).
If its in another store (as you have it on the combobox), you have to find the displayField from the store using a function with the record.valueField. (In the renderer function)

...
function yourCustomRender(value, p, record){
//Get the record from the other store remember to load it before rendering...
indexOfYourFieldInStore = yourComboListStore.find('yourvaluefield',value);
yourDisplayField = yourComboListStore.getAt(indexOfYourFieldInStore).yourDisplayField;
return String.format(
'{0}',
yourDisplayField); //change yourDisplayField to the displayField of your record definition
}
...

Something like that, i can't test it but that can give you an idea...

bt_bruno
4 May 2009, 11:48 AM
Didn't understand the use of String.format instead of a simple return...

this:

return String.format('{0}', theValue);

instead of:

return theValue;

syscobra
4 May 2009, 11:59 AM
you are right lol.
you can do that.
I just copied from a renderer i had that has some values. (for giving a quick not too much time spend response)

It is useful only when you have to return more than 1 value in an string format.

(maybe that way prevent code injection too)

PacPac
5 May 2009, 1:47 AM
Thanks a lot, it works ! ;)