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

Button to Deselect the selected value ? #1449

Closed
amitsquare opened this issue Jul 24, 2016 · 22 comments
Closed

Button to Deselect the selected value ? #1449

amitsquare opened this issue Jul 24, 2016 · 22 comments
Milestone

Comments

@amitsquare
Copy link

As per documentation , "data-action-box" adds the two button to select all / deselect all button in the header .But this only happens in case of "multiple selection " .

Is there any way to add similar deselect button for single selection dropdown ?

@caseyjhol
Copy link
Member

No - I recommend using a multiple select with data-max-options="1".

@amitsquare
Copy link
Author

But this is not correct solution , As selectAll button will select all the options .

@tontonsb
Copy link

tontonsb commented Nov 6, 2016

I also need a dropdown where you can select an option or select none of them. Currently this requires using multiple, otherwise the user can't deselect. Or we have to introduce the action bar which allows the users to deselectAll and selectAll.

But how do we get value out of this? Currently I have to do this, to get the single value out of multiple-select element:

elementVal = element.val() ? element.val()[0] : null;

Of course, this is all doable, but it shouldn't require so many workarounds to do these things.

It also would be useful if we could include only the deselectAll button without the selectAll :)

@gopuneet
Copy link

gopuneet commented Aug 2, 2017

+1 for Deselect/Clear button for single selection dropdown.
Using a multiple select adds a redundant Select All button and the Deselect All button doesn't look correct as we're allowing selection/deselection of only one option.

@ernestopinchaaqui
Copy link

+1 for Desect/Clear

@mattfeury
Copy link

+1

3 similar comments
@szmalec
Copy link

szmalec commented Apr 27, 2018

+1

@zsoltiNagy
Copy link

+1

@keymastervn
Copy link

+1

@sohebcakewalk
Copy link

+1 for Desect/Clear

@jadamec
Copy link

jadamec commented May 30, 2019

+1

4 similar comments
@florianbinderme
Copy link

+1

@mulite
Copy link

mulite commented Jul 3, 2019

+1

@fr0z3nfyr
Copy link

+1

@maurizioPollen
Copy link

+1

@tombauto
Copy link

tombauto commented Sep 18, 2019

3 years after and this isn't a feature up until now?
I wonder why, as I am new on using this tool and I really like what you have done here.
But this feature is actually expected as we are improving the way traditional selection works, rather than a user adding an option "none" as another selection, as Clear/Deselect is the perfect way to do it.

I tried to insert an element during ready but not all time the element gets inserted, maybe because the generator for selectpicker() was not done yet; However there is no .done() function to extend to selectpicker, so my workwaround was to put this insert to a setTimeout but it feels more a hack rather than proper integration. I also don't get the same result every time, not all the time the element gets inserted even it is a delayed time.

Here is my code for adding element:

$(function(){
$(".selectpicker").find("ul").append('

  • Clear Selection
  • ');
    });

    Although the above code, may work on adding the element, I couldn't make the clear function work.
    I was only able to clear the current selected option, but when selecting another value, its no longer reflecting as selected.

    [EDIT] : I was able to do a work-around but still require setTimeout but for this approach I followed the suggested using of "multiple" with data-max-options="1"
    Here is the code:

            setTimeout(function () {
                enableClearOptionInSelect("opt-mySelection");
            }, 1000);
    
        let enableClearOptionInSelect = function (id) {
            $("#"+ id +" .bs-select-all").hide(); // Hide Select-All
            let btnClear = $("#"+ id +" .bs-deselect-all");
            btnClear.html("Clear Selection");
            btnClear.addClass("w-100");
            console.log("enableClearOptionInSelect completed.");
        }
    

    Output:
    image

    @hartadi
    Copy link

    hartadi commented Sep 24, 2019

    +1 pls

    @caseyjhol
    Copy link
    Member

    caseyjhol commented Apr 25, 2020

    This change will be a part of the v1.14.0 release. This issue will be updated after it's made available.

    How to enable

    allowClear: true or data-allow-clear="true"

    Demos:

    @ctf0
    Copy link

    ctf0 commented Oct 12, 2020

    until the new option is released, u can use the below to target the select with single option only

    .bootstrap-select {
        select[data-max-options='1'] ~ .dropdown-menu .bs-actionsbox {
            display: none;
        }
    }

    now u have the same behavior as multi selection, keep in mind that this will not auto focus the selected option when the menu opens 👎 .

    PS
    there is also https://codepen.io/Prateik2710/pen/vpgKdL

    PPS
    you can also add an empty option to the start of your list.

    caseyjhol added a commit that referenced this issue Nov 28, 2020
    @caseyjhol
    Copy link
    Member

    Released in v1.14.0-beta!

    @scammi
    Copy link

    scammi commented Apr 12, 2021

    Until allowClear function well is tested.

    Screenshot_2021-04-12_17-17-19

    https://codepen.io/scammi/pen/XWpErrL

    @MaaxGr
    Copy link

    MaaxGr commented Feb 5, 2024

    3 years and 1.14 is still not released. :o

    I used input-groups for now to solve this problem:

    <div class="form-group">
        <label for="unternehmen_Ansprechpartner_Intern">Ansprechpartner intern:</label>
        <div class="input-group">
            <select id="unternehmen_Ansprechpartner_Intern"
                    name="unternehmen_Ansprechpartner_Intern"
                    class="form-control selectpicker"
                    data-live-search="true" data-non-selected-text="body" data-allow-clear="true">
                <option value=""></option>
                <option value="1">Großmann, Max</option>
            </select>
            <span class="input-group-addon" id="basic-addon2"
                  style="cursor: pointer"
                  onclick="$('select[name=unternehmen_Ansprechpartner_Intern]').selectpicker('val', '')">
                <i class="fas fa-times"></i>
            </span>
        </div>
    </div>
    

    image

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

    No branches or pull requests