Converting json data to an HTML table

前端 未结 4 924
心在旅途
心在旅途 2021-01-03 09:43

I have an array of data in php and I need to display this data in a HTML table. Here is what an example data set looks like.

Array(
  Array
  (
     [compar         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-03 10:03

    dnaluz,

    as mentioned by others, there are great libs out there to do thios client side. I'd also take the stance that you'd be best sending the json array to the client and then using the lib to present the data in tabular format.

    Altho i mention the use of nice presentation libs to create the table, below is a little function that i use for lightweight quick and dirty table visualizations from a json array:

    function CreateTableFromJson(objArray) {
        // has passed in array has already been deserialized
        var array = typeof  objArray != 'object' ? JSON.parse(objArray) : objArray;
    
        str = '';
        str += '';
        for (var index in array[0]) {
            str += '';
        }
        str += '';
        str += '';
        for (var i = 0; i < array.length; i++) {
            str += (i % 2 == 0) ? '' : '';
            for (var index in array[i]) {
                str += '';
            }
            str += '';
        }
        str += ''
        str += '
    ' + index + '
    ' + array[i][index] + '
    '; return str; }

    hope this is useful in some minor way.. jim

    [edit] - here's a link to a page using a very similar technique: http://www.zachhunter.com/2010/04/json-objects-to-html-table/

提交回复
热议问题