Avoiding the issue of table row height glitch when adding to the DOM

I have a table that I load first, and then I dynamically append tags asynchronously. However, when the tags are appended, the height of the table row seems to shift up. How can I prevent this from happening?

https://i.sstatic.net/Dx3G4.gif

I've attempted to address this issue by using the following CSS:

.portfolio-table tr {
    height: 80px;
}

Here is the structure of my table:

<table class="table portfolio-table">
    <thead class="thin-border-bottom">
        <th width="2%">#</th>
        <th width="28%">Name</th>
        <th width="60%" class="text-left">Tags</th>
        <th width="5%">Edit</th>
        <th width="5%">Delete</th>
    </thead>

    <tbody>

        <tr>

            @foreach ($portfolios as $portfolio)
            <td title="{{ $portfolio->id }}">{{ $portfolio->id }} </td>

            <td>
                <a href="/portfolio/{{ $portfolio->id ?? '' }}/">
                    {{ $portfolio->name }}
                </a>
            </td>

            <td class="text-right" >
                <img src="/assets/fe/img/svg/default.svg" alt="Loading" width="30px">
                <p class="portfolioSkillTags text-left" id="{{ $portfolio->id ?? '' }}"></p>

            </td>


            <td>
                <a  href="/portfolio/{{ $portfolio->id ?? '' }}/edit" type="button" class="btn btn-info btn-sm">
                    Edit
                </a>
            </td>

            <td>
                <a  data-toggle="modal"  data-target="#delete_portfolio_{{ $portfolio->id ?? '' }}" type="button" class="btn btn-danger btn-sm">
                    Delete
                </a>
            </td>

        </tr>

        @endforeach

    </tbody>
</table>

This is how the asynchronous appending operation is handled with AJAX:

$("tbody").each(function(){
    $($(this)).find('tr').each(function(){

        var selector = $(this);
        var id = $(this).find('td:nth-child(1) ').attr('title');
        
        var data = {};
        data.id  = id;

        $.ajax({
            method: 'POST',
            url: '/api/portfolio/' + id + '/skills',
            crossDomain: true,
            contentType: false,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value'),
                "Accept": "application/json",
                "Content-Type": "application/x-www-form-urlencoded",
                "Cache-Control": "no-cache"
            },
            data: data,
            success: function(response){
                console.log(id, response);

                $('p.portfolioSkillTags#'+ id).prev('img').fadeOut();

                for (i = 0; i < response.length; i++) {

                    var name  = response[i]['name'];
                    var color = response[i]['color'];

                    $('p.portfolioSkillTags#'+id).prepend('<span class="badge" style="background-color:' + hexToRgb(color,.2) + ';border:' + hexToRgb(color,.7) + ' 2px solid;">' + name + '</span>').fadeIn('slow');
                }

            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(JSON.stringify(jqXHR));
                console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
            }
        });



    });
});

Answer №1

There appears to be a glitch caused by the loading spinner image, as it is causing text overflow. To resolve this issue, you can apply a CSS style such as floating the image to the right so that it does not wrap the paragraph.

<img src="/assets/fe/img/svg/default.svg" style="float: right;" alt="Loading" width="30px">

Answer №2

The problem lies in the height of the spinner, specifically the svg element. When data is loaded asynchronously, it brings a spinner with a larger height. Once the API call successfully resolves, the spinner element with the larger height disappears. This causes the other data, which can fit in a smaller height, to shrink and create the issue.

There are several ways to fix this, but one simple solution is to ensure that the height (including paddings) of the spinner (svg) element is smaller than the height of the td element. For example:

<img src="/assets/fe/img/svg/default.svg" alt="Loading" width="30px" height="30px">


.portfolio-table tr td {
    height: 50px;
}

UPDATE

Please correct the HTML as well by starting the foreach loop outside the opening tag of the tr HTML element.

@foreach ($portfolios as $portfolio)
<tr>

            <td title="{{ $portfolio->id }}">{{ $portfolio->id }} </td>

            <td>
                <a href="/portfolio/{{ $portfolio->id ?? '' }}/">
                    {{ $portfolio->name }}
                </a>
            </td>

            <td class="text-right" >
                <img src="/assets/fe/img/svg/default.svg" alt="Loading" width="30px">
                <p class="portfolioSkillTags text-left" id="{{ $portfolio->id ?? '' }}"></p>

            </td>


            <td>
                <a  href="/portfolio/{{ $portfolio->id ?? '' }}/edit" type="button" class="btn btn-info btn-sm">
                    Edit
                </a>
            </td>

            <td>
                <a  data-toggle="modal"  data-target="#delete_portfolio_{{ $portfolio->id ?? '' }}" type="button" class="btn btn-danger btn-sm">
                    Delete
                </a>
            </td>

        </tr>

        @endforeach

Answer №3

$('p.portfolioSkillTags#' + id).prev('img').fadeOut();

switch it to

$('p.portfolioSkillTags#' + id).prev('img').hide();

Additionally, please update the HTML by ensuring that the foreach loop begins outside the opening tag of the tr element, as suggested by Prince.

Answer №4

It seems like the issue may be related to a CSS animation. You can try temporarily disabling animations globally to see if that resolves the problem:

* {
    animation: none !important;
}

If this does fix the issue, you can target the specific CSS selector responsible for the animation when loading your data.

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

Center the text within the surrounding box

I am trying to center text next to an inline-block element but using negative margin on the span element changed the layout of the whole content. Can anyone help me with this issue? Here is my code: fieldset { max-width: 350px; margin: 0 auto; ...

Ways to modify the hue of an li element in the context menu upon hovering

I'm currently working on a project that involves VueJS and Bootstrap. To enhance user experience, I've incorporated a context menu using the npm package called vue-context Vue Context Menu When a user hovers over an item on the context menu, the ...

Attempting to reduce the width of the dig when it reaches 400

I have a square element with a click event that increases its size by 50 pixels on each click. However, once it reaches a size of 400 pixels, the size decreases by 50 pixels with every click instead. Additionally, when the size of the square reaches 100 p ...

When trying to click on an HTMLTableRowElement, an Uncaught ReferenceError occurs in React.js and jQuery, stating that the function is

When I was working on my web app, I encountered an issue while trying to create a table. Upon clicking on it, I received an error message: Uncaught ReferenceError: (function) is not defined at HTMLTableRowElement.onclick Here is the code for the table: $( ...

Tap and hold with Zepto

I've been on the hunt for a Zepto plugin that can handle a longClick event. While Zepto already supports longTap, which is perfect for mobile devices, I need something specifically for desktop browsers when a user clicks and holds. It's also impo ...

Tables that adapt to different screen sizes, displaying perfectly on desktop but with slight variations on mobile devices

I recently experimented with a demo to enhance the performance of tables on my website in development. The demo worked flawlessly on my desktop when I resized using a responsive tester extension for Chrome: https://i.stack.imgur.com/KzFiR.jpg However, u ...

Tips for updating button appearance when clicked using React Bootstrap

Is there a way to add custom styling to a toggle button in React? I want the button to change color when selected, but the issue is that the color reverts back to default once the click is released. Can anyone assist me with this? Below is my React Compon ...

Exploring the Depths of Facebox Lightbox with JavaScript

I encountered a strange issue while working on my rails app. I am utilizing jQuery and the Facebox Lightbox for popup forms, and I wanted to incorporate some jQuery validation into the form. Strangely, when I attempted to implement this, I faced unexpected ...

Leveraging PrimeFaces and the p:ajax component, trigger Ajax within an inputText field only when keystrokes lead to changes in the field

I am currently utilizing PrimeFaces and have a p:inputText field that requires updating certain components on the view based on the most recent keystroke within that p:inputText. Below is the code snippet: <p:inputText value="#{customerLController.surn ...

Chrome mobile's justify-content for space-evenly is malfunctioning

Experiencing an issue with the space-evenly value for justify-content on Chrome mobile, while it functions correctly on Desktop and Firefox mobile. A minimal example can be seen here: Example Having a flex container in a row direction aiming to evenly sp ...

Having trouble exporting an object from a different JavaScript file in Node.js

I have been attempting to make the getCurrentSongData function retrieve the songdata object passed in from the scraper. However, I am encountering the following output: ******************TESTING**************** c:\Users\(PATH TO PROJECT FOLDER)& ...

Having trouble with a jQuery.validationEngine reference error?

Despite everything being correctly referenced, I am facing difficulties in getting it to function properly. It's strange because it worked once, but the validation message took 10 seconds to appear. After that, without making any changes, I tried agai ...

Only permit the toggling of one element at a time

Here's a list of movie titles that I've put together. Users with permission can edit these names by holding crtl + left click on the title they wish to change. When the P tag is clicked, it transforms into an input field. Upon hitting enter, an ...

Should private members be kept confidential during program execution?

While Typescript's "private" members may not be truly private at runtime, traditional closures maintain the privacy of their members. Is there value in ensuring that private members remain private during runtime? ...

An application using AJAX that supports multiple languages

I am currently developing a web application that heavily relies on JavaScript and AJAX. One of the requirements for this app is to support multiple languages. For server-side translation, I have implemented a template engine in PHP which handles translati ...

show the attributes of an item contained within an array of objects

Hey there! I'm facing an issue with displaying the content of an object on my HTML page using React. So, here's what's happening: I can access the frontend properties in my object array, but when I try to loop through each element and displa ...

What is the best way to ensure that items have been properly loaded?

I am currently working on a system that involves three distinct item types: courses, packages, and programs. Users have the option to filter by All or by one of these three specific types. However, I am facing a challenge in how to handle the scenario wh ...

Whenever I try to access my Node.js API from Heroku's live server, I encounter a CORS policy error

Whenever I try to access my Node.js API from the Angular app (running on a local setup) and host the API on Heroku's live server, I encounter the following error: CORS policy: No 'Access-Control-Allow-Origin'. Interestingly, when I host the ...

AngularJS controllers and $scope are essential components in structuring and

As a newcomer to Angular, I've spent some time reading up on scopes and controllers, but I still feel like something isn't quite clicking for me. Let's take a look at this code snippet: var myApp = angular.module("myApp", []); myAp ...

Utilize jQuery to locate a specific value within a collapsible Bootstrap 4 table

Is it possible to use a Bootstrap 4 table inside a collapse? I need to search for a specific value in the table and if the value is found, I want to open the collapse and display the row containing that value. <div id="collapseStudents" class="collapse ...