davidstutz/bootstrap-multiselect

How to dynamically add option value to an existing multiselect element that has data

mhguelleh opened this issue · 0 comments

Hi, thanks for the wonderfull library.
But I am looking for a feature that I did not see in the docs.

Is there a way to add a newly created option to an existing multiselect element while preserving the data that's already present in the multiselect ?

For example, I have a multiselect that is initialized with an array of data on page load

  1. My array is filled like so:
const arrFillMultiSelectElement = arrDataDependencies?.map(data => (
    {
        "label": data.at(1)?.toString().trim() || "", // name
        "title": data.at(1)?.toString().trim() || "", // name
        "value": data.at(0)?.toString().trim() || "", // id
        "selected": false, 
        "disabled": false, 
        "attributes": null 
    }
)) || [];
  1. My multiselect element is initialized with the array like so:
    $(multiSelectElement).multiselect('dataprovider', arrFillMultiSelectElement);

  2. The multiselect is checked and some values are selected.

  3. But then a new value is added later in the HTML and I need to read and add that value to the multiselect (already contains data and values).

Right now, I can just do by destroying what's present and recreating the multiselect from scratch, which means I lose all information that was already checked within the multiselect.

Thanks for any help