-1

I am new to javascript and jQuery. I have loaded a JsonData.json file. I need help to loop through all the data and display it in a HTML table when the user clicks a button.

<script>
    var a = {};
    $.getJSON('JsonData.json', function (data) {
        a = data;
    });
</script>

My table structure is below:

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>IDNumber</th>
    </tr>
</table>

My "JsonData.json" file structure is below:

[
    {
        "ID": 1,
        "Name": "John Smith",
        "IDNumber": "7606015012088"
    },
    {
        "ID": 2,
        "Name": "Molly Malone",
        "IDNumber": "8606125033087"
    },
    {
        "ID": 3,
        "Name": "Rianna Chetty",
        "IDNumber": "6207145122087"
    },
    {
        "ID": 4,
        "Name": "Gregory Nazul",
        "IDNumber": "8112125042088"
    },
    {
        "ID": 5,
        "Name": "Mary Billat",
        "IDNumber": "9103317012087"
    },
    {
        "ID": 6,
        "Name": "Harry Popadopalous",
        "IDNumber": "7206085031088"
    },
    {
        "ID": 7,
        "Name": "Jim Beam",
        "IDNumber": "5101236012088"
    }
]
4

1 回答 1

1

你可以使用类似的东西:

     <script>
            var a = {};
            $.getJSON('JsonData.json', function (data) {
                a = data;

    $.each(a, function(idx, elem){
    $('table#tbl TBODY').append('<tr><td>'+elem.ID+'</td><td>'+elem.Name +'</td><td>'+elem.IDNumber +'</td></tr>');
    });
                });

         </script>

<table id="tbl">
<thead><tr><th>ID</th><th>Name</th><th>IDNumber</th></tr></thead>
<tbody></tbody>
</table>
于 2013-10-25T11:12:06.197 回答