jQuery DataTables - Slow initiation, “Normal” html table shown in the beginning

后端 未结 10 1314
旧时难觅i
旧时难觅i 2021-02-03 22:01

I\'m using jQuery DataTable plugin, but I got a concern where the scripts loading seems to take some time, so my web page is always displaying the ordinary html table first, and

10条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-03 22:53

    I think you should probably just load scripts in the _Layout.cshtml, after all that's what it's for. Partial views are really meant for segments that can be re-used in other areas or at the very least, rendered HTML content.

    That being said, one easy thing to do would be to either hide the table until it's done loading or even hide it and show a progress indicator.

    You could do something like:

    // .loadTable() is some function that loads your table and returns a bool indicating it's finished
    //  just remember to check this bool within the function itself as it will be called over and over until it returns true
    
    while (!loadTable()) {
    
        // maybe show a progress bar
    
        if ($('#myTable').css('display') != 'none')) {
            $('#myTable').hide();    // if it isn't already hidden, hide it
        }  
    }
    
    // hide progress bar
    $('#myTable').show();
    

    UDPATE:

    If the jQuery table plugin has a "success" or "finished" callback, just hide the table on page load and show it when it's done loading.

    $(document).ready(function () {
        $('#myTable').hide();
    
        // run plugin and .show() on success or finished 
    });
    

提交回复
热议问题