使用JavaScript数组填充HTML表格

3
我希望用JavaScript数组填充HTML表格。但是,它没有起作用,我不知道为什么,我的“innerHTML”没有被解释。
我的变量包含正确的值,但当我执行以下操作时:
document.getElementById("title").innerHTML = title;

它没有生效。

这是我的代码:

var title = "";
var link = "";
var date = "";
var image = "";

function get_posts() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "my_url");
xhr.onload = function () {
    if (xhr.responseText == 0) {
        alert("Vous n'avez poster aucun post");

    } else {
        var posts_array = JSON.parse(xhr.responseText);

        for (var i = 0; i < posts_array.length; i++)
        {
            title = posts_array[i][0];
            link = posts_array[i][1];
            date = posts_array[i][2];
            image = posts_array[i][3];

        }
    document.getElementById("title").innerHTML = title;
    }
}
xhr.send();
}

这是我的HTML代码:
<table id="posts">
                <tr>
                    <th id="test">Titre</th>
                    <th>Lien</th>
                    <th>Date</th>
                    <th>Image</th>
                </tr>
                <tr>
                    <td id="title"></td>
                    <td id="link"></td>
                    <td id="date"></td>
                    <td id="image"></td>
                </tr>
            </table>

你能提供你的HTML吗? - Andy Jenkins
2
你检查过xhr.responseText是否确实包含数据了吗?另外,你知道在每次循环posts_array时,你都会用最新的值覆盖标题变量吗?你是想要将它们连接起来吗? - ADyson
首先在循环中弹出标题,以进行检查。 - Arun Prasad E S
3个回答

4
你正在循环内部分配title的值,然后将单元格的innerHTML设置为title。假设你的responseText格式正确,那么posts表格将只包含你的数组中的最后一个元素。看起来你需要为posts_array中的每个项目创建一个新的表行,并将其添加到posts表格中以获得你想要的结果。
例如:
var t = "";
for (var i = 0; i < posts_array.length; i++){
      var tr = "<tr>";
      tr += "<td>"+posts_array[i][0]+"</td>";
      tr += "<td>"+posts_array[i][1]+"</td>";
      tr += "<td>"+posts_array[i][2]+"</td>";
      tr += "<td>"+posts_array[i][3]+"</td>";
      tr += "</tr>";
      t += tr;
}
document.getElementById("posts").innerHTML += t;

1
你的代码有3个错误。
  1. 在每次迭代中,你都覆盖了 titlelinkdateimage
  2. 你只设置了标题,我想你想设置所有数据。
  3. (可能的错误) 你只将一个帖子设置到表格中,可能你想看到它们全部。
从数组创建表格最简单(也是最常见)的方法是构建 HTML 字符串(带有表格标记),并将其附加到表格中。不幸的是,IE 不支持将 html 添加到 table 中,为了解决这个问题,你可以使用 jquery(它会从 html 创建 Elements 并将它们附加)。
示例:
var posts_array = JSON.parse(xhr.responseText);
var columns = ['title', 'link', 'date', 'image']
var table_html = '';
for (var i = 0; i < posts_array.length; i++)
{
    //create html table row
    table_html += '<tr>';
    for( var j = 0; j < columns.length; j++ ){
        //create html table cell, add class to cells to identify columns          
        table_html += '<td class="' +columns[j]+ '" >' + posts_array[i][j] + '</td>'
    }
    table_html += '</tr>'
}
$( "#posts" ).append(  table_html );

另一种方法是使用表格DOM API,这不需要jQuery:
var posts_array = JSON.parse(xhr.responseText);
var columns = ['title', 'link', 'date', 'image']
var table = document.getElementById('posts');

for (var i = 0; i < posts_array.length; i++)
{
    var row = table.insertRow( -1 ); // -1 is insert as last
    for( var j = 0; j < columns.length; j++ ){
        var cell = row.insertCell( - 1 ); // -1 is insert as last
        cell.className = columns[j]; //
        cell.innerHTML = posts_array[i][j]
    }
}

0

对我来说不起作用。

我想将WordPress文章显示在HTML表格中。

我的JS:

function get_posts() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "myUrl");
    xhr.onload = function () {
        if (xhr.responseText == 0) {
            alert("Vous n'avez poster aucun post");

        } else {
            var posts_array = JSON.parse(xhr.responseText);
            var columns = ['title', 'link', 'date', 'image']
            var table_html = '';
            for (var i = 0; i < posts_array.length; i++)
            {
                //create html table row
                table_html += '<tr>';
                for (var j = 0; j < columns.length; j++) {
                    //create html table cell, add class to cells to identify columns          
                    table_html += '<td class="' + columns[j] + '" >' + posts_array[i][j] + '</td>'
                }
                table_html += '</tr>'
            }
        }
        $("#posts").append(table_html);
    }
    xhr.send();
}

我的 HTML:

<table id="posts">
                    <tr>
                        <th id="test">Titre</th>
                        <th>Lien</th>
                        <th>Date</th>
                        <th>Image</th>
                    </tr>
                </table>

我的网络服务(我使用WordPress):

global $current_user;
if(is_user_logged_in){
$current_user = wp_get_current_user();
}

$array = array();
$posts_array = array('author' => $current_user->ID, "post_type" => "alertes", "orderby" => "date", "order" => "DESC", "post_status" => "publish", "posts_per_page" => "10");

$posts = new WP_Query($posts_array);

if($posts->have_posts()){
    while($posts->have_posts()){
        $posts->the_post();

        $post_array = array(get_the_title(), get_the_permalink(), get_the_date(), wp_get_attachment_url(get_post_thumbnail_id()));
        array_push($array, $post_array);

    }
        echo json_encode($array);

}

else {
    echo '0';
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接