JSON数据进入看起来像表格的HTML列表

艰苦健身

具有嵌套的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?
提神

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

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"));

演示

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

更新

根据您的要求有嵌套列表可折叠所看到这里,我已经更新了我的原代码,以及做了一些修改,您提到的网站上的代码。

更新的演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在php中显示看起来像电子商务产品列表格式的用户列表

来自分类Dev

有没有办法使此下拉列表看起来像表格?

来自分类Dev

如何对齐显示的数据以使其看起来像表格?

来自分类Dev

字符串转换看起来像python列表

来自分类Dev

将看起来像JSON的文件解析为JSON

来自分类Dev

如何采样数据看起来像双变量月亮?

来自分类Dev

我如何添加双引号看起来像json

来自分类Dev

匹配看起来像JSON的字符串

来自分类Dev

将看起来像列表“ [0448521958,+61439800915]”的字符串从JSON导入到Python,并使其成为实际列表?

来自分类Dev

哎呦,看起来像出事了

来自分类Dev

如何使按钮看起来像标签?

来自分类Dev

绘制看起来像球体的球体

来自分类Dev

使普通布局看起来像PreferenceScreen

来自分类Dev

如何使UITableView看起来像这样?

来自分类Dev

看起来像JTextArea的JTextPane

来自分类Dev

使Ubuntu看起来像Parrot os

来自分类Dev

使按钮看起来像文本

来自分类Dev

如何使UITableView看起来像这样?

来自分类Dev

防止链接看起来像按钮

来自分类Dev

看起来像ImageButtons的RadioButtons

来自分类Dev

应用看起来像DialogFragment

来自分类Dev

使Outlook看起来像雷鸟吗?

来自分类Dev

这看起来像加密吗?

来自分类Dev

让 linux 看起来像 windows 8

来自分类Dev

C#在许多看起来像列表的列表中选择一些列表

来自分类Dev

C#在许多看起来像列表的列表中选择一些列表

来自分类Dev

如何样式化表格使其看起来像gridview

来自分类Dev

我们如何使引导程序行/列看起来像表格?

来自分类Dev

如何使用引导程序使表格内部的单选按钮看起来像按钮?

Related 相关文章

热门标签

归档