Project

General

Profile

setAttributeValue Combobox option

Added by K G over 1 year ago

Hello,

I am trying to set data-foo on selectbox option using setAttributeValue. I can use setAttributeValue on the combobox and it works, but I cant set it against the combobox options.

<select id="select">

  <option value="1" data-foo="dogs" >this</option>
  <option value="2" data-foo="cats">that</option>
  <option value="3" data-foo="gerbils">other</option>
</select>

Thanks!


Replies (3)

RE: setAttributeValue Combobox option - Added by K G over 1 year ago

Ok, so I was a little lazy in detailing my question. For that, I will provide a more detailed question and a solution that I was able to get to work.

I was following the example from the widget gallery in creating a combobox (https://webtoolkit.eu/widgets/forms/combo-box). I was using a standardItemModel to populate the combobox. My standardItemModel was 2 columns, with the first being Wt::ItemDataRole::Display and the second column Wt::ItemDataRole::User. This second column contained a unique number for each option. Setting and collecting the data in the model worked as expected but what I was ultimately trying to accomplish was to use setAttributeValue on the options in the combobox. I wanted to be able to assign a data-value to each option in the dropdown but could not find a way to do so using setAttributeValue.

I was able to assign the attributes using doJavascript and everything worked perfectly.

    for (int p =0; p<personComboModel2->rowCount(); p++)
        {

            auto a1 = Wt::asNumber(personComboModel2->data(personComboModel2->index(p,0)));
            std::cout << a1 <<std::endl;
            ss << "document.getElementById('PersonSelect').options[" << p << "].setAttribute('data-uid','" << a1 << "');";

           this->doJavaScript(ss.str());

        }

I am curious to know if there was another way; other than calling JavaScript, to setting the data attribute on combobox options.

RE: setAttributeValue Combobox option - Added by Korneel Dumon over 1 year ago

Hi K G,

I don't think there is currently any way to set it from C++ since the <option> tags don't correspond to a C++ widget (as opposed to <select> which matches the WComboBox). Apologies for the unsatisfying answer :)

RE: setAttributeValue Combobox option - Added by K G over 1 year ago

Didn't think so but Thanks for the response!

    (1-3/3)