簡體   English   中英

使用JQuery從數組填充表

[英]Populate table from array using JQuery

我有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>

首先,您必須重置每次點擊的count
接下來,您必須指定<td>元素的確切附加位置。 到目前為止,您將它們直接附加到<table>

// 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('</tr>')不是創建元素對象的正確方法,它應該是'<tr/>''<tr></tr>'


這應該是您要尋找的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM