Problem with Bootstrap Multiselect selection button width

I used Multiselect of Bootstrap.

My problem is when I select an item from the options. If the name is too long, the width of the button also increases.

enter image description here

How to handle the width of buttons after selecting options.

+4
source share
4 answers

Try using the code below.

You can set the maximum width as you want in css.

JavaScript:

 $('#yourXYZID').multiselect({
    buttonText: function(options) 
    {
        var retStr = "";
        if (options.length === 0) {
           retStr = "None selected";
        } else if(options.length <=3){
           var textArray = [];
           $.each(options,function(key,value){
               textArray.push($.trim($(value).html()));
           });
           retStr = "<div class='pull-left restricted'>"+textArray.join(",")+"</div>";
        } else {
           retStr = options.length+" selected";
        }
        return retStr+" <b class='caret'></b>";
    }
 });

CSS

.multiselect.dropdown-toggle.btn.btn-default > div.restricted {
    margin-right: 5px;
    max-width: 100px;
    overflow: hidden;
}
+1
source

Another job is that in the "buttonText" function in the bootstrap-multiselect.js file, we can set the length of the button text, and then return as follows:

if(selected.length>20){ 
   return selected.substr(0, 20); //if the text is too long, then this if statement will be executed
 }
else{
   return selected.substr(0, selected.length - this.delimiterText.length);
 }

/*Bootstrap adds extra characters for the selected option.For example: the length of the option 'peter' will actually be 14 because of the delimiter and extra spaces. So you may want to set a bigger number in the if statement*/

. :

return selected.substr(0, selected.length - this.delimiterText.length);
+1

, , . :

$('#anyID').multiselect({       

and then some properties like

maxHeight:'300px'

well, if you want to change the width of the button on a specific event. Do it:

buttonWidth:function(){             
     if(something happens) 
                    return '300px';  //  value chosen randomly
                else
                    return "150px";  //  value chosen randomly
    },

and so you will have

FINAL MULTIMETER

 $('#anyID').multiselect({      

    maxHeight:'300px',
    buttonWidth:function(){             
     if(something happens) 
                    return '300px';  //  value chosen randomly
                else
                    return "150px";  //  value chosen randomly
    },
 )};
0
source

.multiselectgives you a parameter buttonWidth. This will keep the width wider if nothing is selected. You can also add%. You can set the width like this:

<script type="text/javascript">
    $(document).ready(function() {
        $('#yourcontrol').multiselect({
            buttonWidth: '100px'
        });
        //caret is in the middle, switch it to right
        $(".caret").css('float', 'right');
        $(".caret").css('margin', '8px 0');
    });
</script>
0
source

All Articles