根据条件在动态表中显示数组值

时间:2014-09-04 15:25:38

标签: javascript html arrays html-table

在HTML中,我有一个数组,我想根据条件在表中显示数组值, 数组有“No Cust”和其他值。我想只显示其他值。数组大小可能会动态更改。所以我尝试了下面的代码。它正在创建表但不显示文本值,请帮助

<HTML>
    <HEAD>
        <TITLE>Sample Page that Writes Out an HTML Table</TITLE>
    </HEAD>
    <BODY>
        <SCRIPT Language="JavaScript">
            var text = ["No Cust", "No Cust", "20 cust", "No Cust", "30 Cust", "80 cust", "50 cust"];
            count=0;
            var text2 = "";

            for (i=0; i<7; i++) {
                if (text[i] == "No Cust") {
                    continue;
                }
                else {
                    text2[count] = text[i];
                    count = count + 1;
                }
            }

            document.write('<table border="1" cellspacing="1" cellpadding="5">')

            for(j = 0; j < count; j++) {
                document.write('<tr>')
                document.write('<td> text2[j] </td>')
                document.write('</tr>')
            }

            document.write('</table>')
        </SCRIPT>
    </BODY>
</HTML>

5 个答案:

答案 0 :(得分:1)

您的脚本中存在多个错误。首先,语法不正确,正如其他人所说。你不需要修复数组的长度。您还在过滤器中不必要地匹配“No Cust”。您正在多次写入DOM,这非常昂贵。试试这个:

var text = ["No Cust", "No Cust", "20 cust", "No Cust", "30 Cust", "80 cust", "50 cust"],
    text2 = [],
    i;


for (i = 0; i < text.length; i++) {
    if (text[i] !== "No Cust") {
        text2.push(text[i]);
    }
}

var table = '<table border="1" cellspacing="1" cellpadding="5">';

for (i = 0; i < text2.length; i++) {
    table += '<tr>';
    table += '<td>' + text2[i] + '</td>';
    table += '</tr>';
}

table += '</table>';

document.getElementById('content').innerHTML = table;

http://jsfiddle.net/7fbq3ps7/

答案 1 :(得分:0)

您的代码中有错误:

document.write('<td> text2[j] </td>')

应该是

document.write('<td>' + text2[j] + '</td>')

答案 2 :(得分:0)

这可以通过单循环实现,如下所示:

    var text = ["No Cust", "No Cust", "20 cust", "No Cust", "30 Cust", "80 cust", "50 cust"];

    document.write('<table border="1" cellspacing="1" cellpadding="5">');
    for (i = 0; i < text.length; i++) {
        if (text[i] == "No Cust") {
            document.write('<tr>');
            document.write('<td>' + text[i] + '</td>');
            document.write('</tr>');
        }
    }
    document.write('</table>');

工作Fiddle

答案 3 :(得分:0)

您正试图将text2 [j]显示为静态文本,方法是将其包含在&#34;&#39;&#34;&#34;(单引号)中。 因此,document.write(&#39;&#39; + text2 [j] +&#39;&#39;)将解决问题

我已经重构了你的for循环以处理数组的动态长度: -

<!DOCTYPE html>
<HTML>
<HEAD>
    <TITLE>Sample Page that Writes Out an HTML Table</TITLE>
</HEAD>
<BODY>
    <SCRIPT Language="JavaScript">
        var text = ["No Cust", "No Cust", "20 cust", "No Cust", "30 Cust", "80 cust", "50 cust"];
        count=0;
        var text2 = new Array();

        for (i=0; i<text.length; i++) {
            if (text[i] == "No Cust") {
                continue;
            }
            else {
                text2[count] = text[i];
                count = count + 1;
            }
        }

        document.write('<table border="1" cellspacing="1" cellpadding="5">')

        for(j = 0; j < count; j++) {
            document.write('<tr>')
            document.write('<td>'+ text2[j] +'</td>')
            document.write('</tr>')
        }

        document.write('</table>')
    </SCRIPT>
</BODY>
</HTML>

如果你想跳过两个循环并使用一个循环,下面是代码:

 <SCRIPT Language="JavaScript">
        var text = ["No Cust", "No Cust", "20 cust", "No Cust", "30 Cust", "80 cust", "50 cust"];
        document.write('<table border="1" cellspacing="1"   cellpadding="5">');
        for (i=0; i<text.length; i++) {
            if (text[i] != "No Cust") {
               document.write('<tr>')
               document.write('<td>'+ text[i] +'</td>')
               document.write('</tr>')
         }
       }          
    </SCRIPT>

答案 4 :(得分:0)

您的代码没问题,但我会更改一些内容:

  1. 不要使用document.write
  2. 为什么不加入数组来创建表? (连接比文​​本连接更快,更短)

  3. var text=['No Cust','No Cust','20 cust','No Cust','30 Cust','80 cust','50 cust'],
        l=text.length,
        c=0,
        A=[];
    for(;c<l;c++){
     text[c]=='No Cust'||A.push(text[c])
    }
    document.body.appendChild(document.createElement('table')).innerHTML=
    '<tbody><tr><td>'+A.join('</td></tr><tr><td>')+'</td></tr></tbody>';
    

    注意:如果它是一张大表,请使用document.createDocumentFragment()

    <强>样本

    http://jsfiddle.net/5tswopt4/

    如果您对代码有疑问,请询问。