The options in the Dropdown choices-js remain hidden beneath a div within a bootstrap table-responsive element

When utilizing the choices-js drop-down menu within a Bootstrap 5 responsive table, I am encountering display issues. Specifically, when ID 1 is selected, it appears incorrectly while ID 3 displays correctly.

It is crucial that overflow-x incorporates a scroll bar while overflow-y does not.

However, setting overflow: inherit does not produce the desired outcome.

.table-responsive{ overflow: inherit !important; }

Code Snippet:

let task_data = [
  { value: "1", label: "TASK 100" },
  { value: "2", label: "TASK 101" },
  { value: "3", label: "TASK 102" },
  { value: "4", label: "TASK 103" },
  { value: "5", label: "TASK 104" },
  { value: "6", label: "TASK 105" },
];

new Choices($(`#task_0`)[0], {
  position: "bottom",
  choices: task_data,
  allowHTML: true,
});
body {
  background-color: #f5f5f5;
}

.table-responsive {
  overflow-y: visible!important;
}

#calplaceholder {
  max-height: 400px;
  height: auto!important;
  min-height: auto!important;
}

.cal-viewmonth,
.cal-toprow,
.cal-toprow-pro td:first-child {
  text-align: center;
  vertical-align: middle !important;
  font-weight: 600 !important;
}

.cal-toprow {
  font-size: 0.8rem;
  width: 162px;
  min-width: 162px;
  color: #3e5569;
  background-color: #F7F9FB!important;
  border: 1px solid #CCCCCC;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/choices.js/10.1.0/choices.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/choices.js/10.1.0/choices.min.js"></script>

<div class="row mt-5 justify-content-center">
  <div class="col-sm-11 col-md-8">
    <div id="calplaceholder" class="table-responsive bg-white p-3">
      <table class="table table-striped table-bordered cal-borders">
        <thead>
          <tr>
            <th class="cal-toprow">1</th>
            <th class="cal-toprow">2</th>
            <th class="cal-toprow">3</th>
            <th class="cal-toprow">4</th>
            <th class="cal-toprow">5</th>
            <th class="cal-toprow">6</th>
            <th class="cal-toprow">7</th>
            <th class="cal-toprow">8</th>
            <th class="cal-toprow">9</th>
            <th class="cal-toprow">10</th>
            <th class="cal-toprow">11</th>
            <th class="cal-toprow">12</th>
            <th class="cal-toprow">13</th>
            <th class="cal-toprow">14</th>
            <th class="cal-toprow">15</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="p-0">
              <select id="task_0" class="form-select"></select>
            </td>
            <td class="p-0"></td>
            <td class="p-0">
              <div id="task_1">
                <select class="form-select">
                  <option value="1">TASK 100</option>
                  <option value="2">TASK 101</option>
                  <option value="3">TASK 102</option>
                  <option value="4">TASK 103</option>
                  <option value="5">TASK 104</option>
                  <option value="6">TASK 105</option>
                </select>
              </div>
            </td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
            <td class="p-0"></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

Answer №1

To bring the dropdown out of the relative box, you'll need to use some clever tricks.

Here is a function that can help manipulate the dropdown style:

// Define scrollableElement as the table container
// Select selectElement as the element Choices should be applied to
// Specify options for Choices
function applyChoices(scrollableElement, selectElement, options) {
  const choices = new Choices(selectElement, options);

  const containerOuter = choices.containerOuter.element;
  const dropdown = choices.dropdown.element;
  const containerRec = containerOuter.getBoundingClientRect();

  dropdown.style.position = 'fixed';
  dropdown.style.left = `${containerRec.x}px`;
  dropdown.style.top = `${containerRec.y + containerRec.height}px`;
  dropdown.style.width = `${containerOuter.offsetWidth}px`;

  scrollableElement.addEventListener('scroll', function () {
    choices.hideDropdown();
    dropdown.style.left = `${
      containerRec.x - scrollableElement.scrollLeft
    }px`;
    dropdown.style.top = `${
      containerRec.y - scrollableElement.scrollTop + containerRec.height
    }px`;
  });
}

You have the flexibility to change the position of the dropdown!

Check out this Codesandbox code to understand how to utilize this function.

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

"Struggling to horizontally center a div with a fixed width in CSS - what am I

Whenever I try to use it on my browser, the content shifts to the left and I can't seem to center it using text-align or flexbox. The issue gets resolved when I remove the width property, but I really need it to have that paragraph-style look I'm ...

The functionality of jQuery's slideUp and slideDown is not performing as intended

I am trying to implement a feature where a div element will hide and show based on a checkbox selection using jQuery's slideUp() and slideDown() functions. The structure of my div is as follows: <div class="parent"> <label class="childLe ...

Guide on integrating animate.css animations with Vue's Transition and TransitionGroup components

Vue offers the v-if and v-for directives that allow you to manipulate elements in the DOM based on certain conditions. In order to animate elements controlled by v-if and v-for, you need to utilize the built-in Transition and TransitionGroup components. Bu ...

"Angular 6: A Guide to Customizing Text Colors Based on Status

I have a view on angular just like this: https://i.sstatic.net/JimWN.png And this is my dashboard.component.ts: export class DashboardComponent implements OnInit { tablePresetColumns; tablePresetData; ngOnInit() { this.tablePresetColumns = [{id: ...

Troubleshooting issues with Bootstrap's responsiveness configuration

I've been working on creating a responsive user login page using Angular 5. It looks great on full-screen display. https://i.sstatic.net/YQrL5.png However, when I resize the browser window, the responsiveness seems to break. https://i.sstatic.net/4 ...

The added class is not being successfully applied to the ClassList

I'm currently working on a page where I want the colors of a button and the background to switch when the button is clicked. document.querySelector("body.light").classList.toggle("dark"); document.querySelector("button.dark").classList.toggle("light" ...

Verify if a personalized angular directive is able to display or conceal an element

I have a custom directive that toggles the visibility of an element based on the value of another service. I attempted to write a test to verify if the directive functions as intended. Initially, I used the ":visible" selector in my test, but it consistent ...

Strategies for reducing the amount of space between cards in Bootstrap 4

After creating 4 cards using Bootstrap version 4.5, I noticed that the spacing is not right. I'm struggling to reduce the spacing between the cards. Have a look at the image below if you're still confused: https://i.sstatic.net/RrJLL.jpg .car ...

Expanding the Width of a Web Page with CSS on a Squarespace Website

Looking to adjust the width of my Squarespace website on the SKYE template to have minimal padding around images and content. Check out the website here using the password123. I've attempted to make changes with no success using the following code: ...

Issue with Material UI Menu positioning incorrectly when button is aligned to the right or left

When looking at the first image, it appears that the material-UI menu position is working correctly. However, when we change the button position to align-right, the menu position shifts slightly to the left, not perfectly aligning with the right side as in ...

Having trouble with the DataTables jQuery plugin? Seeing a blank page instead of the expected results?

I've been trying to set up the Datatables jquery plugin for my HTML table but it's not working as expected. I have linked to the Datatables CDN for CSS styling and script, along with Google's hosted jQuery plugin. Additionally, I have a loca ...

Identifying elements using xpath - Streamlined Xpath Techniques

I successfully found the element using the xpath copied directly from the code. Can someone assist me in creating a simpler xpath for the following code snippet, which is fully functional! WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id=&ap ...

Modal closes automatically after being clicked twice on data-bs-dismiss

Having an odd issue with Bootstrap 5.0.2 In order to close the modal, I have to double-click the button (data-bs-dismiss) It's worth noting that I use multiple modals on the page, but they only open when needed. <div class="modal" id="open-rat ...

What is the best way to choose every single element on the webpage excluding a specific element along with all of its children?

Is there a way to hide all content on a page except for a specific element and its children using CSS? I've been exploring the :not selector, but I'm struggling to make it work with the * selector. :not(.list-app) { display: none; } :not(. ...

Python KeyError: Implementing a Strategy for Displaying Two Divs Side by Side Efficiently with Dynamic Data

media.py: http://pastebin.com/r1nxPzTQ This file contains a Python class with methods like __init__ and show_trailer(self). fresh_tomatoes.py: http://pastebin.com/bcdUYiiu It's a Python script that generates an HTML file and populates it using data ...

Using CSS styling to dictate how browsers display web content, specifically through setting font size measurements in EM

On different mobile devices and various browsers such as Internet Explorer and Mozilla, the font size in some divs does not appear consistent. Specifically, on a Samsung Galaxy S6. I am using EM units: I know it may not be ideal but I prefer this method a ...

W3C validation issue: "Failure to immediately follow a start-tag with a null end-tag" and similar errors

Encountering validation errors with the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <hea ...

Maintaining aspect ratio while resizing images: A foolproof guide

I've been working on image editing and encountered a bit of trouble with aspect ratios. <img src="big_image.jpg" width="900" height="600" alt="" /> As you can see, the height and width are already specifi ...

The click function for the responsive navbar hamburger is not functioning properly

Having some trouble with the code not working in responsive mode. I've tested it on a 600px screen and the hamburger button doesn't seem to work (I click it and nothing happens). I've gone through both the CSS and JS multiple times but can&a ...

Designing tables and image galleries using HTML, CSS, and jQuery: A guide

How can I easily transform tabular data into thumbnails with the click of a button? I need a 2-view system that switches between table and thumbnail views without including image thumbnails. I welcome any creative suggestions! :) Examples: ...