davidstutz / bootstrap-multiselect

JQuery multiselect plugin based on Twitter Bootstrap.
https://davidstutz.github.io/bootstrap-multiselect/
Other
3.68k stars 1.98k forks source link

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

Open mhguelleh opened 2 years ago

mhguelleh commented 2 years ago

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 
    }
    )) || [];
  2. My multiselect element is initialized with the array like so: $(multiSelectElement).multiselect('dataprovider', arrFillMultiSelectElement);

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

  4. 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