列表數據(包含父節點關系)轉化為樹形結構


github 列表數據(包含父節點關系)轉化為樹形結構

今天開發遇到一個需求,就是把包含父子關系的數據轉化為樹形結構,這個需求來自我開發的一個功能,前端需要使用 Element 的級聯選擇器實現行業級聯選擇的功能。

  • 列表數據類型
    每條數據都列出了其父 id,如果沒有就是最頂層的數據(樹結構的話就是第一層)
{'id': 'A', 'pId': '', 'singlehyname': '農、林、牧、漁業', 'name': '農、林、牧、漁業(A)', 'isParent': True, 'parentcode': ''}
{'id': 'A01', 'pId': 'A', 'singlehyname': '農業', 'name': '農業(A01)', 'isParent': True, 'parentcode': 'A'}
{'id': 'A011', 'pId': 'A01', 'singlehyname': '谷物種植', 'name': '谷物種植(A011)', 'isParent': True, 'parentcode': 'A01'}
{'id': 'A0111', 'pId': 'A011', 'singlehyname': '稻谷種植', 'name': '稻谷種植(A0111)', 'isParent': False, 'parentcode': 'A011'}
'isParent': False, 'parentcode': 'A011'}
{'id': 'A012', 'pId': 'A01', 'singlehyname': '豆類、油料和薯類種植', 'name': '豆類、油料和薯類種植(A012)', 'isParent': True, 'parentcode': 'A01'}
{'id': 'A0121', 'pId': 'A012', 'singlehyname': '豆類種植', 'name': '豆類種植(A0121)', 'isParent': False, 'parentcode': 'A012'}
'isParent': False, 'parentcode': 'A012'}
{'id': 'A013', 'pId': 'A01', 'singlehyname': '棉、麻、糖、煙草種植', 'name': '棉、麻、糖、煙草種植(A013)', 'isParent': True, 'parentcode': 'A01'}
{'id': 'A0131', 'pId': 'A013', 'singlehyname': '棉花種植', 'name': '棉花種植(A0131)', 'isParent': False, 'parentcode': 'A013'}
  • 要整合成這種數據類型
    層級關系不固定,依賴於上面的數據
[
  {
    children: [
      {
        children: [
          {
            value: "海水養殖",
            label: "海水養殖(A0411)"
          },
        ],
        value: "水產養殖",
        label: "水產養殖(A041)"
      },
    ],
    value: "漁業",
    label: "漁業(A04)"
  }
];
  • 如何實現
    首先遍歷列表數據,使用第一層級的數據(就是沒有父親的數據)初始化初始結果,然后再次遍歷列表數據,利用遞歸將有父親的數據添加到結果中。(具體實現見下面的代碼)
  • 最終前端實現效果
    實現效果
  • 代碼
import json
import requests

def arrange(result, industry):
    p_id = industry['pId']
    for temp in result:
        if p_id in temp['label']:
            if 'children' in temp:
                temp['children'].append(dict(value=industry['singlehyname'], label=industry['name']))
            else:
                temp['children'] = [dict(value=industry['singlehyname'], label=industry['name'])]
            continue
        if 'children' in temp:
            arrange(temp['children'], industry)

def get_permit_industry():
    # 獲取源數據
    headers = {
        'Host': 'permit.mee.gov.cn',
        'Origin': 'http://permit.mee.gov.cn',
        'Referer': 'http://permit.mee.gov.cn/permitExt/common/tree/xzxk-common-trade!selectHylist.action?hyids=&hytype=more',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    url = 'http://permit.mee.gov.cn/permitExt/common/tree/xzxk-common-trade!hynodeslist.action'
    res = requests.post(url, headers=headers)
    industry_info = json.loads(res.text.replace("/**/\r\n", ''))
    result = []
    # 初始化結果
    for i in industry_info:
        if not i['pId']:
            result.append(dict(value=i['singlehyname'], label=i['name']))
    # 將其他層級的數據添加到結果中(原始數據相對比較規整,如果完全打亂,這樣實現會有問題)
    for i in industry_info:
        if i['pId']:
            arrange(result, i)
    return result


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM