What is the best way to restrict the size of a table that is filled with data from a database?

Currently using a combination of React, Node, Express, and Postgres to populate a table with data retrieved from Postgres. The issue arises when the table becomes overly long, prompting the need to display only 5 rows at once while adding a scroll bar for easier navigation.

Below is my React table structure:

return <Fragment>

        <div id="ListContactTable">
        <table className="table mt-5" id="ListContactTable">
            <thead>
                <tr>
                    <th>Supplier Name</th>
                    <th>Contact Name</th>
                    <th>Job Title</th>
                    <th>Edit Entry</th>
                    <th>Delete Entry</th>
                </tr>
            </thead>
            <tbody>
                {contacts.map(contact => (
                    <tr key={contact.scontact_id}>
                        <td>{contact.supplier_name}</td>
                        <td>{contact.scontact_name}</td>
                        <td>{contact.scontact_title}</td>
                        <td><EditContact contact={contact} getContact={getContact}/></td>
                        <td><button className="btn btn-danger" onClick={()=> deleteContact(contact.scontact_id)}>Delete</button></td>
                    </tr>
                ))}           
            </tbody>
        </table>
        </div>
        </Fragment>

Attempts have been made utilizing CSS on both the table and its containing div to constrain the height and incorporate a scrollbar, though unsuccessful so far:

.modal-body{
  overflow-y:scroll; 
  max-height: 300px;
}
#ListContactTable{
  height: 300px;
}
.modal-dialog {
  width: 600px;
}
.modal-footer {
  border-top: 0 none;
}

Seeking advice on effectively limiting the table's display size and integrating a scrollbar:

Solution Incorporating this styling adjustment specifically to the table resolved the issue:

table 
{
    table-layout:fixed;
    width:100%;
}

Answer №1

To simplify things, consider updating your mapping code like this:

{contacts.slice(0,5).map(contact => (
                    <tr key={contact.scontact_id}>
                        <td>{contact.supplier_name}</td>
                        <td>{contact.scontact_name}</td>
                        <td>{contact.scontact_title}</td>
                        <td><EditContact contact={contact} getContact={getContact}/></td>
                        <td><button className="btn btn-danger" onClick={()=> deleteContact(contact.scontact_id)}>Delete</button></td>
                    </tr>
                ))}          

Alternatively, you can go to your node backend and set a limit for your GET controller.

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

Leverage recursion for code optimization

I'm currently working on optimizing a function that retrieves JSON data stored in localStorage using dot notation. The get() function provided below is functional, but it feels verbose and limited in its current state. I believe there's room for ...

Is it possible to display a div when hovering over it and have it remain visible until

How can I make the div ".socialIconsShow" fade in when hovering over ".socialIcons", and then fade out when hovering over ".socialIconsShow"? Is there a way to keep the icons visible even after leaving ".socialIcons"? <div class="socialNetworks"> ...

Toggle the class to slide in or out of the div

I am attempting to create a header with two positions - one absolute and one fixed. I want the header to smoothly slide in as you scroll down, and then slide out when you scroll back to the top. However, I am having trouble getting it to slide; instead, it ...

Enhanced customization of material-ui styles

After exploring various resources on styling overrides, I encountered a unique challenge. I am engaged in crafting styled material-ui components and integrating them across different sections of my application. My objective is to enable the customization o ...

Load a page from a different domain using uframe

Looking for a solution to successfully load an external URI using uFrame. Currently encountering an "Access Denied" issue when attempting to do so on Firefox. Any suggestions? ...

Is there a way to convert HTML into a structured DOM tree while considering its original source location?

I am currently developing a user script that is designed to operate on https://example.net. This script executes fetch requests for HTML documents from https://example.com, with the intention of parsing them into HTML DOM trees. The challenge I face arise ...

transition from mapStateToProps to using hooks

Just dipping my toes into the world of React (hooks) and learning by writing code. I'm grappling with converting MapStateToProps to hooks, specifically stuck on one part just before 'currentItem'. Here's the original code snippet: co ...

What is the best way to set a boolean value for a checkbox in a React project with Typescript?

Currently, I am working on a project involving a to-do list and I am facing an issue with assigning a boolean value to my checkbox. After array mapping my to-dos, the checkbox object displays 'on' when it is unchecked and a 'Synthetic Base E ...

Struggling with deploying to Firebase hosting

When I run firebase deploy, it runs for a few minutes before giving me a timeout error Error: ESOCKETTIMEDOUT I have successfully deployed multiple times before, without making any changes other than the frontend of my React project. My cloud functions i ...

Enhancing the Rendering of React's props.children

I have set up my code like this: // Parent.js class Parent extends React.Component { render() { return { this.props.children } } } // Child.js class Child extends React.Component { render() { let message; const greet = ...

Combining the powers of Javascript and Drupal can create

My current setup includes an "open video" button and a form that triggers an ajax call to render files with the corresponding buttons when users click on handouts or videos. I have encountered a problem: Whenever I click on "open the video" after renderi ...

Is it a usual technique to retrieve dynamic content using PhoneGap?

Recently, I've been utilizing phonegap for creating mobile applications on various platforms. I've noticed the need to use AJAX calls within JavaScript to fetch dynamic content. I'm curious, is it a standard practice in HTML5 apps to retrie ...

Utilizing the controller specified in the template that has been included

Within this snippet of code, I am attempting to utilize a controller named FooCtrl that is defined in the included template app/foo.html, using the directive common.script. angular.module('common.script', []).directive('script', func ...

The color input click event does not work properly when triggered within a context menu event

This may sound a bit complicated, but let me try to explain it clearly. I'm working on a web app where I want users to be able to change the background color of certain divs. I plan to use a color picker interface for this purpose and utilize the con ...

Ways to display a collection of random images with a click of a button?

I've created a simple php webpage that is supposed to display random images from my images folder when a button is clicked. However, I'm facing an issue where no images are showing up and I can't seem to pinpoint the problem in my code. ...

Refreshin the attached DOM to a directive without a page reload

Within a directive, I have implemented some text and a video tag in the code below: app.directive('ngAzuremediaplayer', function () { return { restrict: 'AE', priority: 10, link: function (scope, elem, attr ...

Adjust the height of a div in JQuery to fit new content after specifying a height previously

I have a division element with an initial height of 0 and opacity set to zero, its overflow is hidden, and it contains some content. <div style='height: 0px; opacity: 0px; display: none; overflow: hidden; border: 1px solid #000;' id='myd ...

Ways to evaluate the amount of traffic on a webpage?

Recently, I encountered an issue while creating a page to showcase blog posts. Each post had the typical social media share buttons like "Facebook like," "tweet this post," and "+1." Additionally, there were some extra JavaScript functions added for variou ...

Cease the progress of a Sequelize promise within an Express.js application

Exploring the realm of promises is a new adventure for me, and I'm still trying to grasp their full potential in certain situations. It's refreshing to see Sequelize now supporting promises, as it greatly enhances the readability of my code. One ...

Bring google map markers to life with animation

Is there a way to create an animation like the one shown in this example? I'd like for the map to highlight the corresponding place when a user clicks or hovers over a location in the list. How can I make this happen? I've tried searching on Go ...