How to dynamically set the 'multiple' parameter in a Select2 box?
How to dynamically set the 'multiple' parameter in a Select2 box?
ttsullivan
Posts: 10Questions: 5Answers: 1
in Select
Allan, my application includes a chain of select2 boxes (5 are dependent). For the final box in the chain, depending upon the option selected, I need to show either a single value select box or a multi-value select box. In order to accommodate either situation Is there a way to dynamically change the field's opts: "multiple" parameter to either true or false?
This discussion has been closed.
Answers
I am not Allan but I am pretty sure there isn't. I couldn't even change the options for a selectize field after dt initialization. (It works with select fields though.)
But there is always a work around:
- Define the field as a "regular" field in your Editor instance so that it is a known field after initialization (doesn't have to be declared as a select2 field at that time). If you don't define it initially you may run into all sorts of problems if your are using the field in other data tables events. So it doesn't hurt to define the field as a dummy initially.
- on "open" (or whatever event is suitable) clear the field and add it again the way you need it as single edit or multi edit.
https://editor.datatables.net/reference/api/clear()
https://editor.datatables.net/reference/api/add()
Here is an example. I need to read the options individually for each row selected. (Editor uses the same options for all records of the data table which wasn't suitable in my case.) With selectize you can't update the options dynamically after initialization. Hence the work around:
And this is the definition of the field in the Editor instance (yes, label is redundant here but I kept it for better legibility):