Create an HTML table cell that shows only part of the text with a button labeled "Show more/less" to toggle between displaying or hiding the complete text

Concern: I am dealing with a HTML table that contains multiple columns, with the last column being "Stack Trace" which is often lengthy. The complete display of the stack trace leads to poor table formatting.

Goal: My aim is to showcase the "Stack Trace" as truncated text with a 'Show more/less' button. Upon clicking the 'Show more/less' button for any cell, it should reveal/hide the full "Stack Trace" specific to that cell.

Provided below is my csv file.

DATETIME,HOST,REQUEST,STACK_TRACE
5/13/2022 15:04:05,host1,POST,"java.lang.Exception: Stack trace at java.base/java.lang.Thread.dumpStack(Thread.java:1383) at com.ericgoebelbecker.stacktraces.StackTrace.d(StackTrace.java:23) at com.ericgoebelbecker.stacktraces.StackTrace.c(StackTrace.java:19) at com.ericgoebelbecker.stacktraces.StackTrace.b(StackTrace.java:15) at com.ericgoebelbecker.stacktraces.StackTrace.a(StackTrace.java:11) at com.ericgoebelbecker.stacktraces.StackTrace.main(StackTrace.java:7)"
5/13/2022 15:06:05,host2,POST,"Exception in thread ""main"" java.lang.RuntimeException: A test exception   at com.stackify.stacktrace.StackTraceExample.methodB(StackTraceExample.java:13)   at com.stackify.stacktrace.StackTraceExample.methodA(StackTraceExample.java:9)   at com.stackify.stacktrace.StackTraceExample.main(StackTraceExample.java:5)"
5/13/2022 15:07:05,host5,GET,"java.lang.Throwable: A test exception   at com.stackify.stacktrace.StackElementExample.methodD(StackElementExample.java:23)   at com.stackify.stacktrace.StackElementExample.methodC(StackElementExample.java:15)   at com.stackify.stacktrace.StackElementExampleTest.whenElementOneIsReadUsingThrowable_thenMethodCatchingThrowableIsObtained(StackElementExampleTest.java:34)"

CSS Attempted Solution: -

<head><style>
table{ 
    width:100%; 
    table-layout: fixed; 
    overflow-wrap: break-word; 
} 
th:nth-child(4){ width:60%; }
td:nth-child(4){
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
</style></head>

Findings: The current setup displays a truncated version of the "Stack Trace" without providing a 'Show more/less' option.

https://i.sstatic.net/hZIew.jpg

I require a 'Show more/less' button/link that, when activated, will reveal/hide the complete "Stack Trace" for the respective row cell.

Answer №1

If you're looking to get started, here's a sample code snippet that may give you an idea. Feel free to reach out if you have any questions or need further assistance.

$(document).ready(function(){
  $(".showa").click(function() {
    $("td p.a").css("overflow", "visible");
     $("td p.a").css("white-space", "normal");
  });
  
    $(".hidea").click(function() {
    $("td p.a").css("overflow", "ellipsis");
     $("td p.a").css("white-space", "nowrap");
  });
  
  $(".showb").click(function() {
    $("td p.b").css("overflow", "visible");
     $("td p.b").css("white-space", "normal");
  });
  
    $(".hideb").click(function() {
    $("td p.b").css("overflow", "ellipsis");
     $("td p.b").css("white-space", "nowrap");
  });

});
table{ 
    width:100%; 
    table-layout: fixed; 
    overflow-wrap: break-word; 
} 
th:nth-child(4){ width:60%; }
td:nth-child(4){
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  

}

td:active {
  overflow: visible;
  white-space: normal;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<table colspan="4">
<tr>
 <th>Datetime</th>
 <th>Host</th>
 <th>Request</th>
 <th>Stack_Trace</th>
 </tr>
 <tbody>
 <tr>
 <td>123</td>
 <td>host1</td>
 <td>post</td>
 <td><p class="a">adadasfafsa aSFsfasgagadhsgadhsfjhsfjdgj blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah</p> </td>
 <td><button class="showa">Show</button><button class="hidea"> Hide </button></td>
 </tr>
 
  <tr>
 <td>123</td>
 <td>host1</td>
 <td>post</td>
 <td><p class="b">blah blah  blah blah blah  blah blah blah adadasfafsa aSFsfasgagadhsgadhsfjhsfjdgj blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah blah blah  blah</p> </td>
 <td><button class="showb">Show</button><button class="hideb"> Hide </button></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

Using Firebase with the async pipe in Angular 5

I am struggling to implement async pipe in Angular firebase and encountering an error that I cannot resolve: ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' Utilizing the Firebase NoSQL database: { "bos ...

Excessive vertical space is utilized by the vertical images in the CSS grid image gallery

My responsive CSS grid gallery is experiencing issues with vertical images disrupting the layout, as they appear at full size instead of remaining square like the others. How can I fix this problem? Thank you! Below is the code snippet: <div id=" ...

What is the best way to ensure that bootstrap columns collapse in equal multiples?

I need to have 4 equal columns in each bootstrap row with only specific configurations allowed: All 4 next to each other Two above and two below All in a single column The issue is that when I adjust the screen size, sometimes I get 3 columns at the top ...

Displaying a progress bar during image uploads in PHP without using AJAX

I am in the process of uploading a file on my website and it is working well. However, I would like to display a progress bar while the upload is taking place. I have considered using ajax for this purpose, but I am unable to use ajax. Is there a way to ac ...

Display the tooltip only when the checkbox is disabled in AngularJS

My current setup includes a checkbox that is disabled based on a scope variable in Angular. If the scope variable is true, the checkbox stays disabled. However, if the scope variable is false, the checkbox becomes enabled. I am looking to implement a too ...

Toggle the visibility of a second checkbox based on the checked and unchecked state of an iCheck checkbox

Code snippet for checkbox styling <script> $(function () { //Initialize Select2 Elements $(".select2").select2(); //Applying flat red color scheme for iCheck $('input[type="checkbox"].flat-red, input[type="radio"].flat-r ...

Switching over to styled components from plain CSS using a ternary operator

Currently, I am in the process of converting my project from using regular CSS to styled components (https://styled-components.com/). So far, I have successfully converted all my other components except for one that I'm having trouble with. I've ...

Tips for stopping the mobile keyboard from resizing the viewport

Currently, I am working on a project using React. However, I am facing an issue where the mobile keyboard automatically resizes to fill the viewport when I click on an input field. Is there a way to prevent this from happening? Additionally, upon opening m ...

Is it feasible to incorporate the CSS and Bootstrap (CDNs) specific to an HTML component without affecting the styling of other pages?

One way I am able to recycle the HTML component is using this code: $.get("nav.html", function(data) { $("#fulloptions").replaceWith(data) }) However, there’s a conflict because nav.html uses a different version of Bootstrap than my index file does ...

How to centrally align header elements in Bootstrap 4 on mobile devices?

In this setup using Bootstrap 4, I have a header structure as follows: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="navbar-brand"><img src="..."></div> </div> <div class="col-m ...

Viewport - Presenting a Dynamic Website in Desktop Mode

At first glance, this may seem like a typical and frequently asked question. However, after going through multiple answers, it's clear that many suggest changing the user-agent of a WebView to a desktop string as a solution. Yet, this approach does no ...

Looking to modify and cache external links in a Rails application

v. Rails 2.3.8 How can outbound links in Rails be dynamically modified and cached using fragment caching? I'm eager to hear different approaches without any preconceived notions. Thanks! Note: I have purposely omitted my own thoughts and code to kee ...

Delay in dropping while using React Beautiful DnD

While incorporating react-beautiful-dnd into my project, I faced an issue where the dragged item is initially positioned incorrectly in one of the lists. There is a brief delay before it jumps to its correct position. You can see the problem in action by ...

Paragraph opacity adjustments can alter both the text and background opacities

Is there a way to change the opacity of only the background of a paragraph without affecting the text? When I try to adjust the opacity, it changes both the text and the background. How can I resolve this issue? HTML <div id="opener"> <p id="in ...

Ways to position two buttons in each corner with flexbox in a react js application

I'm fairly new to React JS and have just started building a simple React app. I'm trying to place two buttons on each side using CSS flex, but I'm having trouble achieving it. Can anyone help point out where I might be going wrong? im ...

Unusual problem encountered with Chart.js bar chart, image dispersion

On my HTML page, I have integrated a stacked bar chart using the Chart.js library to visually represent data for users. The data changes based on user selection, and the JavaScript function that enables this onclick feature is: function aggLav(){ [... ...

How can you center a left floated div?

I am seeking to showcase a series of images in a horizontal layout on my page. Beneath each image, there are several links that need to be grouped together within a container. So far, I have tried placing them in floating divs, which align them to the lef ...

Flexbox positioning: Absolute element within a relative container

Having a div using display:flex and position:relative, I am looking to add an overlay element at the bottom of this div by applying position:absolute to it. Ideally, the height of the overlay should be auto, but it's not necessary. The issue arises wh ...

Creating a Dynamic Layout with HTML and CSS: Effortlessly Positioning Elements for a Sle

Starting a web-server project recently, my expertise lies in back-end code. I am still learning CSS and HTML, and struggling to position the bank logo and label closer together - "Maze Bank of Los-Santos." Despite researching extensively, I find it challen ...

Utilizing European letters in HTML code

Just recently, I began learning HTML + CSS and I've encountered a challenge. Being European, I frequently need to use special characters like á, ã, ç, etc. Is there a more efficient way to include these characters without having to use the correspo ...