简体   繁体   English

用JSON创建动态HTML会花费太多时间

[英]Creating dynamic html with json taking too much time

I am working on preparing some dynamic html with jquery and json object. 我正在使用jquery和json对象准备一些动态html。 but the problem is that when my json object has around 1500 rows it takes ages to load. 但是问题是,当我的json对象有大约1500行时,它需要很长时间才能加载。

is there a way to load the thing faster. 有没有办法更快地加载东西。

Some code. 一些代码。

     $(jQuery.each(jsonObject.AvailableColumns, function (i, l) {
                if (type == "manual") {
                    innerList1 += '<li newText="" valueFormat="' + l.ValueFormat + '" scaleID="' + l.ScaleID + '" scaleType="' + l.ScaleType + '" hasWeights="' + l.HasWeights + '" customColumnType="' + l.CustomColumnType + '" class="" id="li_' + controlId + '"><span id="span_' + controlId + '" title = "' + l.QuestionText + '">' + getDisplayString(l.QuestionText) + '</span><a class="actionLeft"></a></li>';
                }
                else if (type = "exportall") {
                    innerList2 += CreateLiWithSpans('li_' + controlId, l.QuestionText, true, false, l.ScaleID, l.ScaleType, l.HasWeights, l.CustomColumnType, l.ValueFormat);
                }
                controlId++;
            }));
    $("#itemList").html(innerlist1);

EDIT : createliwithspan method 编辑:createliwithspan方法

function CreateLiWithSpans(id, html, isLeft, isAddAll, scaleID, scaleType, hasWeights, customColumnType, valueFormat, newText) {
    var ancClass = isLeft ? 'actionRight' : 'actionLeft';
    var liObject = "";

    if (newText == null) {
        newText = "";
    }
    if (isLeft) {
        liObject = '<li newtext="' + newText + '" valueFormat="' + valueFormat + '" scaleID="' + scaleID + '" scaleType="' + scaleType + '" hasWeights="' + hasWeights + '" customColumnType="' + customColumnType + '" class="" id="' + id + '"><span id="span_' + id + '" title = "' + html + '">' + getDisplayString(html) + '</span><span style="margin:0 10px 0 20px;pagging:0"><input title = "' + (newText == "" ? html : newText) + '" type="text" id="' + id + 'displayText" value="' + (newText == "" ? html : newText) + '"  /><span style="color:Red; width:100%;" id="' + id + 'displayTextError"></span></span><span style="float:left">' + CreateDropDown('ddl_' + id, valueFormat, hasWeights) + '</span><a class="' + ancClass + '"></a></li>';
    }
    else {
        liObject = '<li newtext="' + newText + '" valueFormat="' + valueFormat + '" scaleID="' + scaleID + '" scaleType="' + scaleType + '" hasWeights="' + hasWeights + '" customColumnType="' + customColumnType + '" class="" id="' + id + '"><span id="span_' + id + '" title = "' + html + '">' + getDisplayString(html) + '</span><a class="' + ancClass + '"></a></li>';
    }
    return liObject;
}

You can use for loop instead of jQuery.each, that will be faster. 您可以使用for循环而不是jQuery.each,这样会更快。 Store the itemCount before the loop, and use that: 将itemCount存储在循环之前,并使用它:

itemCount = jsonData.items.length;
for(var i = 0; i < itemCount; i++ ) {
...

You can also use use an array instead of string concatenation, like so: 您还可以使用使用数组代替字符串连接,如下所示:

var innerList = [];
... // inside the loop
innerList.push(CreateLiWithSpans('li_' + controlId, l.QuestionText, true, false, l.ScaleID, l.ScaleType, l.HasWeights, l.CustomColumnType, l.ValueFormat));
... // after the loop
$("#itemList").html(innerList.join(''));

This will be faster in IE, I'm not sure about other js engines. 在IE中这会更快,我不确定其他js引擎。

These two methods will not make a significant difference, so you should try implementing a client side pagination from json. 这两种方法不会产生重大差异,因此您应该尝试从json实现客户端分页。 (Not by hiding and showing divs, by rendering only visible page into the DOM). (不是通过隐藏和显示div,而是仅将可见页面呈现到DOM中)。

Instead of waiting for the loop to end to append your data, why not actively append the data as you process it. 与其等待循环结束添加数据,不如在处理数据时不主动添加数据。 This will allow the user to get immediate feedback instead of waiting for the whole thing to process. 这将使用户能够立即获得反馈,而不必等待整个处理过程。 Other than this, I'd stick with my original comment to page the data. 除此之外,我会坚持原始注释来分页数据。

$(jQuery.each(jsonObject.AvailableColumns, function (i, l) {
                if (type == "manual") {
                    $("#itemList").append( '<li newText="" valueFormat="' + l.ValueFormat + '" scaleID="' + l.ScaleID + '" scaleType="' + l.ScaleType + '" hasWeights="' + l.HasWeights + '" customColumnType="' + l.CustomColumnType + '" class="" id="li_' + controlId + '"><span id="span_' + controlId + '" title = "' + l.QuestionText + '">' + getDisplayString(l.QuestionText) + '</span><a class="actionLeft"></a></li>');
                }
                else if (type = "exportall") {
                    $("#itemList2").append(CreateLiWithSpans('li_' + controlId, l.QuestionText, true, false, l.ScaleID, l.ScaleType, l.HasWeights, l.CustomColumnType, l.ValueFormat));
                }
                controlId++;
            }));

Changing from using jQuery.each to a standard javascript for loop should speed it up a bit. 从使用jQuery.each更改为标准javascript for循环应该可以加快速度。 Make sure that you save the length to a variable like this though: 不过,请确保将长度保存到这样的变量中:

for(var i = 0, len = jsonObject.AvailableColumns.length; i < len; i++){
    var l = jsonObject.AvailableColumns[i];
    // Continue with rest of code
}

Probably won't be a huge increase but every little helps. 可能不会有太大的增长,但是会有所帮助。

Also try lowering the number of function calls you make as these have added overhead (not usually an issue, but in a large loop it can help). 还应尝试减少您进行的函数调用的次数,因为它们增加了开销(通常不成问题,但在较大的循环中会有所帮助)。 Unless the code is shared between functions try doing it inline and see how much that speeds it up. 除非代码在函数之间共享,否则请尝试内联执行并查看可加快执行速度的程度。

  1. Try replacing jQuery.each with a plain old for...in loop. 尝试用普通的for...in循环替换jQuery.each Using jQuery.each adds overhead that you don't need. 使用jQuery.each会增加您不需要的开销。
  2. Don't concatenate strings inside your loop. 不要在循环内连接字符串。 Instead, .push them onto an array variable and use .join('') to build the string all at once at the end. 相反, .push它们到一个数组变量,并使用.join('')以在端部一下子建立的字符串。
  3. You may need to eliminate CreateLiWithSpans as a separate function in order to fully implement (2). 您可能需要消除CreateLiWithSpans作为单独的函数,以完全实现(2)。

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

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