chosen icon indicating copy to clipboard operation
chosen copied to clipboard

Add optional Select All / Deselect All to the UI for multi-select lists

Open ArtemGoutsoul opened this issue 11 years ago • 34 comments

Option allow_deselect_all:true:

  • If more than 1 item is selected show a little cross in the bottom right corner to deselect all

Option allow_select_all:"Select All":

  • If there are more than 1 options - show one extra option (colored differently) saying "Select All" - this option should add all currently filtered options to the selection

ArtemGoutsoul avatar Jan 23 '13 11:01 ArtemGoutsoul

+1

indiejames avatar Feb 05 '13 19:02 indiejames

Too bad this sat around for 5 months, is this feature still being considered, on the roadmap, already implemented or dead?

halcyonandon avatar Jun 13 '13 20:06 halcyonandon

+1

cschroed avatar Jul 24 '13 11:07 cschroed

+1

I put an answer concerning this request on StackOverflow thread: http://stackoverflow.com/questions/11172269/select-all-and-remove-all-with-chosen-js#18785302 if it is of any help (of course should be a cleaner solution and work with grouping and all)

ratamaa avatar Sep 13 '13 12:09 ratamaa

+1

th3sly avatar Nov 29 '13 08:11 th3sly

+1

budlight avatar Feb 25 '14 17:02 budlight

I ended up just doing something on my own. Seems to work out okay. I am 100% sure there is a better way to do this. I would rather have it built in though. Just add the class 'chosen-select-all' to your chosen select.

// Add the Select All and Deselect all options to the chosen multiselect control
$(".chosen-select.chosen-select-all").each(function(){
    var parentSelect = $(this);

    //check to see if this was already added.  
    var selectAllOption = parentSelect.find("option[value='chosen-select-all-option']");
    if(selectAllOption == undefined || selectAllOption.length == 0){
        //Add the options as default first and last for Select and Deselect respectively.
        parentSelect.prepend("<option value='chosen-select-all-option' id='chosen-select-all-option'>-- Select All --</option>");
        parentSelect.append("<option value='chosen-select-none-option' id='chosen-select-none-option'>-- Deselect All --</option>");

        //When it chages loop through the options list to see which were selected.
        parentSelect.change(function() {
            $(this).find("option:selected").each(function(){
                var value = $(this).attr("value");
                  switch (value){
                    //If one of the options selected was the 'Select All' option, remove the Select All and Deselect All options, set all other options to selected, add the master Select All and Deselect All back after the fact. Update Chosen
                    case "chosen-select-all-option":
                        parentSelect.find("option[value='chosen-select-all-option']").remove();
                        parentSelect.find("option[value='chosen-select-none-option']").remove();
                        parentSelect.find("option").prop("selected","selected");
                        parentSelect.prepend("<option value='chosen-select-all-option' id='chosen-select-all-option'>-- Select All --</option>");
                        parentSelect.append("<option value='chosen-select-none-option' id='chosen-select-none-option'>-- Deselect All --</option>");
                        parentSelect.trigger("chosen:updated");
                      break;
                    case "chosen-select-none-option":
                        parentSelect.find("option[value='chosen-select-all-option']").remove();
                        parentSelect.find("option[value='chosen-select-none-option']").remove();
                        parentSelect.find("option").prop("selected",false);
                        parentSelect.prepend("<option value='chosen-select-all-option' id='chosen-select-all-option'>-- Select All --</option>");
                        parentSelect.append("<option value='chosen-select-none-option' id='chosen-select-none-option'>-- Deselect All --</option>");
                        parentSelect.trigger("chosen:updated");
                        break;
                  }
            });
          }).trigger( "change" );

        //Update chosen to include the Select all and Deselect All options
        parentSelect.trigger("chosen:updated");
    }
});

jordan1210j avatar Mar 11 '14 19:03 jordan1210j

I guess adding it into the actual list like you did there probably solves a lot of UI issues. I think later versions of jQuery would use

'selected', true

Not sure if "selected" will eventually quit working. You could also add a class to the ALL/NONE option choices and use a not() selector to clean this code up quite a bit, by not forcing the adding and removal of the option items.

budlight avatar Mar 12 '14 00:03 budlight

I'm on an older version of jquery, but yes! Also good call on the class thing. I'm very dumb for not thinking of that. Very dumb indeed. Stupid legacy apps are making me sloppy. I just wanted to post a solution to the interwebs to help.

jordan1210j avatar Mar 12 '14 01:03 jordan1210j

Well i think you solved what would keep this from getting merged in by solving how to display the ui for this.

Also .val('') might be even more compatible than .prop("selected",false) I know it works for me.

budlight avatar Mar 12 '14 01:03 budlight

+1

wizonesolutions avatar Mar 26 '14 16:03 wizonesolutions

+1 That would be really helpful feature

cspwizard avatar May 21 '14 06:05 cspwizard

+1, but you can first select the option, and then update the chosen, like this:

$('option').prop('selected', true); //true to select all, and false to deselect.
$('select').trigger('chosen:updated');

This way, you can put it within a click event.

raphaklaus avatar Jul 31 '14 13:07 raphaklaus

+1 pretty please

joelworsham avatar Feb 27 '15 17:02 joelworsham

+1

mohamedbenjelloun avatar Aug 27 '15 15:08 mohamedbenjelloun

+1

laulaz avatar Dec 21 '15 21:12 laulaz

+1

RodrigoSampaio avatar Feb 26 '16 15:02 RodrigoSampaio

+1

selvarajpalani avatar Mar 10 '16 22:03 selvarajpalani

and deselect for single select

selvarajpalani avatar Mar 10 '16 22:03 selvarajpalani

+1

gwin003 avatar Mar 15 '16 17:03 gwin003

+1

doganmeh avatar Apr 13 '16 15:04 doganmeh

+1

taschetto avatar May 10 '16 14:05 taschetto

👍 🙏

jakefreeberg avatar May 25 '16 14:05 jakefreeberg

+1

varp avatar Jul 12 '16 18:07 varp

+1

ghost avatar Aug 04 '16 08:08 ghost

👍

michaeldevenney avatar Jan 12 '17 13:01 michaeldevenney

+1

cyrilchapon avatar May 10 '17 12:05 cyrilchapon

+1

avenet avatar Mar 14 '18 15:03 avenet

+1

misterjoonas avatar Apr 17 '18 06:04 misterjoonas

+1

jishakm avatar Apr 18 '18 18:04 jishakm