Revise the design of the bootstrap-multiselect interface

I am currently utilizing the bootstrap-multiselect plugin for filtering options, which can be found at https://github.com/davidstutz/bootstrap-multiselect. Everything is functioning correctly except for one issue. My dropdown menu contains approximately 80-90 values, requiring a scroll feature. However, when I enable scrolling, the search box also scrolls along with the options, causing it to get hidden when reaching the last value. I need the search box to remain fixed at the top while allowing the options to scroll beneath it.

Upon inspecting the HTML code generated by the bootstrap-multiselect plugin, I noticed that the search control is within the same parent element.

<ul class="multiselect-container dropdown-menu" aria-expanded="false">
 <li class="multiselect-item filter" value="0" role="menuitem">
   <div class="input-group">
       <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input type="text" class="form-control multiselect-search" placeholder="Search">
      <span class="input-group-btn"><button type="button" class="btn btn-default multiselect-clear-filter"><i class="glyphicon glyphicon-remove-circle"></i></button></span>
 </div>
</li>
<li role="menuitem"><a tabindex="0"><label class="radio"><input type="radio" value=""> Please select</label></a></li>
<li class="multiselect-item multiselect-group" role="menuitem"><label>Test</label></li>
<li role="menuitem"><a tabindex="0"><label class="radio"><input type="radio" value="1"> Item 1</label></a></li>

</ul>

CSS Used:-

ul.dropdown-menu {
    font-size: 1.1em;
    max-height: 300px;
    overflow: auto;
    z-index: 9999;
}

Is there a CSS solution available to keep the search box in a fixed position while allowing the options to scroll beneath it? I have already explored the template option provided by the library without success.

Thank you

Answer №1

This is my debut answer on SO, so any constructive feedback is highly appreciated.

I recently achieved this feat by following these steps:

  1. Set the "frozen at top" items' position to "fixed" in CSS and make necessary adjustments to ensure that the remaining items are not visible behind them when the dropdown expands.

  2. Invoke a function (refer to resetDropdownHeight() in the fiddle below) in the multiselect's onDropdownShown event to adjust the expanded dropdown's height based on the frozen items' height.

  3. To maintain correct dropdown height during filtering, modify the buildFilter: function in bootstrap-multiselect.js to incorporate the resetDropdownHeight() function. If collapsible option groups are used, do the same within the click event for

    $('li.multiselect-group > a', this.$ul)
    .

Although demonstrating item #3 (above) in the example fiddle was challenging, here are the instances where you should call resetDropdownHeight() in your modified version of bootstrap-multiselect.js:

buildFilter:
1. Call it as the final line of the clearBtn.on event
2. Call it as the last line of the extensive function for this.searchTimeout

"click" event for

$('li.multiselect-group > a', this.$ul)
:
1. Call it as the ultimate line of the event

Check out an example here (includes #1 and #2 only): https://jsfiddle.net/mjh42/v3bc9udk/

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

        <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css"/>

        <script type="text/javascript" src="https://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>

        <style type="text/css">
            #freezeDemo + div.btn-group li.multiselect-filter {
                position: fixed;
                left: 1px;
                top: 35px;
                width: 162px;
                z-index: 10;
                background-color: white;
            }

            #freezeDemo + div.btn-group li.multiselect-all {
                position: fixed;
                left: 1px;
                top: 75px;
                width: 162px;
                z-index: 10;
                background-color: white;
                border-bottom: 1px solid;
            }

            #freezeDemo + div.btn-group ul.multiselect-container.dropdown-menu > li:not(.multiselect-filter):not(.multiselect-all) {
                position: relative;
                top: 65px;
            }

            #freezeDemo + div.btn-group ul.multiselect-container.dropdown-menu {
                top: 34px;
                width: 180px;
            }
        </style>
    </head>
    <body>
        <select id="freezeDemo" multiple="multiple">
            <optgroup label="Group A">
                <option value="A1">Item A1</option>
                <option value="A2">Item A2</option>
                <option value="A3">Item A3</option>
                <option value="A4">Item A4</option>
                <option value="A5">Item A5</option>
            </optgroup>
            <optgroup label="Group B">
                <option value="B1">Item B1</option>
                <option value="B2">Item B2</option>
                <option value="B3">Item B3</option>
                <option value="B4">Item B4</option>
                <option value="B5">Item B5</option>
            </optgroup>
        </select>

        <script type="text/javascript">
            var $_dropdownMenu = null;

            initializeBootstrapMultiselect();

            function initializeBootstrapMultiselect() {
                $('#freezeDemo').multiselect({
                    buttonWidth: '180px',
                    enableCollapsibleOptGroups: true,
                    includeSelectAllOption: true,
                    enableFiltering: true,
                    enableCaseInsensitiveFiltering: true,
                    maxHeight: 200,
                    onDropdownShown: function(event) {
                        resetDropdownHeight();
                    }
                });

                $_dropdownMenu = $('#freezeDemo + div.btn-group > button.multiselect.dropdown-toggle.btn.btn-default ~ ul.multiselect-container.dropdown-menu');
            }

            function resetDropdownHeight() {
                if ($_dropdownMenu !== null) {
                    $_dropdownMenu.css('height', 'auto');

                    var filterHeight = $('li.multiselect-filter', $_dropdownMenu).height();
                    var selectAllHeight = $('li.multiselect-all', $_dropdownMenu).height();

                    if (isNaN(filterHeight)) { filterHeight = 0 }
                    if (isNaN(selectAllHeight)) { selectAllHeight = 0 }

                    var heightAdjustment = filterHeight + selectAllHeight;
                    var currentDropdownHeight = $_filterDropdownMenu.height();
                    var newHeight = (currentDropdownHeight + heightAdjustment).toString() + 'px';

                    $_dropdownMenu.css('height', newHeight);
                }
            }
        </script>
    </body>
</html>

Answer №2

  • Insert this code snippet into your jQuery script:

     templates: {
             dropdown: '<ul class="dropdown-menu-list" style="height:300px;overflow-y:auto;"></ul>'
        }
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a method to streamline the code by utilizing a numeric array?

Currently, I am encountering an issue with jQuery. My objective is to create a gallery using the following code: HTML <div id="gallery"> <img src="img/1.jpg" alt="image" id="img"> </div> <div id="thumbnails"> <img src= ...

Trouble with CSS display in Internet Explorer 8

My website located at this link is working perfectly in IE 10 and IE 11, however, any version lower than that is causing the content to be displayed on the far left of the screen instead of centering it. I have tried tweaking the CSS but can't seem to ...

How to make QWebView background translucent

My goal is to make the background of a QWebView element transparent. +---------------------------+ | x | Window title | <<< Hidden borders and title bar +---------------------------+ view->setWindowFlags(Qt::FramelessWindowHint); | ...

Maintaining the integrity of Jquery Tab even after refreshing the page is essential

I recently started using Jquery and encountered an issue with tab implementation. Whenever I refresh the page, it automatically directs me back to the initial tab setting. $(function() { var indicator = $('#indicator'), i ...

Tips for creating CSS styles for a selected input field

I seem to be stuck in a situation where my screen looks similar to the screenshot provided. There are four input elements that I would like to have bordered just like in the image, complete with a circled tick mark. I've managed to create these four i ...

How to use jQuery to locate a numerical value within a string

Here is my input: var str = 'Cost USD 400.00'; I am trying to figure out how to use jQuery to extract only the number from this string. Can anyone help me with that? ...

Navigate through a series of div elements using Jquery

I need help figuring out how to make the window scroll between different divs in a sequence. The issue is that my current code only works for one specific div at a time. $('.down_arrow').click(function(e){ $('html, body') ...

Is it possible to utilize a jQuery function to count JSON data based on specific keys and values?

function calculateJSONObjFrequencyByKeyValue(obj, key, value) { var frequencyCount = 0; $.each(obj, function(i, item) { if(obj[i].key == value){ frequencyCount++; } }); alert(frequencyCount); } calculateJSONObjFrequencyByKeyValu ...

Ways to input a string into a field discreetly without displaying it?

I'm currently developing a Rails app that includes a form for users to input their age. When they enter `20`, I want to display it on the next page as `20 years old`. <%= form_tag age_path, method: "POST", id: 'age', remote: true do %> ...

Why doesn't the style show up when JavaScript is turned off with Material UI, CSS Modules, and Next.js?

This is my first time diving into Material UI, CSS Modules, and Next.js with a project. Issue: I noticed that when I disable JavaScript in the Chrome DevTools, the styles are not being applied. I'm not sure if this has something to do with Materia ...

Troubleshoot: Font Rendering Issue with CSS3 @font-face in Opera 11.x

Having some trouble with Opera not displaying my HTML5 site correctly. Everything looks good in Safari, Chrome, IE9, Firefox 3.5+ and more. Wondering if anyone has encountered this before and found a solution. Here's the breakdown of my setup: In the ...

Guide to setting up a back button to return to the previous page in an iframe application on Facebook

I've developed a Facebook application that is contained within an IFRAME. Upon opening the application, users are presented with the main site, and by clicking on the gallery, they can view a variety of products. The gallery includes subpages that lo ...

Guidelines for retrieving a class name using jQuery when hovering over a div

I need assistance in fetching the class name by hovering over a div. Both divs have the same id but slightly different class names. Here is an example: <div id="1-someid" class="1-example-class border cz"> ...more elements go here.... </div> ...

Pass the form data to a Bootstrap modal upon submission of the form

I'm attempting to retrieve form data and convert it to JSON to display in a modal dialog that opens on the Submit button click! This is my progress so far: HTML <form class="form-horizontal" id="configuration-form"> --irrelevant-- <button ...

Troubleshooting Problem With Foundation 5 Abide and Modal AJAX Integration

I am encountering a problem with getting Abide to function properly on a modal form in Foundation 5. The form is triggered by: <a href="/admin/edit_customer/<?= $order->id ?>/<?= $order->cust_id ?>" class="button tiny" data-reveal-id ...

Appium successfully triggers a click action on an obscured WebElement

I am facing an issue with my android appium test for an android application. The app contains a webview with a loader that appears at the start. There is a link within the app that I need to click on, which should navigate to another web-page. Although ...

Clearing existing HTML content in the TR body

I've been struggling with a Jquery/Ajax call that updates cart details. Currently, I can't seem to clear the existing HTML content in the cart-body (tablebody) even though the ajax request adds all the items to the cart successfully. The code sni ...

Enhancing User Experience in VueJS with Pug Templates and Transitions

Managing multiple 'pages' of content within a multi-step process can be challenging, especially when trying to incorporate VueJS's transition feature for a carousel-like slide in/out effect. The contents being transitioned are stored in sepa ...

I am interested in incorporating array elements into a class selector

I need help adding array elements into a class selector. The image attached shows the current result, but I want the divs to be separated like this $(document).ready(function () { var arr = ['top', 'right', "Bottom", &q ...

Tips for organizing the data you have collected from the table

After successfully printing all the data from my database, I am facing a challenge in designing my data effectively. The table I am working with is called post_tbl and it has columns named post_id, post_message, and post_date. This is the query I am usin ...