Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数完成后显示JavaScript的结果_Javascript_Html - Fatal编程技术网

函数完成后显示JavaScript的结果

函数完成后显示JavaScript的结果,javascript,html,Javascript,Html,形势 下面的脚本将更改表格布局。但是,由于数据量很大,在几秒钟内我可以看到一个压缩的表 问题 如何在运行JavaScript或就绪后显示表。 这把小提琴有少量的数据,所以它没有显示上述问题 内部HTML <head> <script type="text/javascript" src="settable.js"></script> </head> JavaScript *实际数据量由800~1000行组成 $(document).ready

形势
下面的脚本将更改表格布局。但是,由于数据量很大,在几秒钟内我可以看到一个压缩的表

问题
如何在运行JavaScript或就绪后显示表。 这把小提琴有少量的数据,所以它没有显示上述问题

内部HTML

<head>
<script type="text/javascript" src="settable.js"></script>
</head>

JavaScript
*实际数据量由800~1000行组成

$(document).ready(function () {

    var tbody = "#table-body";
    var row = $("#table-body tbody>tr");

    $("<div>", {
        class: "tablewrapper"
    }).insertBefore(tbody);
    $("<table>", {
        class: "header"
    }).appendTo($("<div>", {
        class: "headerwrapper"
    }).appendTo("div.tablewrapper"));
    $(tbody).appendTo($("<div>", {
        class: "bodywrapper"
    }).appendTo("div.tablewrapper"));
    $(tbody + ">thead").clone().val("").appendTo("table.header");
    $("table.header>tr").removeClass("header_hidden");
    $(tbody).find("thead th").empty();
    $(tbody).find("tbody td:nth-child(3)").addClass("lefty");
    $("<input>", {
        type: "text"
    }).attr("id", "search-criteria").appendTo($("<div>", {
        class: "s_box"
    }).insertAfter("div.bodywrapper"));
    $("<div>").attr("id", "count").appendTo("div.s_box");

    resizeTable();
    //var bodyTd = $("#table-body tr td");

    $(window).resize(resizeTable);

    //search function
    $("#search-criteria").on("keyup", function () {
        var keyword = $(this).val().replace(/[A-Za-z0-9]/g, function (td_word) {
            return String.fromCharCode(td_word.charCodeAt(0) - 0xFEE0);
        }).toLowerCase();
        //var row = $("#table-body tbody>tr");

        if (keyword !== "") {
            row.each(function () {

                var td_word = $(this).text().toLowerCase();
                //shorthand if function
                $(this).closest(row)[td_word.indexOf(keyword) !== -1 ? 'show' : 'hide']();
            });
            var srowCount = row.filter(":visible").length;
            document.getElementById('count').innerHTML = srowCount;
            rowCount();
        } else {
            $("tr.s_empty").remove();
            row.show();
            document.getElementById('count').innerHTML = row.length;
        }

        resizeTable();
    });

//    var row = "#table-body tbody>tr";
    var srowCount = row.filter(":visible").length;
    document.getElementById('count').innerHTML = srowCount;

    function rowCount(srowCount) {
        if (srowCount === 0) {
            if (!row.last().hasClass('s_empty')) {
                $("#table-body tbody").last().append('<tr class="s_empty"><td colspan="5" style="text-align:center">Search not found</td></tr>');
            }
            $("tr.s_empty").show();
        } else {
            $("tr.s_empty").remove();
        }
    }

    function resizeTable() {
        //width adjustments
        $("#search-criteria").width($("div.headerwrapper").width() - 30);

        var tbody = $('#table-body');
        var thead = $('table.header');
        var tds = tbody.find('thead th');
        var ths = thead.find('th');
        tbody.width('100%');
        tds.css('width', '');
        thead.width(tbody.width());
        for (var i = 0; i < tds.length; i++) {
            tds.eq(i).css('width', ths.eq(i).outerWidth());
        }
        tbody.width('auto');
    }
});
$(文档).ready(函数(){
var tbody=“#表体”;
var行=$(“#表体tbody>tr”);
$("", {
类:“表格包装器”
}).插入前(t车身);
$("", {
类别:“标题”
}).appendTo($(“”){
课程:“校长说唱”
}).appendTo(“div.tablewrapper”);
$(tbody).appendTo($(“”){
类:“bodywrapper”
}).appendTo(“div.tablewrapper”);
$(tbody+“>thead”).clone().val(“”.appendTo(“table.header”);
$(“table.header>tr”).removeClass(“header\u hidden”);
$(tbody.find(“thead th”).empty();
$(tbody.find(“tbody td:nth child(3)”).addClass(“lefty”);
$("", {
键入:“文本”
}).attr(“id”,“搜索条件”).appendTo($(“”){
类别:“s_盒”
}).insertAfter(“div.bodywrapper”);
$(“”).attr(“id”,“count”).appendTo(“div.s_框”);
可调整大小();
//var bodyTd=$(“#表体tr td”);
$(窗口)。调整大小(可调整大小);
//搜索功能
$(“#搜索条件”)。在(“键控”,函数(){
var关键字=$(this).val().replace(/[A-Za-z0-9]/g、 功能(td_字){
返回字符串.fromCharCode(td_word.charCodeAt(0)-0xFEE0);
}).toLowerCase();
//var行=$(“#表体tbody>tr”);
如果(关键字!==“”){
行。每个(函数(){
var td_word=$(this.text().toLowerCase();
//速记中频功能
$(this).closest(row)[td_word.indexOf(关键字)!==-1?'show':'hide']();
});
var srowCount=row.filter(“:visible”).length;
document.getElementById('count')。innerHTML=srowCount;
行计数();
}否则{
$(“tr.s_empty”).remove();
row.show();
document.getElementById('count').innerHTML=row.length;
}
可调整大小();
});
//var row=“#表体tbody>tr”;
var srowCount=row.filter(“:visible”).length;
document.getElementById('count')。innerHTML=srowCount;
函数行计数(srowCount){
如果(srowCount==0){
如果(!row.last().hasClass('s_empty')){
$(“#表体tbody”).last().append('Search not found');
}
$(“tr.s_empty”).show();
}否则{
$(“tr.s_empty”).remove();
}
}
函数resizeTable(){
//宽度调整
$(“#搜索条件”).width($(“div.headerwrapper”).width()-30);
var tbody=$(“#表体”);
var thead=$('table.header');
var tds=tbody.find('thead th');
var ths=thead.find('th');
t车身宽度('100%);
tds.css('宽度','');
thead.width(tbody.width());
对于(变量i=0;i

非常感谢大家的评论和帮助。

您可以将新表添加到尚未连接到DOM或当前
style=“display:none”
的div中。无论采用哪种方式,用户都将无法看到该表,直到您向其添加行为止

var div = $("<div>", {class: "tablewrapper"});
var table = $("<table>").appendTo(div);
// Add all rows to table
...
div.appendBefore(tbody)
var div=$(“”,{class:“tablewrapper”});
变量表=$(“”)。附录(div);
//将所有行添加到表中
...
附件前分区(T正文)

var div=$(“”,{class:“tablewrapper”,样式:“display:none”});
...
div.css('display','block');
是否说明了您的问题?如果是,是否解决了您的问题

在第一个链接中,我在render和javascript运行之间添加了3秒的延迟

$(document).ready(window.setTimeout(dostuff, 3000));
在第二个链接中,我添加了css规则

#table-body {
    width:100%;
}

#table-body thead {
    height:34px;
    line-height:32px;
    background-color:#1BA7F5;
    color:#FFF;
    width: 100%;
}
当你的javascript运行时,仍然会有一个flash,因为我没有复制你的javascript添加的所有样式


因此,简而言之,在呈现所有内容之前,您的javascript不会运行,如果表格很大,这将需要一些时间。因此,请确保您的样式从一开始就到位,将它们包含在html文档的
标题中的css文件中。

是JSON数据吗?是否允许使用一些插件?如datatables?我不认为它是JSON。只是纯HTML表和带有Jquery库的JavaScript//如果我写插件,我可以使用它。但是如果你建议使用外部的,我可以使用它。谢谢datatable插件的建议。我不太明白你在说什么。我可能在这里说错了。我以为你在哪里通过a获取数据JAX调用并在收到输入后添加到DOM。查看您的代码,延迟可能只是等待表在加载前呈现。我添加了一个不同的答案,并对您的问题进行了不同的假设。谢谢David。虽然我在想除了setTimeout之外是否还有其他简单的方法。但这同样有效,而且很好,你提到了我的css订购。谢谢。
#table-body {
    width:100%;
}

#table-body thead {
    height:34px;
    line-height:32px;
    background-color:#1BA7F5;
    color:#FFF;
    width: 100%;
}