Magicsuggest / magicsuggest

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

Programmatically selecting with setSelection #337

Open marcomarsala opened 5 years ago

marcomarsala commented 5 years ago

I'm trying to programmatically select an element in a combo box powered by MagicSuggest.

The following code, the first time gives an error, the second time works as expected, the third time gives error, the fourth time works, and so on:

citta = $('#ms_citta').magicSuggest().getData().find(x => x.name === 'Genova');
$('#ms_citta').magicSuggest().setSelection(citta);

The error is:

Uncaught TypeError: Cannot read property 'cap' of undefined
    at MagicSuggest.eval (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:113417:26)
    at MagicSuggest.dispatch (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:3:8549)
    at MagicSuggest.r.handle (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:3:5252)
    at Object.trigger (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:3:7650)
    at Object.e.event.trigger (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:2:6732)
    at MagicSuggest.eval (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:3:15517)
    at Function.each (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:2:2975)
    at e.fn.init.each (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:2:835)
    at e.fn.init.trigger (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:3:15493)
    at MagicSuggest.removeFromSelection (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (VM8367 jquery.min.js:2))), <anonymous>:1:3563)
zergioz commented 4 years ago

Provide minimal and reproducible example Thanks.