Skip to content Skip to sidebar Skip to footer

Creating Html Table From Json Data

I have a an html file that has this snippet in it.
I am receiving JSON data that looks like this: { '1': [

Solution 1:

Try this simple working example. I hope it will work as per your expectation.

var dataObj = {
    "1": [{
            "A": "",
            "B": "",
            "C": "",
            "D": "",
            "E": ""
        },
        {
            "F": "",
            "G": "",
            "H": "",
            "I": "",
            "J": ""
        },
        {
            "K": "",
            "L": "",
            "M": "",
            "N": "",
            "O": ""
        }
    ]};

var dictionaryData = dataObj["1"];

for (var i in dictionaryData) {
  var table = document.getElementById("apps");
  var tr = document.createElement("tr");
  var td = document.createElement("td");
  
  for (var key in dictionaryData[i]) {
    var txt = document.createTextNode(key);
    td.appendChild(txt);
    tr.appendChild(td);
  }
  table.appendChild(tr);
}
table, td {
    border: 1px solid black;
}
<div><tableid="apps"></table></div>

Solution 2:

Don't know your requirements for the project, but I would skip all that trouble and use a library like DataTables. There are many ways to populate a table from AJAX or other data sources. Even if you are unable to tinker with table data source to comply with its standards, there is a method to reformat.

Solution 3:

Your question is "Which is better: having the table row and cell tags already in the markup, or build them as I get data?"

I would say the better option is to build the data as you get the objects.

In pseduocode:

foreach object in1
  make newrow<tr></tr>foreach data in object
    make new cell <td></td>insert data into<td>[here]</td>insert cell into<tr>[here]</tr>insert filled rowinto "apps" byusing it's ID
done

Does this suffice for what you wanted to know?

Solution 4:

Check this demo:

var jsonResponse = {
    "1": [{
            "A": "",
            "B": "",
            "C": "",
            "D": "",
            "E": ""
        },
        {
            "F": "",
            "G": "",
            "H": "",
            "I": "",
            "J": ""
        }
    ],
    "2": [{
            "K": "",
            "L": "",
            "M": "",
            "N": "",
            "O": ""
        },
        {
            "P": "",
            "Q": "",
            "R": "",
            "S": "",
            "T": ""
        }
    ]
};

$.each(jsonResponse, function(outerKey, list) {
    var row = $('<tr>', {
        id: 'row_' + outerKey
    });
    $.each(list, function(innerKey, value) {
        for (var key in value) {
            var col = $('<td>', {
                id: key,
                text: key
            })
            row.append(col);
        }
    });
    $('#apps').append(row);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><tableid="apps"></table></div>

Solution 5:

CREATE PROC SearchAllTables 
(
    @SearchStr nvarchar(100)
)
ASBEGINCREATETABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ONDECLARE@TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET@TableName=''SET@SearchStr2= QUOTENAME('%'+@SearchStr+'%','''')

    WHILE @TableNameISNOTNULLBEGINSET@ColumnName=''SET@TableName= 
        (
            SELECTMIN(QUOTENAME(TABLE_SCHEMA) +'.'+ QUOTENAME(TABLE_NAME))
            FROM    INFORMATION_SCHEMA.TABLES
            WHERE       TABLE_TYPE ='BASE TABLE'AND QUOTENAME(TABLE_SCHEMA) +'.'+ QUOTENAME(TABLE_NAME) >@TableNameAND OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) +'.'+ QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) =0
        )

        WHILE (@TableNameISNOTNULL) AND (@ColumnNameISNOTNULL)
        BEGINSET@ColumnName=
            (
                SELECTMIN(QUOTENAME(COLUMN_NAME))
                FROM    INFORMATION_SCHEMA.COLUMNS
                WHERE       TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND TABLE_NAME  = PARSENAME(@TableName, 1)
                    AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                    AND QUOTENAME(COLUMN_NAME) >@ColumnName
            )

            IF @ColumnNameISNOTNULLBEGININSERTINTO #Results
                EXEC
                (
                    'SELECT '''+@TableName+'.'+@ColumnName+''', LEFT('+@ColumnName+', 3630) 
                    FROM '+@TableName+' (NOLOCK) '+' WHERE '+@ColumnName+' LIKE '+@SearchStr2
                )
            ENDENDENDSELECT ColumnName, ColumnValue FROM #Results
 ENDexec SearchAllTables @SearchStr='Canada'

Post a Comment for "Creating Html Table From Json Data"