Convert JSON array to an HTML table in jQuery

前端 未结 15 1076
温柔的废话
温柔的废话 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条回答
  •  -上瘾入骨i
    2020-11-22 16:39

    For very advanced JSON objects to HTML tables you can try My jQuery Solution that is based on this closed thread.

    var myList=[{"name": "abc","age": 50},{"name": {"1": "piet","2": "jan","3": "klaas"},"age": "25","hobby": "watching tv"},{"name": "xyz","hobby": "programming","subtable": [{"a": "a","b": "b"},{"a": "a","b": "b"}]}];
    
    // Builds the HTML Table out of myList json data from Ivy restful service.
     function buildHtmlTable() {
          addTable(myList, $("#excelDataTable"));
     }
    
    function addTable(list, appendObj) {
        var columns = addAllColumnHeaders(list, appendObj);
    
        for (var i = 0; i < list.length; i++) {
            var row$ = $('');
            for (var colIndex = 0; colIndex < columns.length; colIndex++) {
                var cellValue = list[i][columns[colIndex]];
    
                if (cellValue == null) {
                    cellValue = "";
                }
    
                if (cellValue.constructor === Array)
                {
                    $a = $('');
                    row$.append($a);
                    addTable(cellValue, $a);
    
                } else if (cellValue.constructor === Object)
                {
    
                    var array = $.map(cellValue, function (value, index) {
                        return [value];
                    });
    
                    $a = $('');
                    row$.append($a);
                    addObject(array, $a);
    
                } else {
                    row$.append($('').html(cellValue));
                }
            }
            appendObj.append(row$);
        }
    }
    
    
    function addObject(list, appendObj) {
        for (var i = 0; i < list.length; i++) {
            var row$ = $('');
    
            var cellValue = list[i];
    
            if (cellValue == null) {
                cellValue = "";
            }
    
            if (cellValue.constructor === Array)
            {
                $a = $('');
                row$.append($a);
                addTable(cellValue, $a);
    
            } else if (cellValue.constructor === Object)
            {
    
                var array = $.map(cellValue, function (value, index) {
                    return [value];
                });
    
                $a = $('');
                row$.append($a);
                addObject(array, $a);
    
            } else {
                row$.append($('').html(cellValue));
            }
            appendObj.append(row$);
        }
    }
    
    // Adds a header row to the table and returns the set of columns.
    // Need to do union of keys from all records as some records may not contain
    // all records
    function addAllColumnHeaders(list, appendObj)
    {
        var columnSet = [];
        var headerTr$ = $('');
    
        for (var i = 0; i < list.length; i++) {
            var rowHash = list[i];
            for (var key in rowHash) {
                if ($.inArray(key, columnSet) == -1) {
                    columnSet.push(key);
                    headerTr$.append($('').html(key));
                }
            }
        }
        appendObj.append(headerTr$);
    
        return columnSet;
    }
    

提交回复
热议问题