【问题标题】:JSON data into HTML list that looks like a table将 JSON 数据转换为看起来像表格的 HTML 列表
【发布时间】:2014-03-14 11:00:56
【问题描述】:

有嵌套的 JSON 数组,目前正在尝试创建一个类似于表格的 HTML 列表 ul/li 类型。

这是我的 JSON 数据,问题是对象的名称。

{
  "aaData": [
    {
      "id": "1",
      "template_id": "1",
      "question": "Is government stable?",
      "answer": "Stable",
      "parent_question_id": "0",
      "section_id": "2",
      "subquestions": [
        {
          "id": "2",
          "template_id": "1",
          "question": "Is there funding approved?",
          "answer": "Since March 2013",
          "parent_question_id": "1",
          "section_id": "2",
          "subquestions": [
            {
              "id": "3",
              "template_id": "1",
              "question": "How much funding do we have?",
              "answer": "120 Million",
              "parent_question_id": "2",
              "section_id": "1"
            },
            {
              "id": "4",
              "template_id": "1",
              "question": "Do we have all raw materials available?",
              "answer": "Not all, need to find correct type of wood.",
              "parent_question_id": "2",
              "section_id": "1"
            },
            {
              "id": "5",
              "template_id": "1",
              "question": "What currency is the funding in?",
              "answer": "GBP",
              "parent_question_id": "2",
              "section_id": "1"
            },
            {
              "id": "6",
              "template_id": "1",
              "question": "When will the currency be paid?",
              "answer": "7 days after invoice",
              "parent_question_id": "2",
              "section_id": "1"
            },
            {
              "id": "13",
              "template_id": "1",
              "question": "why do we do this?",
              "answer": null,
              "parent_question_id": "2",
              "section_id": "1"
            }
          ]
        },
        {
          "id": "7",
          "template_id": "1",
          "question": "What groups are going to investigate?",
          "answer": null,
          "parent_question_id": "1",
          "section_id": "2",
          "subquestions": [
            {
              "id": "8",
              "template_id": "1",
              "question": "What employees have clearance to go?",
              "answer": null,
              "parent_question_id": "7",
              "section_id": "1"
            },
            {
              "id": "9",
              "template_id": "1",
              "question": "Do employees have passports?",
              "answer": null,
              "parent_question_id": "7",
              "section_id": "1",
              "subquestions": [
                {
                  "id": "10",
                  "template_id": "1",
                  "question": "Are employees able to travel?",
                  "answer": null,
                  "parent_question_id": "9",
                  "section_id": "1"
                },
                {
                  "id": "11",
                  "template_id": "1",
                  "question": "Can employees enter without VISA?",
                  "answer": null,
                  "parent_question_id": "9",
                  "section_id": "1"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "id": "12",
      "template_id": "1",
      "question": "Is market good?",
      "answer": null,
      "parent_question_id": "0",
      "section_id": "2"
    }
  ]
}

这是我的代码,我只想在行中显示问题和答案,而不是每个元素。并且还希望它看起来像一张桌子,但有正确的缩进(twitter bootstrap?)。

function buildHtml( obj , ul ) {

            for (i in obj) {

                console.log(obj[i]);
                //if(i == "question") {
                    var li = document.createElement('li');
                    li.innerHTML = obj[i];
                    li.className = "list-group-item";
                    //li.style.display = "table-cell";
                    ul.appendChild( li );
                    ul.className = "list-group";
                    //ul.style.display = "table-row";

                    if ( typeof(obj[i])== "object" ) {

                        var childUl = document.createElement('ul');
                        li.appendChild( childUl ); 

                        buildHtml(obj[i], childUl );            
                    } 
                //} 

            }


        } 
        var ul = document.createElement('ul');
        ul.className = "list-group";
        //ul.style.display = "table-row";
        buildHtml( questions ,ul );
        var div = document.getElementById('test');    
        div.appendChild( ul );

......

<div id="test"></div>

所以如果有人有想法请告诉我。

添加表格结构的外观:

Is government stable? Stable
     Is there funding approved? Since March 2013
           How much funding do we have? 120 Million
           Do we have all raw materials available? Not all, need to find correct type of wood.
           What currency is the funding in? GBP
           When will the currency be paid? 7 days after invoice
           why do we do this?
     What groups are going to investigate?
           What employees have clearance to go?
           Do employees have passports?
                Are employees able to travel?
                Can employees enter without VISA?
Is market good?

【问题讨论】:

  • 首先将id从"#test"重命名为"test"
  • 哦,是的,对不起,我没有从程序中手动复制和粘贴它。如果你有一个@Satpal,期待你的解决方案
  • 有人知道为什么它被否决了吗?应该没有理由,因为它符合标准。
  • 没有线索。三人投票决定关闭,因为“缺乏足够的信息来诊断问题”。那些投票结束的人会发布 cmets 解释他们认为需要哪些额外信息,这将是有帮助的。

标签: javascript jquery html json recursion


【解决方案1】:

这是一个简单的原型函数,可以帮助您入门。

var Menu = function (data) {
    this.data = data;
};

Menu.prototype.render = function (root) {
    var ul = $("<ul></ul>");
    var li = $("<li></li>");
    li.append($("<a></a>", {
        url: this.data.id,
        text: this.data.question
    })).appendTo(ul);
    ul.appendTo(root);
    if (this.data.subquestions) {
        Menu.renderMenus(this.data.subquestions, $("<li></li>").appendTo(ul));
    }
};

Menu.renderMenus = function (menus, root) {
    $.each(menus, function (key, val) {
        var m = new Menu(val);
        m.render(root);
    });
}

Menu.renderMenus(aaData, $("#test"));

DEMO

您显然可以在需要的地方向数据添加更多字段。

更新

根据您的要求,让嵌套列表可折叠,如 here 所示,我已经更新了我的原始代码,并对您引用的网站上的代码进行了一些修改。

UPDATED DEMO

【讨论】:

  • 这太棒了!我想在这之后我想做的就是让它成为一种格式,这样我就可以像这个页面那样做一个折叠/展开功能:gabrieleromanato.name/…我玩过你的代码,但不知道在哪里添加它。任何帮助都会很棒!
  • 我在结尾添加了这个:$('#test li:has(> ul)').addClass('parent');那么也许这可能会更好地将类父级添加到li?
  • @HardFitness - 请参阅我的回答中的 UPDATE 部分。
  • 太好了,如果我只想使用 + 和 - 打开和关闭数据而不是单击问题名称,我应该关闭 并创建一个 div 吗?基本上我希望用户点击问题并弹出一个弹出窗口,只有加号会使其扩展和 - 折叠。谢谢
  • 是的,&lt;a&gt; 中的任何内容都会触发切换。如果您不希望文本成为其中的一部分,则需要将其移到 &lt;a&gt; 标记之外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2015-09-02
  • 2013-07-02
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多