What are some solutions to address sluggish rendering when adding content to a container?

I have implemented a jquery datatable and noticed that there is a slow rendering issue when adding toolbar items to the top of the table container.

I attempted to improve the performance by using jQuery to append the items during the preInit event. However, this method did not significantly impact the render speed:

       $("#escalation").one("preInit.dt", function (evtObj, settings) {

            var $newbtncontainer = $("<div class='dt-buttons'></div>");
            $("#escalation_wrapper").prepend($newbtncontainer);
            $("a.buttons-colvis").appendTo($newbtncontainer);
            $("#tblDateRange").appendTo($newbtncontainer);
        });

The table itself loads relatively quickly but the process of adding toolbar items remains sluggish.

Question:

How can I address the slow rendering issue when appending items to the datatable container?

Below is the markup for the datatable and the associated jQuery for appending toolbar items:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-daterangepicker/2.1.24/daterangepicker.min.css" />

            <div class="table-responsive max-width-table" id="datatable-wrapper">
                <style>
                    #escalation tr > *:nth-child(1) {
                        display: none;
                    }
                </style>

                <div class="form-group form-inline pull-left" id="colFilter"></div>

                <div class="form-group form-inline pull-right" id="tblDateRange">
                    <label id="dateRangeLbl" class="date-range-label">
                        <span class="glyphicon glyphicon-calendar"></span>
                        <input class="input-sm top-buffer pull-right date-range-input" id="dateRangeInput" type="text" name="daterange" value="01/01/2016 - 12/25/2016" />
                    </label>
                </div>

                <div class="btn-group pull-right">

                    <button id="scrollXLeft-btn"><span class="glyphicon glyphicon-arrow-left"></span> </button>
                    <button id="scrollXRight-btn"><span class="glyphicon glyphicon-arrow-right"></span> </button>

                </div>



                <table id="escalation" class="table table-striped table-bordered" cellspacing="0">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Fruit</th>
                        </tr>
                    </thead>
                    <tbody>
                       <tr>
                          <td>1</td>
                          <td>Apple</td>      
                       </tr>
                    </tbody>
                </table>

    </div>





<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.flash.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.colVis.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>



<script>


    //Once the Document DOM is ready..
    $(document).ready(function () {

        $("#scrollXRight-btn").click(function () {

            var leftPos = $('div.dataTables_scrollBody').scrollLeft();
            $("div.dataTables_scrollBody").animate({ scrollLeft: leftPos + 800 }, 800);

        });

        $("#scrollXLeft-btn").click(function () {

            var leftPos = $('div.dataTables_scrollBody').scrollLeft();
            $("div.dataTables_scrollBody").animate({ scrollLeft: leftPos - 800 }, 800);

        });


        $("#escalation").one("preInit.dt", function (evtObj, settings) {

            var $newbtncontainer = $("<div class='dt-buttons'></div>");
            $("#escalation_wrapper").prepend($newbtncontainer);
            $("a.buttons-colvis").appendTo($newbtncontainer);
            $("#tblDateRange").appendTo($newbtncontainer);
        });


        var historyTable = $('#escalation').DataTable({
            "order": [[1, "desc"]],
            colReorder: true,
            scrollY: 1000,
            scrollX: "100%",

        });



    });



</script>

Answer №1

 $("#escalation").one("preInit.dt", function (evtObj, settings) {

        $("#escalation_wrapper").prepend( "<div class='dt-buttons'></div>"  );
        $("a.buttons-colvis").appendTo("<div class='dt-buttons'></div>");
        $("#tblDateRange").appendTo("<div class='dt-buttons'></div>" );
    });

Modifying the code in this way reduces unnecessary processing because you are no longer creating a separate DOM object before appending and prepending to other controls. Using the preInt.dt method allows for efficient addition of elements without impacting performance.

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

Utilize Bootstrap accordions nesting inner accordions to create a dynamic user interface with varying shown.bs.c

I am currently dealing with a nested bootstrap accordion issue. The problem arises when I click on the inner accordion - it unexpectedly triggers the 'shown.bs.collapse' event bound to the outer accordion, which is not the intended behavior. Is t ...

Struggling to decide on the perfect CSS selector for my puppeteer script

I am trying to use puppeteer page.type with a CSS selector. <div class="preloader"> <div class="cssload-speeding-wheel"></div> </div> <section id="wrapper" class="login-register"> <div class="login-box"> <d ...

I am struggling to move my dropdown list in HTML and CSS

I'm having trouble adjusting the position of a dropdown list on my website using CSS. Can someone help me move the dropdown list to the center? HTML Code: <header> <h1 id="my_cycle_head">MY CYCLE</h1> <ul id= ...

When I'm working on iPhone css hover effects, I find that I need to include a

Why do I have to add a touch event to my element in order for it to trigger hover styles? Is this typical behavior? It seems a bit unconventional. This is the code I need to include to make :hover work; without it, no hover style is applied. Apologies f ...

Concerns with JQuery UI autocomplete: Limited search results available

I'm in the process of developing a search engine using JQuery UI autocomplete feature. The data is stored in a mysql database. While the search query does return some results, it fails to generate a satisfactory number of matches. When I run a search ...

Arrange an array of strings in which the elements follow the format "Name:Score"

I am currently working with a string array that is stored in LocalStorage. Within this array, each element follows the format "Name:Score". My goal is to sort this array by score in order to create a leaderboard. How can I efficiently achieve this? Perhap ...

Adjusting the dimensions of an image based on the size of the device screen

Is there a way to show my image on different devices in full screen automatically adjusting to fit the display? I attempted using the "Width: Device-width" option but it didn't work. Any suggestions? ...

What is the best way to send a parameter from JavaScript to a UserControl?

Within my ASP.Net application, I have a UserControl named EmployeesUserControl that is displayed within a JQuery modal dialog. Prior to displaying the Employees, I need to provide a filter criteria indicating which entity the employee belongs to. My query ...

How can I use Python Selenium to replicate the styles of elements in Chrome?

Looking at this image, we can manually extract styles using copy > copy styles from Chrome. But is there a way to achieve this using Selenium? https://i.sstatic.net/3XzsT.png Although this guide explains how to obtain the CSS value of a specific eleme ...

Modifying the CSS display property in Javascript following a media query update

Seeking a solution for a table that needs to be visible on desktop but hidden on mobile, with the option of clicking a button to toggle its visibility. Currently, the javascript function close_table() sets display:none which overrides the CSS display:block ...

What is the reason for the malfunction of the top input function?

In Section 1, if I select checkbox bbbbbb or checkbox cccccc, the input will be disabled and it doesn't work. However, in Section 2, everything is functioning correctly. Why isn't the functionality working in Section 1? Thank you. Section 1 & ...

Maintain Open State of Toggle Drop Down Menu

Having an issue with the toggle down menu or list on my webpage. The problem is that the menu is constantly open (the #text_box) when the page loads. My intention was for it to be closed initially, and then open only when the user clicks on the 'Ope ...

Creating a CSS grid layout with dual columns to dynamically adjust and accommodate two items in each row

I am attempting to create this layout using CSS grid: https://i.sstatic.net/Ktbnm.png The goal is to have an input and a textarea. The textarea should be of the size of 2 inputs. If there is already a textarea in the previous column, the next column shou ...

Changing the background color of a div after a mouse click

Recently, I attempted to change the background color of a div after clicking it using only CSS. I experimented with :visited, :focus, and .visited classes in CSS, but unfortunately, none of these methods worked. I am wondering if there is a solution to a ...

Stunning Bootstrap 4 landing page components stacking beautifully on one another

I'm relatively new to frontend development and I'm facing a challenge in organizing a layout using Bootstrap 4. My goal is to create a banner for the landing page. I have attached an example of how I want the layout to look, please take a look. B ...

The hover and active effects are malfunctioning

I can't understand why the default color for the "a" element is set to #2bb673 instead of #222. What mistake did I make? I am also using Bootstrap. <div class="our-work"> <a href="#our-work" class="our-work">Our Work</a> ...

Tips for setting the page size while adjusting the browser window dimensions

Is there a way to make a web page size adjust based on the client's monitor resolution? Currently, my webpage looks good when viewed at 1680x1050 resolution. However, if I change the monitor resolution to something smaller, everything on the page gets ...

Is it possible to use just one <map> tag for multiple images in jQuery or JavaScript?

I have multiple images on my website <img usemap="#slideMap_branches" src="branches.png" width="890" height="270" alt="Slide 4"> <img usemap="#slideMap_order" src="order.png" width="890" height="270" alt="Slide 2"> <img usemap="#slideMap_c ...

Toggle form elements on or off depending on dropdown selection

After browsing through various questions, I've managed to disable a textbox by changing the selection in a dropdownlist. However, I am now trying to figure out how to re-enable the textbox when the dropdownlist goes back to its default value of <Se ...

What is the CSS method for altering the color of a slider's runnable track upon selection?

Seeking assistance in changing the slider track color upon selection. Struggling to achieve the desired outcome of altering the color as it slides. CSS: /* Custom Styles */ .text-size-slider { line-height: 100%; font-size: 14px; position: relative ...