PDA

View Full Version : customize ComboBox displayField rendering



Grandiosa
29 May 2008, 7:06 AM
I want my combo-box to display a concatenation of two model attributes (like Id - Name )

I removed the setDisplayField call and tried this:


sectorCombo.setModelStringProvider(new ModelStringProvider<PriceSector>() {
public String getStringValue(PriceSector model, String property) {
return model.getId() + " - " + model.getName();
}
});
but it doesn't work, combobox is empty.

Do I have to use a ModelPropertyEditor for this? If yes, can you give an example please..

darrellmeyer
3 Jun 2008, 4:12 AM
ModelStringProvider is not working in ComboBox. The fix will be available soon.

Grandiosa
17 Jun 2008, 9:25 AM
Any news on this Darrel? Very often combos need to show an aggregation of two model fields, typically and id and a some string value.

darrellmeyer
18 Jun 2008, 12:46 PM
Sorry for the delay. It took a little while to figure out how to add this feature. You can now set a string provider on Template. Here is what you can do with the latest code:


public void onModuleLoad() {
ListStore<Stock> store = new ListStore<Stock>();
store.add(TestData.getStocks());

ComboBox test = new ComboBox();
test.setWidth(225);
test.setDisplayField("name");
test.setStore(store);

Template t = test.getTemplate();
t.setStringProvider(new ModelStringProvider<Stock>() {
public String getStringValue(Stock model, String property) {
if (property.equals("name")) {
return model.getName() + " " + model.getSymbol();
}
return null;
}
});
RootPanel.get().add(test);
}Let me know if that works for you.

Grandiosa
19 Jun 2008, 5:26 AM
I tried it and it almost worked.

The aggregated strings display properly in the dropdown list, but after a selection only the displayfield is shown in the text field. I reckon this is because the string provider is not used to display the content of the field ?

Also, since this kind of behaviour is so common for a combobox, why not add this functionality into the ComboBox class. I really just want to do like this:

combo.setDisplayFields("name", "age");
combo.setDisplayFieldsSeparatorString(" - ");

Alternatively, if the display field was not set for the combo it could fallback to use the toString() method of the Model object.

Would be nice not have to deal with a template and implement a provider for this.

Thanks for the help.

sdc
19 Jun 2008, 8:52 AM
Hi all,

What I find enhanceable is not the fact that you have to implement to provider but the fact you have to manage with a string property. Something like this would be nicer, I believe :

t.setDisplayProvider(new ModelDisplayProvider<Stock>() {
public String getDisplayValue(Stock model) {
return model.getName() + " " + model.getSymbol();
}
});

Grandiosa
19 Jun 2008, 10:40 PM
Yep, I'd vote for that...

sdc
10 Sep 2008, 12:25 AM
I've opened a feature request (http://extjs.com/forum/showthread.php?t=46670).