文件夹分布tree

1、处理数据

deviceList=[{
address: "江西省九江市都昌县狮山街",
province: "江西省",
city: "九江市",
device_id: "2021041600100001",
device_name: "江西明成水产养殖监测",
},{
address: "江西省九江市都昌县狮山街",
province: "湖北省",
city: "襄阳市",
device_id: "2021041600100001",
device_name: "江西明成水产养殖监测",
}]

2、分类处理数据,按照省->市->设备名

var noProvince = { name: "未归类", children: [] };

      var zNodes = [],massifNodes=[];
      if (!devices || !devices.length) {
        return;
      }
      // console.log("devices,,,",devices);
      if (devices.length < 2) {
        zNodes = devices.map((device) => {
          return { name: device.device_name, device, isParent: false     
      };
        });
      } else {
        devices.forEach((device) => {
          if (!device.province) {
            if (!zNodes.find((node) => node.name === "未归类")) {
              zNodes.unshift(noProvince);
            }
            noProvince.children.push({
              name: device.device_name,
              device
            });
            return;
          }
          var provinceNode = zNodes.find(
            (item) => item.name === device.province
          );
          if (!provinceNode) {
            provinceNode = {
              name: device.province,
              children: [],
            };
            zNodes.push(provinceNode);
          }

          if (!device.city) {
            provinceNode.children.push({
              name: device.device_name,
              device,
              font: { color: device.online_ind ? "green" : "red" },
            });
          } else {
            var cityNode = provinceNode.children.find(
              (item) => item.name === device.city
            );
            if (!cityNode) {
              cityNode = {
                name: device.city,
                children: [],
              };
              provinceNode.children.push(cityNode);
            }
            cityNode.children.push({
              name: device.device_name,
              device,
              font: { color: device.is_online ? "green" : "red" },
            });
          }
        });
      }    

 

posted @ 2021-07-20 17:09  一江春水向东刘小姐  阅读(55)  评论(0编辑  收藏  举报