Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I put default values in multiselect #41

Closed
marisoru opened this issue Feb 25, 2015 · 13 comments
Closed

How do I put default values in multiselect #41

marisoru opened this issue Feb 25, 2015 · 13 comments
Assignees
Labels
Milestone

Comments

@marisoru
Copy link

I need to include selected values by default. What I mean is to put pre selected values when I initialize the ajax bootstrap select. I tried using .selectpicker('val',[]) and it puts the values selected but not with the ajax bootstrap select styles, and when I want to add a new value typing on the search input the preselected values are deleted.

@truckingsim
Copy link
Owner

Can you post the ajaxSelectPicker code you are using? So we can see your config.

@marisoru
Copy link
Author

The options:

test

and

$('#reportTemplateGroupFieldValues').selectpicker().ajaxSelectPicker(optionsGroups);

@truckingsim
Copy link
Owner

So we have an option called preserveSelected which will keep previously selected options between ajax requests. Try adding that to your options hash.

var optionsGroups = {
  ajax: { ... },
  log: 3,
  preprocessData: function(){ ... },
  preserveSelected: true
};

@diegoaam
Copy link

Hello, and thanks for the quick answers you give, I have the same problem, I tried with the preserveSelected that you said, and it does not work, I think the problem is that the style of ajaxSelectPicker are not applied, and when I type something it applies the styles and for sure deletes the selected values

showcase

If you see the left image the styles are not applied, it should have a 'Current Selected' div and all that stuff

In the next image I clicked on 'Brasil' and now 'Colombia' goes to Currently Selected and then I can type without any problem
showcase2

@truckingsim
Copy link
Owner

Ahh that second image helps. Ok yeah this is a bug. Basically we aren't checking for currently selected on initialization only on update/keydown. It will probably be this weekend before I have time to work on this though.

@truckingsim truckingsim self-assigned this Feb 25, 2015
@KaitaniLabs
Copy link
Contributor

+1, I also need this for my system. Thanks

@truckingsim
Copy link
Owner

@diegoaam @MarkCarver I've spent 5 hours on this so far over multiple days and haven't been able to find out why it can't keep initial selected options yet.

@MarkCarver If you can find time, I'm pushing up a bugfix branch which has what I've done so far, maybe you'll see something. The commit df4b2b5 has the change I made that will save one of the options but not both. I also added a 4th select to the example page for testing this.

@markhalliwell
Copy link
Contributor

I'll have to look at this in more depth later. This month is going to be chaotic for me (family/moving).

@truckingsim
Copy link
Owner

Thanks for giving it a quick once over. Just moved myself so fully understand. Good luck!

@truckingsim
Copy link
Owner

Adding this 2.0.0 I really want to get this fixed.

@jengjeng
Copy link

jengjeng commented May 9, 2017

You need to insert <option selected> tag before initializing select picker and prevent cache bug by using this code
// Use after init select picker
$('#select-picker').trigger('change').data('AjaxBootstrapSelect').list.cache = {}

You can try from this fiddle https://jsfiddle.net/kinkongjeng/2o7j08kb/

@callumstrubi
Copy link

Can confirm that the fix from @jengjeng works and also fixes a secondary bug of de-selecting pre-selected options that must be incorrectly initialised.

I'd consider this bug critical and without the workaround we'd have had to implement an alternative library entirely.

@truckingsim truckingsim marked this as a duplicate of #121 Jul 21, 2017
@markhalliwell
Copy link
Contributor

I believe this will be fixed by #164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants