【问题标题】:Populate table from array using JQuery使用 JQuery 从数组中填充表
【发布时间】:2015-06-02 02:06:45
【问题描述】:

我有一个包含 16 个元素的数组,我想填充一个表格。我希望它有 2 行,每行有 8 个单元格,其中填充有数组。我的问题是,当表格被填充时,表格将所有元素填充到一行中。我对 JQuery 没有太多经验,我想尝试让它发挥作用。任何帮助表示赞赏!这是我的代码:

//**********Javascript & JQuery**********
var array = [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8];
var count = 0;
var totalCells = 8;

function writeTable() {
    var $table = $('#summaryOfResults');

    //Array always includes enough elements to fill an entire row, which is 8 cells. Outer loop determines how many rows to make.
    //Inner loop determines the elements to print in the cells for that row.
    for (var i = 0; i < array.length / 8; i++) {
        $table.find('#body').append('<tr>');
        for (var j = 0; j < totalCells; j++) {
            $table.append('<td>' + array[count] + '</td>');
            count++;
        }
        $table.append('</tr>');
    }
}

//**********HTML**********
<html>
<head>
</head>
<body>
<div id="resultsTable">
    <table id='summaryOfResults' border='1'>
        <tbody id="body">
            <tr>
                <th>#</th>
                <th>n<sub>i</sub></th>
                <th>n<sub>f</sub></th>
                <th>E<sub>i</sub> (J)</th>
                <th>E<sub>f</sub> (J)</th>
                <th>&Delta;E (J)</th>
                <th>&Delta;E (kJ/mol)</th>
                <th>&lambda; (nm)</th>
            </tr>
        </tbody>
    </table>
</div>
<div id="tableButtons">
    <button id='copyButton' onclick=''>Copy Table</button>
    <button id='clear' onclick='clearTable();'>Clear Table</button>
    <button id='write' onclick='writeTable();'>Write Table</button>
</div>
</body>
</html>

【问题讨论】:

    标签: javascript jquery html arrays populate


    【解决方案1】:

    首先,您必须在每次点击时重置 count
    接下来,您必须指定必须将&lt;td&gt; 元素附加到的确切位置。至于现在,您将它们直接附加到 &lt;table&gt;

    // your declaration of the table element:
    var $table = $('#summaryOfResults');
    // ...
    // then in nested loop, you're appending the cells directly to the table:
    $table.append('<td>' + array[count] + '</td>');
    

    最后一件事 - .append('&lt;/tr&gt;') 不是创建元素对象的正确方法,它应该是 '&lt;tr/&gt;''&lt;tr&gt;&lt;/tr&gt;'


    这应该是你要找的:

    function writeTable() {
        // cache <tbody> element:
        var tbody = $('#body');
        for (var i = 0; i < array.length / 8; i++) {
            // create an <tr> element, append it to the <tbody> and cache it as a variable:
            var tr = $('<tr/>').appendTo(tbody);
            for (var j = 0; j < totalCells; j++) {
                // append <td> elements to previously created <tr> element:
                tr.append('<td>' + array[count] + '</td>');
                count++;
            }
        }
        // reset the count:
        count = 0;
    }
    

    JSFiddle


    或者,创建一个 HTML 字符串并将其附加到循环外的表格中:

    function writeTable() {
        // declare html variable (a string holder):
        var html = '';
        for (var i = 0; i < array.length / 8; i++) {
            // add opening <tr> tag to the string:
            html += '<tr>';
            for (var j = 0; j < totalCells; j++) {
                // add <td> elements to the string:
                html += '<td>' + array[count] + '</td>';
                count++;
            }
            // add closing </tr> tag to the string:
            html += '</tr>';
        }
        //append created html to the table body:
        $('#body').append(html);
        // reset the count:
        count = 0;
    }
    

    JSFiddle

    【讨论】:

    • 谢谢!我需要更多地了解如何在 jquery 中追加,不确定为什么或如何将 TD 元素追加到 。我认为它必须在开始标签和结束标签之间。
    猜你喜欢
    • 2011-03-30
    • 2011-12-01
    • 2016-06-19
    • 2012-09-14
    • 1970-01-01
    • 2019-01-14
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多