Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selectpicker with select all by default

I am using the selectpicker plugin. Now I am trying to select all the options by default, or at least a button to select all without the need of click in the dropdown.

Currently the demo only works if the dropdown is clicked and after that, click the button.

Any idea?

$('.selectpicker').selectpicker();


$(".btn_clk").click(function () {
    $('.selectpicker').selectpicker('selectAll');
});

http://jsfiddle.net/tpnw96ed/

like image 444
user2990084 Avatar asked Dec 15 '14 15:12

user2990084


People also ask

What is selectpicker('refresh')?

.selectpicker('refresh')To programmatically update a select with JavaScript, first manipulate the select, then use the refresh method to update the UI to match the new state.

How to destroy selectpicker?

selectpicker("destroy"); $('select#pause' + _week + _day). selectpicker("destroy"); In other plugins if you use destroy, it shows the original element again. Is this an issue in the boostrap select plugin or is there another way to remove the bootstrap select and reshow the original select.

What is select picker?

Select Picker is a jQuery plugin supporting work with select boxes. It extends the default possibilities of select boxes with a new range of features.

How to use selectpicker in bootstrap?

selectpicker'). selectpicker({ style: 'btn-info', size: 4 }); }); Your script is declared and therefore executed before the <select class="selectpicker"> -declaration, causing bootstrap-select never being initialized opon . selectpicker (selectpicker doesnt exists when the script is runned).


2 Answers

There is default way to add Select All and Deselect All buttons to selectpicker now. You should use option data-actions-box="true" to enable buttons and data-select-all-text="Select all buttton name" data-deselect-all-text="Deselect all button name" to rename them

like image 166
Ivan Platonov Avatar answered Sep 21 '22 15:09

Ivan Platonov


You can open/close the Selectpicker before/after clicking the button:

$(".btn_clk").click(function () {
    $('.dropdown-menu').css("display","block");
    $('.selectpicker').selectpicker('selectAll');
    $('.dropdown-menu').css("display","none");
});

Edit: You will also need to add the following to manually trigger the dropdown on clicks after a selectAll:

$('.selectpicker').click(function () {
    if($('.dropdown-menu').css("display") == "block") $('.dropdown-menu').css("display","none");
    else $('.dropdown-menu').css("display","block");
});

JSFIDDLE

like image 41
ltalhouarne Avatar answered Sep 25 '22 15:09

ltalhouarne