简体   繁体   English

在 MVC 4 脚本部分初始化 jQuery DataTable 时隐藏表数据的最佳方法是什么

[英]What's the best way to Hide the Table data while jQuery DataTable is being initialized in MVC 4 Scripts Section

I would like to know if anyone has the best way to hide all the data that is going to be displayed inside a jQuery DataTable.我想知道是否有人有最好的方法来隐藏将在 jQuery DataTable 中显示的所有数据。 For Instance when the page is loaded - a "data loading" text is displayed or it's blank until the Jquery DataTable is initialized and then all data is displayed inside the DataTable.例如,当页面加载时 - 显示“数据加载”文本或者它是空白的,直到 Jquery DataTable 被初始化,然后所有数据都显示在 DataTable 中。

I am using MVC4 so I am making use of the Scripts Section on the bottom of my screen.我正在使用 MVC4,所以我正在使用屏幕底部的脚本部分。 If anyone has any idea's or something that worked for you...如果有人有任何想法或对你有用的东西......

Thank you!谢谢!

You can do this by hiding the table with style (display:none) and then showing it again after the datatable has finished loading.您可以通过使用样式 (display:none) 隐藏表格,然后在数据表加载完成后再次显示它来实现此目的。 There's an example in this question: jQuery DataTables - Slow initiation, "Normal" html table shown in the beginning在这个问题中有一个例子: jQuery DataTables - 慢启动,“正常” html 表显示在开头

Just putting my solution here for fellow lost souls,只是把我的解决方案放在这里给那些迷失的灵魂,

What I did (with the help of my brother who laughed at my attempt to write jQuery!)我做了什么(在我哥哥的帮助下,他嘲笑我尝试编写 jQuery!)

was to wait for the initialise to be completed as described here : https://datatables.net/reference/option/initComplete等待初始化完成,如下所述: https : //datatables.net/reference/option/initComplete

I added a display:none to my table's style.我在我的表格样式中添加了display:none

<table id="your-table-id" style="display:none">...</table>

then expose it once the init has completed.然后在 init 完成后公开它。

<script>
$('<h2 class="loading text-center">Loading...</h2>').appendTo('body');
  $(document).ready(function () {
    $('#your-table-id').DataTable({
        "initComplete": function( settings, json ) {
            $('h2.loading').remove();
            $(this).show()
        },
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM