【问题标题】:jQuery + table creation from jsonjQuery + 从 json 创建表
【发布时间】:2011-03-14 16:33:42
【问题描述】:

我有一个 JSON 文件

{
  "Scenario"    : "ModelNameHere",
  "id"          : "1",
  "description" : "Description of table model goes here",
  "headers"     : {
     "Column 1 header" : {
       "order"      : 1,
       "items"      : {
         "Row 1 Col 1 value" : {
           "id"         : 1,
           "comment"    : "Comment on Row 1 Col 1",
           "order"      : 1
         },
         "Row 2 Col 2 value" : {
           "id"         : 2,
           "comment"    : "Comment on Row 2 Col 2",
           "order"      : 2
         }
       }  
     },
     "Column 2 header" : {
       "order"      : 2,
       "items"      : {
         "Row 1 Col 2 value" : {
           "id"         : 3,
           "comment"    : "Comment on Row 1 Col 2",
           "order"      : 1
         },
             "Row 2 Col 2 value" : {
           "id"         : 4,
           "comment"    : "Comment on Row 2 Col 2",
           "order"      : 2
         }
       }  
     }
  }
}

我想使用 jQuery 将 JSON 数据放入 HTML 表格中。

标题应该放在 TH td 项目中,标题中的项目应该是每个相关行的 TD 中的元素。

在 JS 或 jQuery 中是否有使用 JSON 数据填充表格的函数?

如何循环遍历 JSON 元素并将其与表格元素匹配?

谢谢。

【问题讨论】:

  • OP 有 31 年 :D 可能不是功课 :)
  • JSON 字符串无效,先在JSLINT.com上检查这个

标签: javascript jquery json


【解决方案1】:

不,没有这样的功能,因为它是一个真正的情境特定功能。

虽然写起来很容易。
您可以将 JSON 作为普通 JS 对象访问。
这意味着您可以循环遍历它并将信息放在正确的位置。

【讨论】:

    【解决方案2】:

    This question 探索了各种模板引擎,它们可以帮助您实现您想要实现的目标。您仍然需要为表格定义模板,但填充它会非常容易。

    【讨论】:

    【解决方案3】:

    看看这个插件。有很多 jQuery 插件,所以选择一个适合你的。 http://www.datatables.net 这是来自 json 的示例,除了它是从 txt 文件加载的,但这不是问题。 http://www.datatables.net/examples/data_sources/ajax.html

    以下是我建议您重组 json 的方法。老实说,您应该在这里考虑表格而不是对象。

    {
        "model" : "Model name here",
        "id" : 1,
        "description" : "Description of table model goes here",
        "headers" : {
            "header" : {
                "id" : 1,
                "order" : 1,
                "name" : "Col 1 header name" 
            },
            "header" : {
                "id" : 2,
                "order" : 3,
                "name" : "Col 2 header name" 
            } 
        },
        "row1" : {
            "item" : {
                "id" : 1,
                "name" : "Some td data comes here" 
            },
            "item2" : {
                    "id" : 4,
                "name" : "Some td data comes here" 
            } 
        },
        "row2" : {
            "item" : {
                "id" : 2,
                "name" : "Some td data comes here" 
            },
            "item2" : {
                "id" : 3,
                "name" : "Some td data comes here" 
            } 
        } 
    }
    

    【讨论】:

    • 我探索了 Datatables、jEditable 和 jqGrid。决定使用个人代码,因为它会更简洁,而且我主要只需要这些更大的网格管理器的 5-10% 的功能。
    • 看起来新的(和测试版的)jQuery 模板可以满足我的需要。无论哪种方式,我都会在他们出现时发布经验笔记。感谢您的回答。
    • 新改制更好看?
    • 仍然在努力实现轻松的迭代工作。
    • 表格是从左到右构造的,然后是下一行。尝试以这种方式做某事。
    【解决方案4】:

    如果您使用 PHP,请使用 json_decode('jsonstring',true); 它将以数组的形式出现并轻松操作数组以创建 html

    【讨论】:

    • 为什么要投反对票??实际上给定的字符串是无效的 json 格式
    • jQuery 应该暗示我要构建这个客户端。不过感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多