Magicsuggest / magicsuggest

Multiple Selection Combo Box using Bootstrap 3
nicolasbize.com/magicsuggest/
1.3k stars 269 forks source link

Set default value of input for ajax data type, without ajax call during initialization #323

Open ianion opened 6 years ago

ianion commented 6 years ago

Sorry I'm obviously missing something, or this plugin is not able to set the default value on initialization. I'm using multiple suggest inputs im my form, which all have values and some no. After object is created an ajax is launched, immediately after initialization, even if query is empty So 10x ajax calls, for nothing. In documentation is no option for setting "default_data" or preselected values.

Is there a function to set default values in init phase, without even launch ajax call?

ianion commented 6 years ago

I solved my problem with a small modification in the code. It would be good to implement something similar.

1# On initialization, leave input empty on server side. Enclose code responsible for ajax call in function _processSuggestions()

if(typeof(data) === 'string') { // get results from ajax
    if (ms.input.is(':focus') && ms.input.val().trim() !== '') {
        ......
    }
}

2# Add new function, that sets the _cbData directly. Data must be formated like expected.

this.setEntries =  function(data){
    _cbData = data;
};

3# After plugin is initialized, you need to call only mysuggest.setEntries(); and then mysuggest.setValue()

I hope this helps. Great plugin, thank you.