Shouldn't be a way of setting the value attribute to an option in combox/selects?

Added by Miguel Revilla 10 months ago

When you create a WComboBox you add items (options in the HTML world) but doesn't seem to be a way of setting the "value" attribute for it. It would be very helpful to have it, specially when you use different translations to an app and there isn't an easy (direct) way to translate the current index to a constant value. Imagine a combo with a list of country names and where what you want is the 2-letter ISO code.

Regards


Replies (2)

RE: Shouldn't be a way of setting the value attribute to an option in combox/selects? - Added by Koen Deforche 10 months ago

Hey Miguel,

What you are looking for is the use of different data roles. Data roles add a third dimension to rows and columns in the underlying model.

You could do for example:

 Wt::WStandardItemModel *model = new Wt::WStandardItemModel(0, 1); // because the default combo box model does not support data roles

 // in your loop
 {
   Wt::WStandardItem *item = new Wt::WStandardItem("Belgium");
   item->setData(std::string("be"), Wt::UserRole);
   model->appendRow(item);
 }

 comboBox->setModel(model);

 ...

 std::string code = comboBox->model()->data(comboBox->currentIndex(), 0, Wt::UserRole);

Instead of WStandardItemModel you can also have a more specialized model, which uses the same principle.
The default model choice for a WComboBox, a WStringListModel, is, in hindsight, a bit miserly to save a few bytes of memory, as it does not support any other roles but DisplayRole.

Regards,
koen

RE: Shouldn't be a way of setting the value attribute to an option in combox/selects? - Added by Gustavo Sooeiro 8 months ago

Miguel Revilla wrote:

When you create a WComboBox you add items (options in the HTML world) but doesn't seem to be a way of setting the "value" attribute for it. It would be very helpful to have it, specially when you use different translations to an app and there isn't an easy (direct) way to translate the current index to a constant value. Imagine a combo with a list of country names and where what you want is the 2-letter ISO code.

Regards

Hola Miguel,

Did you find an answer to your problem? I am trying to set values to the selection options, but I just can´t ...
Can you help?

Gracias,

(1-2/2)