树表html来自json [英] Tree table html from json

查看:133
本文介绍了树表html来自json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON格式,我正在使用primeng,并希望使用它为树表结构(HTML文件).

I have a JSON format and I am using primeng and want to use it a tree table structure (Html file).

JSON:

 {
      brinname: "Aamir",
      aantalPersonen: "122",
      signalenVestiging: [
        {
          vestiging: "Ranchi",
          aantalPersonen: "102",
          signalenCode: [
            {
            signaalCode: "4",
            aantalPersonen: "15"
           },
          {
            signaalCode: "5",
            aantalPersonen: "15"
          } ]
        }, {
          vestiging: "Bangalore",
          aantalPersonen: "82",
          signalenCode: [
            {
              signaalCode: "6",
              aantalPersonen: "15"
            },
            {
              signaalCode: "7",
              aantalPersonen: "15"
            } ]
        } ]

    },
    {
      brinname: "Abhinav",
      aantalPersonen: "122",
      signalenVestiging: [
        {
          vestiging: "Bangalore",
          aantalPersonen: "102",
          signalenCode: [ {
            signaalCode: "7",
            aantalPersonen: "15"
          }]
        } ]

有人可以向我解释如何实现上述要求吗?创建树形表html结构使我很困惑.

Can someone explain to me how can I achieve the above request? I am getting a lot of confusion to create a tree table html structure.

推荐答案

说明:您需要通过Object.keys(element).forEach将所有元素的属性克隆到node的数据属性-排除数组类型的属性(signalenVestiging).然后将数组类型属性中的元素添加到node的子数组中. (对不起,我的英语不好)

Explain: You need to clone all element's property to node's data property by Object.keys(element).forEach - exclude array type property (signalenVestiging, signalenCode). Then add elements in array type property to node's children array. (Sorry for my bad english)

您可以使用以下代码

this.jsonData.forEach(element => {
      let tmp: any = {
        data: {},
        children: []
      };
      Object.keys(element).forEach(prop => {
        if (prop != 'signalenVestiging') {
          tmp.data[prop] = element[prop];
        } else {
          element[prop].forEach(c1 => {
            let tmp1: any = {
              data: {},
              children: []
            };
            Object.keys(c1).forEach(prop1 => {
              if (prop1 != 'signalenCode') {
                tmp1.data[prop1] = c1[prop1];
              } else {
                c1[prop1].forEach(c2 => {
                  let clone = $.extend(true, {}, c2);
                  tmp1.children.push({ data: clone });
                });
              }
            });
            tmp.children.push(tmp1);
          });
        }
      });
      this.data.push(tmp);
    });

演示此处

这篇关于树表html来自json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆