How to make a list with a scroll bar in an HTML table cell

Having trouble creating a scrollable list in a dynamically appended HTML table cell using JavaScript? You're not alone. Even after assigning the list to the cell parent, it seems that the list is not behaving as expected - it's not scrollable and the cell size doesn't match what you've set in the CSS. If you can help identify the mistake, I'd greatly appreciate it. Here’s the code snippet.

Essentially, I'm looking for a scrollable list within an HTML table cell.

down vote unaccept You can delete the rows after getting the data from the server

$.getJSON("/data/"+data, function(dataState) {

   $("#data_table tr").remove();
       //...

        for(var data in dataState) {


                var row = document.createElement("tr");

               var list = document.createElement("ul");

                            for(var j = 0; j < data.length; j++) {
                                // Create the list item:
                                var item = document.createElement('li');

                                var dataName = data[j].name;

                                // Set its contents:
                                item.appendChild(document.createTextNode(dataName));

                                // Add it to the list:
                                list.appendChild(item);
                            }

                           // make a list scrollable and add to cell
                            cell.appendChild(list);
                            cell.className = "cellDiv";
                            list.className = "listScrollable";

                tableRef.appendChild(row);
            }

        }

    });

and the CSS:

.cellDiv {
  height: 30px;
  text-align: left;
  vertical-align: top;
  width: 10%;

  font-family: Monaco;
  font-size: larger;

  background-color: whitesmoke;
}

.listSrollable {
  width:20%;
  overflow-y: scroll;
  overflow: hidden;

  font-family: Monaco;

  background-color: white;
}

The issue may lie in conflicting styles between the cell and the scrollable list, but I’m unsure how to resolve it without further assistance.

Answer №1

By setting overflow: hidden, it will take precedence over overflow-y. When using auto instead of scroll, the scrollbar will only be displayed when necessary.

Answer №2

To achieve a scrollable table, use the CSS property overflow-y: scroll (or auto) on the <td> element and set the min-height/width following this specific pattern:

table {
  border-spacing: 5px;
  border: 1px solid grey;
  height: 400px;
  width: 300px;
}
tr {
  min-height: 100px;
}
td {
  border: 1px solid red;
  overflow-y: scroll;
}
ol {
  min-width: 100px;
  height: 100px;
}

Note: By default, a table adjusts its height based on the content it contains. When you need to control the height of a <td>, insert a block-level element inside it and apply the height properties to the block-level element instead.

SNIPPET

table {
  border-spacing: 5px;
  border: 1px solid grey;
  height: 400px;
  width: 300px;
}
tr {
  min-height: 100px;
}
td {
  border: 1px solid red;
  overflow-y: scroll;
}
ol {
  min-width: 100px;
  height: 100px;
}
<table>
  <tbody>
    <tr>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
        </ol>
      </td>
    </tr>
    <tr>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
        </ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
    </tr>
    <tr>
      <td>
        <ol>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
          <li>CONTENT</li>
        </ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
    </tr>
    <tr>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
      <td>
        <ol>&nbsp;</ol>
      </td>
    </tr>
  </tbody>
</table>

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

What steps are needed to design a search bar similar to Zomato's?

I am looking to create a search bar that resembles the one on . I have managed to make a floating search box on top of my background image. However, I am unsure how to align various input fields and buttons side by side. Below is the current CSS code I am ...

Variations in the appearance of scrollbars on the x-axis and y-axis within a single <div> element

Let's say we have a div element like this: <div class="nav-menu"></div> I want to customize the CSS style for scrollbar using the ::scrollbar pseudo-element: .nav-menu::-webkit-scrollbar { width: 8px; background: white; ...

Smartphone screen cluttered with overlapping paragraphs

I am encountering a paragraph overlap issue while using a bootstrap template, specifically on smartphones like iPhone 6 with Safari. Interestingly, the problem does not appear when testing on a browser by shrinking the window size to the minimum. On the ph ...

Choose a specific option from an AngularJS dropdown menu

I am incorporating AngularJs with ASP.NET MVC4. Initially, I am creating a dropdown using razor syntax. @Html.DropDownList("packageName", (IEnumerable<SelectListItem>)ViewBag.packageName, "No Data Available", new { ng_model = "subForm.packageNam ...

What is the best way to dismiss the ant-design-vue select option when the mouse moves away?

<a-select showSearch placeholder="Select a person" optionFilterProp="children" style="width: 200px" :open="isOpen" @mouseenter="handleOpenSelect" @mouseleave="handleCloseSelect" > <a-select-option value="jack"> ...

Is it possible to use the AngularJS function with the <div> HTML element?

I am facing an issue with displaying a modal when clicking on a <div> element. The function assigned to show the modal by changing the CSS property 'display' from 'none' to 'block' works fine when attached to a button, b ...

Is it possible to retract a message on Discord after it has been sent?

I have a code that automatically sends a welcome message when a new member joins the guild, and I want it to be deleted shortly afterwards. Here is my current code: client.on('guildMemberAdd', (member) => { var server = member.guild.id; i ...

Configuring date and time picker options on the fly

I am attempting to dynamically set the options for this datetimepicker plugin. This is what I have in my controller $(document).ready(function () { var today = moment(new Date()).format("YYYY-MM-DD"); var tomorrow = moment().add(1, 'days& ...

Issues with Nuxtjs routing on Netlify

I currently have a NuxtJs (Vue) application deployed on Netlify. The issue I am facing is that when I redirect to other pages, the URL ends up concatenating the last page and the next page. For example, if the current page URL is https://page-example/regi ...

Updating information with AJAX upon clicking a hyperlink

I am trying to implement AJAX for replacing the contents within a div. The setup of the application is quite complex, but I have simplified it to focus on getting the basic concept to work first. Currently, my goal is simply to replace a div based on the ...

Verifying several forms concurrently within a single page

Suppose I have multiple forms on a single page with identical names. How can I efficiently validate each of these forms using just one validation method? <form id="formID"> <input name="nick"> <input type="submit"> </form> <for ...

Having trouble getting background image transitions to work properly in CSS?

I'm encountering an issue with setting the transition CSS property for a hover effect, as it doesn't seem to be working correctly. Here is the relevant HTML: <div class = "menuItemBox" id = "Mughlai"> <p class = ...

Troubleshooting: Issues with importing Less files in TypeScript using Webpack and css-loader

I am currently working on a test project using TypeScript and Webpack. I have an index.ts file and a base.less (or base.css) file imported in the index.ts, but I am experiencing errors with the css-loader. Interestingly, everything works fine when the LESS ...

AngularJS modal behaving oddly when checkboxes are used

My Angular app is available in this plunker. Upon clicking the button, a modal dialog opens displaying a list of items. Two of these items are pre-checked based on conditions set in the checkbox table input. The following function handles pushing and spl ...

Discovering the process of iterating through values from multiple arrays of objects and applying them to a new response in Angular 8

Received the following data from an API response: const apiResponse = { "factoryId": "A_0421", "loss": [ { "lossType": "Planned Stoppage Time", "duration": ...

What are the steps for positioning material-ui icons to the right?

I have a list that is dynamically generated with the following code snippet: <list className = "todos-list"> {allTodos.map((todo, index)=> { return ( ...

Retrieving JSON Data Using JavaScript from a Web Service

After clicking the button in my HTML file to execute the JavaScript, I encounter this error: myapp.azurewebsites.net/:1 Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) Here is the JS code: fun ...

Steps to dynamically modify a class upon clicking

Imagine having a sequence of div elements: <div id='d-1' class='hidden'>this one<br /> <button onclick='next();'>Go to Next</button> </div> <div id='d-2' class='show'&g ...

Using Django to retrieve data from a file uploaded through a website

Looking to create a website that allows users to select a local file (XML/JSON) and then navigate to a Django view to extract data from it. Should I implement javascript for the file selection form and sending it to a specific URL for the Django view? Any ...

AngularJS navigates to specific URL paths instead of only displaying the corresponding HTML pages

Building a simple AngularJS application using the angular-seed starter template consists of: index.html app.js home/home.html home/home.js My objective is to navigate to home.html when clicking on the Home li item with the href="/home". However, the cur ...