After reducing the size of the table, the data spills over

Hey there, amazing individuals of the internet! Hope you're all doing well today. I've encountered a rather perplexing issue that's got me stumped.

So, here's the situation - I have this table full of data and I want it to initially be in a collapsed state with a scroll bar. Then, when a button is pressed, the size should adjust automatically based on the number of rows in the table. Essentially, the height would expand dynamically.

An illustration of what I'm aiming for can be found in the table on the right: example

Currently, the div shrinks but the content spills out of it.

If you'd like to check out the code snippet, feel free to visit this link: Snippet Link

Just in case you prefer copying the code directly, here it is below:

const expandCollapseBtn = $('#expand-collapse-btn');

$(expandCollapseBtn).click((e) => {
    const divHeight = $(".data-container").height();

    if ( divHeight > 400 ) {
        $(".data-container").animate({"height": "200px"}, 100);
    }
    else if ($(".data-container").css("height") == "200px") {
        $(".data-container").animate({"height": "400px"}, 100);
    }
});
.data-container {
    min-height: 45px;
    background: #fff;
    -webkit-box-shadow: 0 1px 5px 0 rgba(0,0,0,.2);
    -moz-box-shadow: 0 1px 5px 0 rgba(0,0,0,.2);
    box-shadow: 0 1px 5px 0 rgba(0,0,0,.2);
    position: relative;
}


.data-container .bg-light {
    font-size: 1.1rem;
    border-bottom: 1px solid rgba(0,0,0,0.125);
    border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
    font-weight: 700;
}

.data-container h2 {
    font-size: 18px;
    margin: 0;
    padding: 0;
}

section .data-container .table-container .table-striped > tbody > tr:nth-child(2n+1) > td {
    background-color: #f8f9fa;
    line-height: 15px;
}

.data-container .table-container table tr td:first-child {
    padding-left: 50px;
    width: 49%;
    text-align: right;
}

.data-container .table-container table tr td:nth-child(2) {
    padding: 5px 0 5px 0;
    width: 2%;
}

.data-container .table-container table tr td:last-child {
    padding-left: 6px;
    width: 49%;
}

.data-container .table-container table td {
    padding: 5px 18px 5px 20px;
}

.data-container .table-container a {
    font-size: 10px;
    color: red;
    text-decoration: underline;
}

.data-container .table-container tr {
    height: 45px;
}

#flag-image {
    width: 25px;
    height: 25px;
}


.data-container button {
    background-color: transparent;
    color: #343a40;
    border: none;
    outline: none;
    position: absolute;
    left: 50%;
    border-bottom-style: dotted;
    transform: translateX(-50%);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data-container w-75 m-auto">
      <div class="card-header bg-light text-center pt-3 pb-3">
          <h2>
              Total: $200
          </h2> 
      </div>

      <div class="table-container">
          <table class="table-striped w-100">
              <tbody>
                  <tr>
                      <td>1</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>2</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>3</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>4</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>5</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>6</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>7</td>
                      <td>:</td>
                     <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>8</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>9</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
                  <tr>
                      <td>10</td>
                      <td>:</td>
                      <td>Lorem Ipsum</td>
                  </tr>
              </tbody>
          </table>
      </div>
      <button id="expand-collapse-btn">expand &darr;</button>
  </div>

Your assistance in tackling this challenge would be greatly appreciated. Thank you!

Answer №1

To ensure the "expand" button stays in place, consider adding overflow-y: scroll; to the .data-container element. This will keep the button stationary even when the container is scrolled. Alternatively, you can apply height constraints and scrolling to the .table-container instead.

Answer №2

Your script has been slightly modified and now it's working correctly.

Additionally, we have included the overflow: scroll CSS property to your data-container.

const expandCollapseBtn = $('#expand-collapse-btn');

$(expandCollapseBtn).click((e) => {
  const divHeight = $(".data-container").height();
  if (divHeight >= 400) {
    $(".data-container").animate({
      "height": "200px"
    }, 100);
  } else if (divHeight <= 200) {
    $(".data-container").animate({
      "height": "400px"
    }, 100);
  }
});
.data-container {
  min-height: 45px;
  background: #fff;
  -webkit-box-shadow: 0 1px 5px 0 rgba(0, 0, 0, .2);
  -moz-box-shadow: 0 1px 5px 0 rgba(0, 0, 0, .2);
  box-shadow: 0 1px 5px 0 rgba(0, 0, 0, .2);
  position: relative;
  overflow: scroll;
}

.data-container .bg-light {
  font-size: 1.1rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.125);
  border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
  font-weight: 700;
}

.data-container h2 {
  font-size: 18px;
  margin: 0;
  padding: 0;
}

section .data-container .table-container .table-striped>tbody>tr:nth-child(2n+1)>td {
  background-color: #f8f9fa;
  line-height: 15px;
}

.data-container .table-container table tr td:first-child {
  padding-left: 50px;
  width: 49%;
  text-align: right;
}

.data-container .table-container table tr td:nth-child(2) {
  padding: 5px 0 5px 0;
  width: 2%;
}

.data-container .table-container table tr td:last-child {
  padding-left: 6px;
  width: 49%;
}

.data-container .table-container table td {
  padding: 5px 18px 5px 20px;
}

.data-container .table-container a {
  font-size: 10px;
  color: red;
  text-decoration: underline;
}

.data-container .table-container tr {
  height: 45px;
}

#flag-image {
  width: 25px;
  height: 25px;
}

.data-container button {
  background-color: transparent;
  color: #343a40;
  border: none;
  outline: none;
  position: absolute;
  left: 50%;
  border-bottom-style: dotted;
  transform: translateX(-50%);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data-container w-75 m-auto">
  <div class="card-header bg-light text-center pt-3 pb-3">
    <h2>
      Total: $200
    </h2>
  </div>

  <div class="table-container">
    <table class="table-striped w-100">
      <tbody>
        <tr>
          <td>1</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>2</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>3</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>4</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>5</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>6</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>7</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>8</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>9</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
        <tr>
          <td>10</td>
          <td>:</td>
          <td>Lorem Ipsum</td>
        </tr>
      </tbody>
    </table>
  </div>
  <button id="expand-collapse-btn">expand &darr;</button>
</div>

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

Display a div element on the page after 10 seconds of loading

Recently, I managed to get this script working successfully where it fades out within 20 seconds. However, I am looking to modify it so that it takes only 10 seconds to display after the page loads. Any suggestions on what I should change in the code? jQu ...

Ensure that all lists are aligned on the left side, from the numbers to

Trying to implement a solution for aligning numbers and text in an ordered list within a border. Found a helpful answer on Stack Overflow: Left align both list numbers and text Encountering an issue where long content in one list item overlaps into the n ...

Incorporating Distinct Items into an Array with JavaScript

There is a Filter object that stores information about different Car Types. The data is fetched via AJAX calls - on the first call, objects 0-10 are created and added to an array. Subsequent calls bring more car types which are also appended to the array. ...

Steps for incorporating Bootstrap 5 pagination without using jQuery

Currently, I am working on incorporating pagination for cards using Bootstrap 5. My reference point is the following link: https://getbootstrap.com/docs/5.0/components/pagination/ While I can find the UI elements, I am struggling to come across a concrete ...

The functionality of Google Maps API 3 seems to be glitchy

Looking for assistance on how to integrate a Google map into a jQueryUI tab. Although the map appears, it does not completely fill its designated space. Additionally, attempting to scroll within the map results in jittery behavior. Any suggestions or tip ...

jQuery struggles to locate the active class within the Bootstrap slider

Want to make some changes to the Bootstrap slider? Here is the HTML Code and jQuery: <div id="carousel-slider2" class="carousel slide bs-docs-carousel-example"> <ol class="carousel-indicators"> & ...

Only one condition is met when using the Javascript if statement with the :checked and .bind methods

I need help creating an Override Button to disable auto-complete for a form using Javascript. I want the div "manualOverrideWarning" to display a warning if the button is selected, but my current function only works the first time the user presses the butt ...

The add method in jQuery allows you to combine multiple elements

I am currently developing a jQuery plugin that showcases images with a specific class in the middle of the screen. When clicked, the image expands to full size and is surrounded by a gray overlay. Clicking on the overlay should make it disappear. Currently ...

Scroll through the menu with ease

I am facing an issue with my menu that has 2 levels. Whenever I try to scroll down, I want the position of the Logo to change to the bottom of the menu smoothly. However, during scrolling, there is a noticeable shake in the menu movement which makes it app ...

What is the best way to link a Javascript routes file in an AngularJS file on the client side within a node application?

When I use require or import in my Angular file, it appears to cause the controllers to be disabled. I am trying to access data from a route and show a portion of that data on the view using angular binding. For instance, I want to display the username re ...

What to do when encountering a problem with HTML, CSS, and JS while loading a webpage from an SD card to a WebView

I am facing an issue while loading an HTML file from the SD card to a webview. The problem is that the CSS, images, and videos are not loading properly in the webview. Compatible with Android 4.4 KitKat and Chrome version Not compatible with versions belo ...

What is the distinction between selecting and entering a date input value?

When a user selects a date, it needs to be immediately sent to the server. If they manually type in the date, it should be sent on blur. The issue arises when the oninput event is triggered for each keydown event, causing unnecessary server requests while ...

Angular input range slider that automatically rounds decimal values from the data bindings

I've implemented a range slider within an Angular form to capture and display recorded values. <input [formControlName]="object.id" [id]="object.id" type="range" [(ngModel)]="object.answer" [step]="objec ...

Getting a URL path in Next.js without relying on the Link component when the basePath is configured

Base Path from the next.js documentation states: For instance, by setting basePath to /docs, /about will automatically transform into /docs/about. export default function HomePage() { return ( <> <Link href="/about"> ...

What is the reason for the malfunction of this JavaScript code designed for a simple canvas operation?

I'm having trouble with the code below. It's supposed to display a black box on the screen, but for some reason it's not working properly. The page is titled Chatroom so at least that part seems to be correct... <html> <head> &l ...

Merging SCSS and CSS into a unified file using WebPack

Trying to grasp webpack as a beginner is proving to be quite challenging for me. I'm struggling with the concept of merging multiple scss and css files together using webpack, after transpiling the sass. Unlike gulp, where I could easily transpile sa ...

The error message "TypeError: Router.use() expects a middleware function, but received a [type of function]" is a common occurrence in FeathersJS

Struggling to bind a method to my /userAuthenticationInfo route, I've made several adjustments in my code based on other posts related to this issue but still unable to make it work. I am using feathersJS's express implementation, and even with e ...

The grid is experiencing improper sizing in the row orientation

Challenges with Grid Layout In my current project, I am facing a challenge in creating a responsive layout with a dynamic grid within a page structure that includes a header and footer. The main issue arises when the grid items, designed to resemble books ...

Tips for implementing dynamic image paths in Next.js

As a newcomer to ReactJS and working with Next.js, I am currently fetching images from a database using an API. The issue I am facing is that I am receiving the image name with the full path (url.com/imagename), but I want to be able to use a dynamic pat ...

Struggling to save a signature created with an HTML5 Canvas to the database

I've been on the hunt for a reliable signature capture script that can save signatures to MySQL, and I finally found one that fits the bill. However, there are two issues that need addressing: The canvas doesn't clear the signature when the c ...