Convert JSON array to an HTML table in jQuery

前端 未结 15 1084
温柔的废话
温柔的废话 2020-11-22 16:33

Is there a really easy way I can take an array of JSON objects and turn it into an HTML table, excluding a few fields? Or am I going to have to do this manually?

15条回答
  •  迷失自我
    2020-11-22 16:52

    Make a HTML Table from a JSON array of Objects by extending $ as shown below

    $.makeTable = function (mydata) {
        var table = $('');
        var tblHeader = "";
        for (var k in mydata[0]) tblHeader += "";
        tblHeader += "";
        $(tblHeader).appendTo(table);
        $.each(mydata, function (index, value) {
            var TableRow = "";
            $.each(value, function (key, val) {
                TableRow += "";
            });
            TableRow += "";
            $(table).append(TableRow);
        });
        return ($(table));
    };
    

    and use as follows:

    var mydata = eval(jdata);
    var table = $.makeTable(mydata);
    $(table).appendTo("#TableCont");
    

    where TableCont is some div

    提交回复
    热议问题
    " + k + "
    " + val + "