What is the best way to adjust the size of table columns dynamically to accommodate content, align them to the left, and set a maximum

I came across many examples that are somewhat close to what I need, but I require more control over the table design. I want to prevent text wrapping, display ellipsis, and provide a tooltip when hovering over the content to view it completely.

This project is based on VueJS, and I'm currently using (or attempting to use) Element UI

Let's get into the specifics. I want my table to be structured like this:

Header A    Header B                     Header C
------------------------------------------------------------------------------- 100%
Apples      Bananas la la la la la la... A box of crackers and some super ri...
Oranges     Mr. Bean Is Cool             More chocolate milk

The example above showcases text-overflow: ellipsis

Alternatively, I aim for a layout like this:

Header A    Header B    Header C
------------------------------------------------------------------------------- 100%
Apples      Bananas     A box of crackers and some super rich chocolate milk
Oranges     Mr. Bean    More chocolate milk

Or possibly like this:

Header A    Header B                      Header C
------------------------------------------------------------------------------- 100%
Apples      Bananas are not my favorite   A box of crackers and some super r...
Oranges     Mr. Bean                      More chocolate milk

Or even like this:

Header A    Header B    Header C
------------------------------------------------------------------------------- 100%
Apples      Bananas     Oranges
Running     Out         Of ideas to put in here

The main emphasis is on maintaining left alignment and avoiding columns that spread responsively. I've experimented with different layouts like table-layout: fixed and width: auto, trying multiple variations with no clear solution.

To complicate matters, the table is dynamically generated from a json source, and the number and content of columns are unknown. Here are some steps I've attempted:

// Determine visible columns
let visibleColumns = _.filter(this.$refs.table.columns, c => {
  return c.label != null && c.label.length;
}).length;

// Calculate max width based on visible columns
let maxWidth;
switch (visibleColumns) {
  case 1:
    maxWidth = 1000;
    break;
  case 2:
    maxWidth = 700;
    break;      
  default:
    maxWidth = 200;
    break;
  }

// Find longest value length in dynamic columns
_.each(cols, c => {
  const comparer = Math.max(...this.data.map(d => String(d[c.field]).length));
});

// Compare max width with column length to determine width

Element UI offers minWidth and width properties but lacks max width control. Despite its auto-tooltip feature, managing the table layout has proven challenging, prompting me to consider building my own table component. I've noticed others facing similar struggles with Element UI's table implementation.

In summary, I need to effectively balance column widths, implement text-overflow, and provide tooltips for partially hidden items. Here's a fiddle illustrating the initial issue I encountered with overflowing content in the last column.

https://jsfiddle.net/xtrvygfq/

Answer №1

Using CSS to tackle the issue at hand.

.cell-overflow {
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  display: -webkit-box;
  -webkit-line-clamp: 1;
}

Your HTML structure

<td v-for="h in colDefs">
    <div class="cell-overflow">{{d[h.field]}}</div>
</td>

Link to JSFiddle example

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

Setting a custom background image in a Bootstrap modal

When users visit my website, I want to enhance their experience by using a background image in my bootstrap modal. Despite several attempts, I have been unable to successfully set a background image in the modal. Can anyone provide guidance on this matter? ...

Encountering the "Variable is not defined" error even when the variable has been previously defined. Currently working with EJS and Node

38| <h2 class="card-activity"> 39| <!-- Display Activity --> >> 40| <%= data.activity %> 41| </h2> 42| <div class="card-info"> 43| ...

Combining two sets of JSON data in JavaScript with JQUERY

I am looking to combine two JSON datasets using JavaScript or jQuery. var object1 = [{ "id": 11, "name": "Vasanth", "username": "Vasanth.Rajendran", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c5a ...

Using NGRX Effects to Load Data for a Specific Item in Angular

On my website, there is a page that displays a range of products from the store managed by a reducer called products. When an action PRODUCTS.LOAD_ALL is dispatched, it triggers an API call through an effect and then sends a PRODUCTS.LOAD_ALL_SUCCESS actio ...

What is the best way to distinguish between various objects that are all in a single line?

After creating an array of objects with data such as name and id, I used the res.json() method to convert it into json format for browser use. However, when I input the array of object data, it looked like this: [ { id: 1, name: 'albany sof ...

bespoke JavaScript confirmation dialogue box

After customizing a confirmation box for the logout feature, I encountered an issue. When the user presses cancel, the box closes and control remains on the same page as expected. However, when the user clicks yes to logout, nothing happens. Could anyone p ...

showing the angular response data in a HTML table layout within a specific div element

I'm currently using the angular-fullstack generator and I've received data in response to a GET request in the form of integer values (1, 2 5, 6 134, 245 236, 567 415, 234 and so on). I'd like to display these values in an HTML t ...

Updating the angular $resource method

I need to customize the query()-method of AngularJS $resource by using a custom $http-GET. However, it doesn't seem to be overriding the operation correctly. The result I am getting is an object with method, data, and headers, instead of data.rows[]. ...

Troubleshooting Issue with Mongoose Virtual Field Population

I am currently facing an issue with my database due to using an outdated backend wrapper (Parse Server). The problem arises when dealing with two collections, Users and Stores, where each user is associated with just one address. const user = { id: &q ...

What is the process for retrieving the user's information following successful social media login?

I have implemented Firebase for authentication in plain JavaScript, without using any frameworks like AngularJS or Angular2. When a user signs up successfully with Google or Facebook, I want to display their name, email, and profile picture. firebase.auth ...

Guide on transmitting information to an API with Vue.js

Here is an example of my API call: this.$http.post("{{ route('shop.checkout.save-order') }}", {'_token': "{{ csrf_token() }}"}) .then(function (response) { if (response.data.success) { if (response.data.redirect_url) { windo ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

Unable to locate the Shadcn error module: Error encountered when trying to resolve the path '@/components/ui/accordion' within the directory 'Desktop/sadcn/src'

I'm encountering an issue with my Shadcn setup in a simple React app with TypeScript. The error message I'm getting is: Module not found: Error: Can't resolve '@/components/ui/accordion' in '/home/vinayak/Desktop/sadcn/src&ap ...

Utilize Javascript or Jquery to intercept and handle both GET and POST requests

Is there a method to effectively intercept and capture both GET and POST requests as they are sent from the browser to the server? In my web application, full page refreshes occur after each request is submitted, however, some pages experience delays in r ...

Delete specific rows by clicking a button in AngularJS

I have a table with checkboxes for each row and I am trying remove the selected rows when a button is clicked. The selected row indexes are stored in an array using ng-change, but I cannot figure out how to delete them all at once with a single button clic ...

Is your idTabs design in need of some sprucing up

Just stumbled upon idTabs and trying to implement a basic one on my server. I've taken the code from the 'Usual' page, included the plugin file and jQuery, but all I'm seeing is... - Tab 1 - Tab 2 - Tab 3 This is tab 1. Seems like it& ...

The event for 'deviceready' in Cordova is not getting triggered when placed inside the angular .run block

Recently, I've been facing difficulties in getting 'deviceready' to register within AngularJS. It was functioning properly earlier, so I'm puzzled about what might have altered. If I trigger 'deviceready' from a global addEve ...

Tips for building a versatile fetch function that can be reused for various JSON formats within a React application

Using the fetch method in various components: fetch(url) .then(result => { if (!result.ok) { throw new Error("HTTP error " + result.status) } return result.json() }) .then(result => { ...

Add a user to a guild using Discord API

Hello, I'm currently working on integrating the Discord API to automatically add users to a guild upon login. However, I keep encountering a 401 Unauthorized error and I'm unsure of the reason behind it. Below is a snippet of my code: const data ...

"An issue with preventing default behavior when clicking on a jQuery element

I need some assistance preventing a JQuery onclick function from scrolling the page to the top. I have tried using preventDefault() without success. You can find the code on this website: Below is the code snippet that I am currently using: $( document ...