I would suggest to have a state in your component called selectedItems
The onChange
callback then, takes as parameters element
and checked
, from the Bootstrap Multiselect docs. Element has the val()
method, which returns the value assigned to the option.
Therefore handleChange could be implemented in the following way
handleChange: function (element, checked) {
var newSelectItems = _.extend({}, this.state.selectItems);
newSelectItems[element.val()] = checked;
this.setState({selectItems: newSelectItems})
},
getInitialState: function () {
return {selectItems: {}};
}
In this way, every time an element is clicked, its checked
attribute is saved in the component state, which is quite handy if you need to change anything based on the MultiSelect
selected values.
Please note that for the above code you need either the Underscore or the Lodash library. This is necessary as React cannot merge nested objects, as answered here.