简体   繁体   English

如何使用 json 数据填充表

[英]how to populate a table with json data

i'm working on this calculator and when the user select one of the type a little table should appear to select a type from tha table which should be filled with a json data but doesn't work我正在使用这个计算器,当用户 select 一种类型的小表应该出现在 select 中时,应该用 json 数据填充表格中的一个类型,但不起作用

this my code这是我的代码

document.addEventListener("DOMContentLoaded", get_json_data, false);

function get_json_data() {
    var json_url = 'example.json';
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var data = JSON.parse(this.responseText);
            append_json(data);
        }
    }
    xmlhttp.open("POST", json_url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
}

function append_json(data) {
    var table = document.getElementById('gable');
    data.forEach(function(object) {
        var tbody = document.createElement('tbody');
        tbody.innerHTML =
            '<tr>' +
            '<td > <input type="radio" value ="' + object.RendementDuPanneau + '" name="choice" class="rad" /> </td>' +
            '<td>' + object.Fournisseur + '</td>' +
            '<td>' + object.Modele + '</td>' +
            '<td>' + object.PuissanceMaximale + '</td>' +
            '<td>' + object.RendementDuPanneau + '</td>' + '</tr>';
        table.appendChild(tbody);
    });
    var elements = document.getElementsByTagName('tr');
    for (var i = 0; i < elements.length; i++) {

        (elements)[i].addEventListener("click", function() {
            const rb = this.querySelector('input[name="choice"]');
            rb.checked = true;

            let selectedValue = rb.value;
            alert(selectedValue);
        });
    }
}

json file data look like json 文件数据看起来像

[{"Fournisseur":"Jinko Solar","Modèle":"JKM270PP-60","PuissanceMaximale":270,"RendementDuPanneau":"16,50%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM275PP-60","PuissanceMaximale":275,"RendementDuPanneau":"16,80%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM280PP-60","PuissanceMaximale":280,"RendementDuPanneau":"17,11%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM285PP-60","PuissanceMaximale":285,"RendementDuPanneau":"17,41%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM320PP-72","PuissanceMaximale":320,"RendementDuPanneau":"16,49%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM325PP-72","PuissanceMaximale":325,"RendementDuPanneau":"16,75%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM330PP-72","PuissanceMaximale":330,"RendementDuPanneau":"17,01%"},{"Fournisseur":"Jinko Solar","Modèle":"JKM335PP-72","PuissanceMaximale":335,"RendementDuPanneau":"17,26%"}]

this how it looks这看起来如何结果

this how it should look like这应该是什么样子喜欢

An output of zero from the following indicates there's no element with id gable .下面的 output 为零表示没有 id 为gable的元素。

console.log( $('#gable').length );

Please confirm that the table has this id, otherwise add an id attribute, thus:请确认该表有这个id,否则添加一个id属性,即:

id="gable"

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

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