Project

General

Profile

WFormModel and SelectionBox

Added by Emeric Poupon 18 days ago

I am using a WTemplateFormView / WFormModel combination with various form widgets like WComboBox, WDoubleSpinBox, etc.
I manage to retrieve the values in the model using value/valueText but I really don't understand how it is supposed to work with a WSelectionBox (multiple selections mode)?

Could you please provide a simple example?


Replies (4)

RE: WFormModel and SelectionBox - Added by Matthias Van Ceulebroeck 16 days ago

Hello Emeric,

for a WSelectionBox that has the SelectionMode::Extended enabled, there exists the WSelectionBox::selectedIndexes().
You can then use the selection's model to retrieve which items are selected (using WSelectionBox::model()), and retrieving the data from the correct row.

If you have further questions, please let me know!

Best,
Matthias

RE: WFormModel and SelectionBox - Added by Emeric Poupon 16 days ago

Thanks!

But I meant from within a WFormModel class.
Like for example in the class UserFormModel in the integration example of the wt's widget gallery (https://webtoolkit.eu/widgets/forms/integration-example), in the userData() method where value methods are called on fields.

RE: WFormModel and SelectionBox - Added by Matthias Van Ceulebroeck 16 days ago

Hey Emeric,

apologies for misreading.

The logic for this doesn't change though, you'll now only be required to ensure that your model is able to handle multiple entries of the data. You can encode this however you want. E.g. a string with a certain separator, or simply a vector of all selected elements.
That becomes the value of the model then, and you'll have to make sure the model and view can work with this data type:

e.g.

class ModelWithSelection : public WFormModel
{
  static constexpr WFormModel::Field SelectionBoxField = "SelectionBox-field";
  ...
};

class FormWithSelection : public WTemplateFormView
{
  ....
  void updateViewValue(Wt::WFormModel* model, Wt::WFormModel::Field field, Wt::WFormWidget* edit) override
  {
    if (field == ModelWithSelection::SelectionBoxField) {
      if (auto selectionBox = dynamic_cast<WSelectionBox*>(edit)) {
        // This now contains a set of selected indices
        std::set<int> indexes = model->value(ModelWithSelection::SelectionBoxField);
        selectionBox->setSelectedIndexes(indexes);
      }
    } else {
      WTemplateFormView::updateViewValue(model, field, edit);
    }
  }

  void updateModelValue(WFormModel* model, WFormModel::Field field, WFormWidget* edit) override
  {
    if (field == ModelWithSelection::SelectionBoxField) {
      if (auto selectionBox = dynamic_cast<WSelectionBox*>(edit)) {
        auto indices = selectionBox->selectedIndexes();
        model->setValue(ModelWithSelection::SelectionBoxField, indices);
      }
    } else {
      WTemplateFormView::updateModelValue(model, field, edit);
    }
  }
}

You may need to add some more safety here and there, and can maybe optimize a couple of things. But that is the rough outline.

I hope that helps a little more than my previous comment.

Best,
Matthias

RE: WFormModel and SelectionBox - Added by Emeric Poupon 16 days ago

Thanks!
Will try this and tell you!
As a workaround, I ended up inheriting from WSelectionBox and implementing setValueText/valueText but as you may imagine I am very unhappy with this solution.

    (1-4/4)