简体   繁体   English

从JSON数据创建HTML表

[英]Creating html table from JSON data

I have a an html file that has this snippet in it. 我有一个HTML文件,其中包含此代码段。

<div>
    <table id="apps"></table>
</div>

I am receiving JSON data that looks like this: 我收到的JSON数据如下所示:

{
    "1": [
        {
            "A": "",
            "B": "",
            "C": "",
            "D": "",
            "E": ""
        }
    ]
}

There will be exactly one "1" , but there can be more than one dictionary within the list of "1" . 恰好会有一个"1" ,但是在"1"的列表中可以有多个词典。 In this example, we only have one {} within the list, [] , but there can exist multiple {} of containing exactly five items like what is shown above. 在此示例中,列表[]只有一个{} ,但是可以存在多个{} ,其中正好包含五个项目,如上所示。

I want to create a table from this data, where each row represents a single {} within the [] and has five columns representing A, B, C, D, E respectively. 我想根据此数据创建一个表,其中每一行代表[]的单个{} ,并具有五列,分别代表A,B,C,D,E。

I am unsure if I should have the structure of this, the tags already in my html(this is not in my html code provided) and then populate these tags or should my function that loads this data in my html file, access table id="apps" and create these tags and then populate these tags? 我不确定是否应该具有这样的结构,即我的html中已经存在标签(此标签不在提供的html代码中),然后填充这些标签,还是我的函数应该将该数据加载到我的html文件中,访问表id = “应用”并创建这些标签,然后填充这些标签? Which is better? 哪个更好? and How might one accomplish this efficiently? 以及如何有效地做到这一点?

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> <table id="apps"></table> </div> 

Don't know your requirements for the project, but I would skip all that trouble and use a library like DataTables . 不知道您对项目的要求,但是我会跳过所有麻烦,而使用DataTables之类的库。 There are many ways to populate a table from AJAX or other data sources . 有很多方法可以从AJAX或其他数据源填充表。 Even if you are unable to tinker with table data source to comply with its standards, there is a method to reformat. 即使您无法修改表数据源以使其符合标准,也可以使用重新格式化的方法

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: 在pseduocode中:

for each object in 1
  make new row <tr></tr>

  for each data in object
    make new cell <td></td>
    insert data into <td>[here]</td>
    insert cell into <tr>[here]</tr>

  insert filled row into "apps" by using it's ID
done

Does this suffice for what you wanted to know? 这足以满足您的要求吗?

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); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <table id="apps"></table> </div> 

CREATE PROC SearchAllTables 
(
    @SearchStr nvarchar(100)
)
AS
BEGIN

    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

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

    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName = 
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM    INFORMATION_SCHEMA.TABLES
            WHERE       TABLE_TYPE = 'BASE TABLE'
                AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(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 @ColumnName IS NOT NULL
            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                    FROM ' + @TableName + ' (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END 
    END

    SELECT ColumnName, ColumnValue FROM #Results
 END


exec SearchAllTables @SearchStr='Canada'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM