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

Issue with module exports not being defined in IE11/Edge

I am experiencing difficulties with an npm module that was updated to ES2015. My application is built in ES2015, bundled by browserify, and compiled with babelify. I am currently trying to upgrade a npm module called credit-card for validation, which has ...

Automatically populate email fields with pre-filled form data

Looking to create an HTML form that utilizes JS/jQuery (no PHP) to capture data from "subject" and "message" fields and send an email. The goal is for the client-side code to open the user's mail client, pre-fill the subject and body with form data, a ...

Create a function that multiplies every element in an array by 2

This example involves some JavaScript code. Currently, I have written the following: var double = function (array) { var newArray = []; for(var i = 0; i<array.length; i++) { newArray.push(array[i]); newArray.push(array[i]); ...

Creating a Jquery method to handle multi-line ellipsis for long text

Check out this fiddle I made: http://jsfiddle.net/mupersan82/JYuSY/ This snippet handles multiline ellipsis: $(function() { var ellipsisText = $('.multilineEllipseText'); var textContainer = $('.incidentCellBottomRight').height(); whi ...

transferring data from grails to javascript via a map

I am facing an issue with passing a Map object from my Grails controller to JavaScript. The code snippet in my controller is as follows: def scoreValue = new HashMap<String,String>(); scoreValue.put("0","poor"); scoreValue.put("1","good"); ... retu ...

Can preloading data from a website impact the accuracy of my google analytics data?

I am interested in creating a simple script that utilizes AJAX to load the content from each page listed in my main navbar into a hidden div on the current page. My goal is to preload important content and cache it on the user's computer to ensure fa ...

Implementing communication between parent and child components: activating setState in a child component

Currently, I am honing my React skills by developing a CV form. Within this form, I have implemented multiple input components that maintain different states ('pending', 'submitted', 'editing') to allow individual editing and ...

The issue at hand involves Javascript, Ajax, latte, and Presenter where there seems to be a restriction on using GET requests for a file located

I have a query. I’ve been given the task of adding a new function to our web application. It was built on PHP by someone else, so it's proving quite challenging for me to debug as I am not familiar with this technology. I’m attempting to incorpor ...

When attempting to debug JavaScript in Edge with Visual Studio Code, an error message stating 'Failed to load source map for chrome-error...' was encountered

Attempting to troubleshoot JavaScript code in Visual Studio Code is resulting in an error: Could not read source map for chrome-error://chromewebdata/: Unexpected 503 response from chrome-error://chromewebdata/edge-elixir-neterror.rollup.js.map: Unsupporte ...

Interact with the button through Swipe Left and Right gestures

Is it possible to trigger a button click using JQuery or JavaScript when swiping left or right on two buttons like these? <button id="right" type="button">SWIPE RIGHT</button> <button id="left" type="button">SWIPE LEFT</button> If ...

Championing React, TypeScript, and TSLint: A Pose of Cur

In my React and TypeScript project, I am utilizing react router dom to dynamically load components from the backend. However, when I import components like "ListData", they are considered unused and removed when I save. How can I keep these components fr ...

Chrome fails to load webfont upon page initialization

Encountering a problem with loading a WebFont specifically on Chrome version "48.0.2564.82 m (64-bit)". The issue arises from the webfont not being applied upon page load or navigation, unless I manually refresh the page. Below is the snippet of code fro ...

Select dropdown in AngularJS for Date formatting

After retrieving json data from a web API, I found that it contains a series of datetimes. My goal is to select a specific datetime from a dropdown list. While I can populate the list without any issues, the formatting appears to be incorrect and I'm ...

Navigating with Breadcrumbs in NextJS

Could anyone provide guidance on creating a breadcrumb for my NextJS application? I am interested in using Ant Design, but the example of the component with React Router is not working for me. Any help would be greatly appreciated! ...

Using jquery to dynamically set the value of a drop down menu

I am struggling with a dropdown list that is being generated dynamically using ajax when the page loads. Additionally, I have a PHP variable that holds the default selected value. However, despite my attempts to select this value, it does not work as expec ...

Encountered an error while attempting to load the resource: the server has issued a status of 405 (Not Allowed), seeking assistance with the nginx

I have a fully functional MERN stack app with AXIOS. While the app works perfectly on my localhost, I face issues when deploying it on nginx as all the POST requests are being denied. Despite trying various solutions found online, none seem to be working. ...

Comparison of Ajax and submit: Why am I not receiving identical responses?

Utilizing Ajax for submission and all global variables ending in box. Note that the initialization code is functioning correctly. function begin() { if (confirm("Proceed at your own risk as all consequences are yours!!")) { var usernameNa ...

Encountering CORS Issue When Accessing Xero Node/Python SDK APIs within a React Application

While trying to fetch data from the Xero node SDK APIs in my React application, I am facing a CORS (Cross-Origin Resource Sharing) error. Even after setting up the necessary headers and permissions on both the client and server ends, the CORS policy remain ...

What's the best way to fix a div to the bottom left corner of the screen?

I have explored various solutions regarding this issue, but none of them seem to meet my specific requirements. What I am trying to achieve is the correct positioning of a div as depicted in the green area in the image. The div located above the green div ...

"I am looking for a way to retrieve dynamic data from a (click) event in Angular. Can

Within my component, I have a video loaded in an i tag on a click event. The challenge is accessing the video ID from the video.component.ts file in order to dynamically load a new video. The solution has been elusive so far. <li *ngFor="let video of c ...