Animation that allows for smooth sliding of sub rows within a table

I am trying to implement a sliding animation for sub rows within a table structure. However, I have noticed that when using the div tag within a table, the animation does not work as expected compared to using the div outside of the table structure. The animation seems to be missing and it is formatting every sub td in the first td of the parent row.

You can find the source code for this issue in the following StackBlitz example

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr (click)="show = !show">
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>

<ng-container *ngIf="show">
  <div class="box" [class.opened]="show">
  <tr>
    <td>Sam</td>
    <td>Sample</td> 
    <td>35</td>
  </tr>
  <tr>
  <tr>
    <td>Piet</td>
    <td>Miller</td> 
    <td>42</td>
  </tr>

  </div>

</ng-container>

  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>


<div style="margin-top: 20px" class="box" [class.opened]="show">
    Here the animation is working proper.  <br> <br>

    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Neque ornare aenean euismod elementum nisi quis eleifend. Vivamus at augue eget arcu dictum. Dui sapien eget mi proin sed libero enim sed faucibus. Justo laoreet sit amet cursus sit amet dictum sit. Varius sit amet mattis vulputate. Lorem ipsum dolor sit amet consectetur adipiscing elit. Convallis tellus id interdum velit laoreet id donec ultrices. Tempus urna et pharetra pharetra massa massa. Tempor commodo ullamcorper a lacus vestibulum sed arcu non odio. Sagittis eu volutpat odio facilisis mauris sit amet massa vitae. Semper auctor neque vitae tempus quam pellentesque nec nam. Mauris nunc congue nisi vitae suscipit tellus mauris. Eget magna fermentum iaculis eu. Mi in nulla posuere sollicitudin. Nibh mauris cursus mattis molestie a iaculis at erat pellentesque. Nam libero justo laoreet sit amet. Aliquam faucibus purus in massa. Velit ut tortor pretium viverra suspendisse potenti nullam ac.
</div>

CSS

 .box {
        background-color: #FFCC55;
        max-height: 0px;
        overflow-y: hidden;
        transition: ease-in-out 400ms max-height;
    }

    .box.opened {
        max-height: 500px;
        transition: ease-in-out 600ms max-height;
    }

Answer №1

To fix the issue, all you need to do is remove the 'ng-container' tag from your code.

Here is the code before:

   <ng-container *ngIf="show">
    <div class="box" [class.opened]="show">
      <tr>
        <td><div>Sam</div></td>
        <td><div>Sample</div></td>
        <td><div>35</div></td>
      </tr>
      <tr></tr>
      <tr>
        <td><div>Piet</div></td>
        <td><div>Miller</div></td>
        <td><div>42</div></td>
      </tr>
    </div>
  </ng-container>

And here is the updated code:

    <div class="box" [class.opened]="show">
      <tr>
        <td><div>Sam</div></td>
        <td><div>Sample</div></td>
        <td><div>35</div></td>
      </tr>
      <tr></tr>
      <tr>
        <td><div>Piet</div></td>
        <td><div>Miller</div></td>
        <td><div>42</div></td>
      </tr>
    </div>

If you still want to use 'ng-container', place it inside the div with the class box. However, keep in mind that this may slow down your animation.

Answer №2

<table style="width:100%">
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
    </tr>
    <tr (click)="show = !show">
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
    </tr>
    <ng-container *ngIf="show">
        <tr class="box" [class.opened]="show">
            <td><div>Sam</div></td>
            <td><div>Sample</div></td>
            <td><div>35</div></td>
        </tr>
        <tr>
            <td><div>Piet</div></td>
            <td><div>Miller</div></td>
            <td><div>42</div></td>
        </tr>
    </ng-container>
    <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
    </tr>
</table>

Here is a table without the wrapping div!

<div style="margin-top: 20px" class="box" [class.opened]="show">
    The animation works well here.  <br> <br>
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Neque ornare aenean euismod elementum nisi quis eleifend. Vivamus at augue eget arcu dictum. Dui sapien eget mi proin sed libero enim sed faucibus. Justo laoreet sit amet cursus sit amet dictum sit. Varius sit amet mattis vulputate. Lorem ipsum dolor sit amet consectetur adipiscing elit. Convallis tellus id interdum velit laoreet id donec ultrices. Tempus urna et pharetra pharetra massa massa. Tempor commodo ullamcorper a lacus vestibulum sed arcu non odio. Sagittis eu volutpat odio facilisis mauris sit amet massa vitae. Semper auctor neque vitae tempus quam pellentesque nec nam. Mauris nunc congue nisi vitae suscipit tellus mauris. Eget magna fermentum iaculis eu. Mi in nulla posuere sollicitudin. Nibh mauris cursus mattis molestie a iaculis at erat pellentesque. Nam libero justo laoreet sit amet. Aliquam faucibus purus in massa. Velit ut tortor pretium viverra suspendisse potenti nullam ac.
</div>

well removed the div from ur table!

Answer №3

I don't have a complete understanding of how angular operates, but I can show you the proper method for implementing a collapsible row. Simply include another row and a <td colspan="100">, allowing this cell to stretch across the entire table width. Place your content inside this section:

<tr>
  <td colspan="100">
    <div class="container">
      insert your markup here
    </div>
  </td>
</tr>

Answer №4

To get a comprehensive guide on how to create an expandable tableview, please visit the following URL:

https://datatables.net/blog/2014-10-02

Custom Script

/* Customize this function to format row details as needed */
function customFormat ( data ) {
    // 'data' represents the original data object for the row
    return '<div class="slider">'+
        '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
            '<tr>'+
                '<td>Full Name:</td>'+
                '<td>'+data.name+'</td>'+
            '</tr>'+
            '<tr>'+
                '<td>Extension Number:</td>'+
                '<td>'+data.extn+'</td>'+
            '</tr>'+
            '<tr>'+
                '<td>Extra Info:</td>'+
                '<td>Add any additional details here (such as images)...</td>'+
            '</tr>'+
        '</table>'+
    '</div>';
}

$(document).ready(function() {
    var exampleTable = $('#example').DataTable( {
        "ajax": "/examples/ajax/data/objects.txt",
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

    // Event listener for expanding and collapsing row details
    $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = exampleTable.row( tr );

        if ( row.child.isShown() ) {
            // Close the already open row
            $('div.slider', row.child()).slideUp( function () {
                row.child.hide();
                tr.removeClass('shown');
            } );
        }
        else {
            // Open the selected row
            row.child( customFormat(row.data()), 'no-padding' ).show();
            tr.addClass('shown');

            $('div.slider', row.child()).slideDown();
        }
    } );
} );

Enhanced View Styling

<style>
div.slider {
    display: none;
}
td.details-control {
    background: url('/examples/resources/details_open.png') no-repeat center center;
    cursor: pointer;
}

tr.shown td.details-control {
    background: url('/examples/resources/details_close.png') no-repeat center center;
}

div.slider {
    display: none;
}

table.dataTable tbody td.no-padding {
    padding: 0;
}
</style>

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

Dealing with a vast Angular 12 project - what strategies can I employ to handle JavaScript files

I have a large Angular 12 application that takes a considerable amount of time to load for the first time for any user. The API is not triggered until all JavaScript files are loaded, causing a delay in loading. Upon inspecting the Dev Tools with JavaScr ...

Guide to accessing a menu through Long press or Right click in Angular2

I recently started experimenting with angular 2 and I am trying to figure out how to create a menu that opens with multiple options on both mobile and desktop devices. What I'm looking for is a way to trigger the opening of a menu when a long hold or ...

Using Angular's ng-repeat directive without the need for an HTML element

I need help with a foreach loop in Angular that I am struggling with. Here is what I am trying to achieve: <ul> <li ng-repeat="g in groups"> <li ng-repeat="c in g.commands">{{c.text}}</li> <li class="divider" ...

Adjust selections in dynamic reactive forms with mat-select updates

Is there a way to dynamically update the options within a mat-select element, which is part of a dynamic form in Angular 7? The structure is as follows: <span *ngIf="item !== undefined"> <div [formGroup]="form"> <div [ngSwitch]="item. ...

Creating a customized design for your jQuery UI modal dialog box using CSS

I recently had to customize the jqueryui modal dialog in order to meet the standards set by my company. Currently, I am facing a cross-browser issue with the float and width of the input labels. You can view the sample website here: http://inetwebdesign. ...

Discover the power of jQuery: Incorporating unique dynamic inputs within dynamic divs

I have a piece of PHP code that looks like this: foreach ($questions as $q){ foreach ($answers as $a) { echo '<input type="text" id="'.$a['question_id'].'_'.$a['id_answer'].'" value="'.$ ...

Tips for creating extra space on both sides of a Bootstrap container

I am currently working on creating some extra spacing on the left and right of a Bootstrap container. My current approach involves using a 'fluid' container/section and adding margin on both sides to create the desired space. However, the issue ...

Modify HTML tags based on the sum of <li> elements using jQuery

I have a dynamic slider with items in a list that will change based on the page. My goal is to create a loop that updates the data-slide-to attribute with the correct slide number for each item. After several attempts, I managed to come up with a solutio ...

Sometimes onblur is triggered repeatedly, other times it doesn't fire at all

Currently, I am developing an interactive web application that is accessible at (login: [email protected], password: demo). This application allows users to move items from the left column to the workspace on the right, where they can resize and edit ...

Steps for modifying the [disabled] attribute of an Angular element

I need to update the [disabled] property of the "app-box-ceding-company-definition" object; in the TypeScript file, the property is initially set to true but I want to change its value based on a condition. Condition: if selectrow.status == "01" => dis ...

Deactivate Pagination with a Button-Based Approach

After conducting thorough research online, I have experimented with various approaches but unfortunately, none have proven successful. view image description here I am seeking guidance on how to disable pagination when clicking on the replace button. Due ...

You can tab through form input boxes on an Adobe Muse website, but they cannot be clicked

Lately, I created a small website using Adobe Muse and incorporated their form widget. However, I haven't altered any of the default settings and now the input boxes are not clickable. The only way to interact with them is by tabbing through. How can ...

Error in Angular: Attempting to read the length property of null within a Promise subscribe() operation

Whenever I make a call to an API endpoint, I receive an error in return. However, when I try to view the error using console.log, I encounter the following issue: Is there any alternative approach to retrieving the error message? ERROR TypeError: Canno ...

transforming bootstrap 3 column into bootstrap 2

Is it possible to achieve dynamic column adjustment in Bootstrap 2 like the example shown here? I want the layout to display 4 columns when the window is wide enough, but switch to 2 columns when the width of the window decreases. Since Bootstrap 2 only us ...

Thymeleaf causing Spring IO POST form to redirect to incorrect URL

One of the challenges I'm facing while coding a website is with a form for uploading files. Ideally, when accessing the URL http://localhost:8080/uploadRevision/{docId}, it should lead to http://localhost:8080/uploadRevisionLanding and then redirect b ...

What is the best way to eliminate a selector from Html.TextBoxFor?

Having a slight issue with Html.TextBoxFor. Simply looking to remove the selector. https://i.sstatic.net/Hj6aP.png @Html.TextBoxFor(m => m.StartFund.Funds, new { id = "startFundsTextBox", @class = "k-textbox", @style = "width:20%; display:inline-block ...

Facebook pages that exceed 700px in length without the need for scrollbars

Is there a way to extend the length of a Facebook page without scrollbars when using an iframe, so that it doesn't crop at 700px? ...

The design of the Angular2 bootstrap navbar is very underwhelming

Struggling to implement Bootstrap's navbar in my Angular 2 application, but the navigation items appear distorted: https://i.sstatic.net/x4vlS.png Interestingly, when hovering over any of the items, they display perfectly (as shown with the mouse ov ...

Struggling to implement a Datepicker in the date column of a table created using HTML and JavaScript

I am encountering an issue while creating a table in html and javascript. I am unable to use a Datepicker in the date column when inserting a new row, even though it works fine when using in normal html code. Below is the javascript function: //Script fo ...

What is the best way to utilize CSS 'ids' with server controls in ASP.NET?

I have a dilemma with my designer-created stylesheet that heavily relies on ids. For example: <div id="globalheader"> <ul id="globalnav">.... Here is the CSS: #globalheader { width: 715px; height: 100px; margin: 18px auto; position: absolute ...