A method for determining the length of text lines within a table cell (td) and revealing additional content upon clicking

I want to implement a feature where clicking on "see more" will reveal the full content within a table cell. The criteria for displaying "see more" is when the text inside the table cell spans more than 3 lines, then show the prompt and expand to show the complete content upon click.

The width is currently set at 318px but may vary based on user actions.

To calculate the number of lines, I can set the line-height of the table cell to 21px. However, dealing with an array containing random text elements whose size increases dynamically, potentially reaching thousands or more, makes it inefficient to store the status of each element regarding the number of lines.

Is there a more efficient approach that can be applied universally to all text elements in the array?

Here is the link to stackblitz:- https://stackblitz.com/edit/angular7-test-mndgc5?file=app%2Fapp.component.html

.ts

// The 'textsss' array is dynamic and can have hundreds of elements

textsss = [
"In 2005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica. Time magazine stated that the open-door policy of allowing anyone to edit had made Wikipedia the biggest and possibly the best encyclopedia in the world, and was a testament to the vision of Jimmy Wales.",
 "005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica.",
"abcdffff",
"ished a peer review comparing 42 science articles from Encyclopædia Britannica and Wiki",
.
.
.
.
]

 ngAfterViewInit(){
  this.viewHeight = this.elementView.nativeElement.offsetHeight;
     console.log( this.viewHeight);
    this.lines = Math.round(this.viewHeight/ 21);
     console.log(this.lines);
     if(this.lines > 3){
       this.dynamicTextHeight = 3 * 21;
     }
  }

  expand(){
     this.dynamicTextHeight = this.lines * 21;
  }

.html

<table class="table">
<tbody >
<tr *ngFor="let text of textsss;let i = index;" [style.height.px]="dynamicTextHeight">
<td #mainScreen>{{i + 1}}. <span *ngIf="lines > 3" (click)="expand()">... see more</span>{{text}}</td>
</tr>
</tbody>
</table>

Answer №1

Update:

I have recently made adjustments to my code in order to increase its efficiency. Instead of relying solely on CSS, I have now refactored the handling of each box as a component. The height calculation is now done at the beginning (AfterViewInit) and it is used to determine whether or not to display the extra lines.

To see a demonstration of these changes, you can visit this link: https://stackblitz.com/edit/angular7-test-x7pqvp


If you want to achieve similar results, you can use a combination of CSS and toggling the ngStyle attribute:

  1. Utilize the properties text-overflow along with a fixed height that is calculated based on factors such as font-size, line-height, and the number of lines to show. Using SASS may simplify this calculation process.
  2. Show a "see more/less" option by switching the ngStyle definition accordingly.

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

The HTML autofill pop-up box shifts its position

This autofill function for a text box that I found on is causing some display issues. When typing in a letter, the popup with suggestions shifts the rest of the content below it. How can I resolve this issue? I have attempted to use z-index without succes ...

"Enhance your website with a unique Bootstrap 5 carousel featuring multiple

As a beginner in Bootstrap, I am currently working on an ecommerce template to learn about Bootstrap 5. I am interested in creating a carousel that displays multiple slides at once, like a products slider with left and right arrows. Is this possible in Bo ...

What methods can I use to avoid unnecessary array elements from being generated in Javascript?

When working in PHP, performing the following code: $var = array(); $var[5000] = 1; echo count($var); Will result in the output of 1. However, in JavaScript, missing elements are created. <script type="text/javascript"> var fred = []; f ...

Transforming dynamic class based on state value from React to TypeScript

I'm trying to implement this React function in TypeScript, but I'm encountering errors. const ListItem = ({ text }) => { let [showMore, setShowMore] = useState(false); return ( <div className="item"> ...

Guide on storing images in a designated folder using CodeIgniter

My code is located in view/admin_view2.php <?php echo form_open_multipart('home_admin/createBerita'); ?> <div class="form-group" > <label class="control-label">upload foto</label> <inpu ...

Synchronize one file between numerous applications

I manage a handful of small web applications for a small team at my workplace. These apps share a similar layout and file structure, with many files being identical across all of them. For instance, the index.js file in the src/ directory is consistent in ...

How to Alphabetize an Array of Strings Containing Numbers using TypeScript and Angular

I'm working with an array that looks like this: arr = ["100 abc", "ad", "5 star", "orange"]; The goal is to first sort the strings without numbers at the beginning, then sort the strings with numbers added at t ...

The tooltip content is a little too narrow

I have successfully implemented QTip2 to display a tooltip: The content of the tooltip is generated in the following method: public string GetHours() { DateTime d = Convert.ToDateTime(Request.Form["date"]); Bump b = new Bump(); ...

Unable to modify the .Css file for the VueJs plugin

I've been utilizing the vue-js-modal plugin and inside, there is a style.css file. I attempted to customize the modal style by making changes to this file, but even after saving my modifications and running the application, the modal page still reflec ...

Tips for positioning a highcharts pie chart and legend in the middle of a page

I'm struggling to center my highchart data in a node/react single page application. Currently, it appears off-center like this: https://i.stack.imgur.com/ccR6N.png The chart is floating to the left and I would like to have everything centered within ...

Styling for Print Media: Adjusting the horizontal spacing between inline elements

I have been developing a custom AngularJS point-of-sale (POS) system that requires printing receipts upon completing a sale. To achieve this, I am using ng-print to print out a sales summary displayed within a specific div element after hiding all other un ...

What is the best way to shift the lower section to align with the right side of the rest of the headers and text blocks?

Help needed with moving the Uni section to align it beside other paragraphs and headers. Tried using float left but no luck so far. Any suggestions would be appreciated! Check out the code here <div class ="container"> <div id="education" class ...

Is it possible to send a server response without requiring a callback function to be provided from the client side?

Clarifying the Issue When utilizing .emit() or .send() with a desire to confirm message reception (referred to as acknowledgements), the syntax typically involves: socket.emit('someEvent', payload, callback); The focus of this discussion is on ...

What is the reason tailwind does not take precedence over locally defined styles?

I've been experimenting with changing the default text color using Tailwind CSS, but for some reason, it's not taking effect. I've noticed that Bootstrap is able to override the default style without any issues. I'm fairly new to Tailw ...

Show only half of the Google Charts

I have a code snippet that displays a chart with dimensions of 500x500. However, I only want to show half of the chart, like 500x250. But whenever I adjust the values in the div, it resizes the entire chart instead of just showing half. My goal is to hide ...

Allow users to select options after they click a radio button

I have a pair of radio buttons and two sets of select options within different classes. Upon selecting the first radio button, the select options belonging to class1 should be enabled. On the other hand, upon selecting the second radio button, the select ...

Angular Kendo UI - How to Rotate X-Axis Labels in a Bar Chart

On smaller screens, I am trying to rotate x-axis labels to prevent overlapping. EXAMPLE <kendo-chart *ngIf="!yearLoader" (seriesClick)="barClick($event)"> <kendo-chart-tooltip format="{0} events"></kendo-chart-tooltip> < ...

We are currently unable to identify the specific web framework being used. For further details, please refer to the firebase-debug

I encountered an issue during the deployment process of my Angular application to Firebase Hosting using the firebase deploy command. The error message I received is as follows: [2024-01-31T22:26:15.400Z] --------------------------------------------------- ...

Chrome has trouble correctly displaying SVG when it is inlined in HTML

I generated an SVG file using etree in Python and now I am facing display issues when embedding it into an HTML document. The pure SVG file displays correctly on its own, but once embedded in the HTML, my browser (Chrome) only shows a text field instead of ...

Having trouble with Jquery's Scroll to Div feature?

When I click on the image (class = 'scrollTo'), I want the page to scroll down to the next div (second-container). I've tried searching for a solution but nothing seems to work. Whenever I click, the page just refreshes. Any help would be gr ...