树形菜单数据转化

2019-10-11  本文已影响0人  领带衬有黄金

1. 需求分析:需要使用树形组件来进行导航,就需要提供与组件相应的数据

2. 组件需要的数据结构

[
    {
        'label': '一级 1',
        'children': [{
            'label': '二级 1-1',
            'children': [{
                'label': '三级 1-1-1'
            }]
        }]
    },
    {
        'label': '一级 2',
        'children': [{
            'label': '二级 2-1',
            'children': [{
                'label': '三级 2-1-1'
            }]
        }, {
            'label': '二级 2-2',
            'children': [{
                'label': '三级 2-2-1'
            }]
        }]
    },
    {
        'label': '一级 3',
        'children': [{
            'label': '二级 3-1',
            'children': [{
                'label': '三级 3-1-1'
            }]
        }, {
            'label': '二级 3-2',
            'children': [{
                'label': '三级 3-2-1'
            }]
        }]
    }
]

3. 组件展示:

image.png

4. 后端数据处理

4.1 数据原型:

[
     'Ferrous alloy___Steel___Stainless steel___Austenitic___316___Heat treated',
     'Nonferrous alloy___Aluminum___Wrought aluminum___2xxx series, aluminum-copper___2219___Al 2219',
     'Ferrous alloy___Steel___Steel casting___Low-alloy___Steel, 0.15% C',
     'Ferrous alloy___Steel___Stainless steel___Austenitic___AL-6XN',
     'Ferrous alloy___Iron-base superalloy___Discaloy___Solution treated and aged',
     'Ferrous alloy___Steel___Carbon steel___Nonresulfurized___1065',
     'Nonferrous alloy___Copper___Wrought copper___Copper___C10100',
     'Ferrous alloy___Steel___Carbon steel___High-manganese___Carbon steel, 0.2% C, 1.5% Mn',
     'Nonferrous alloy___Aluminum___Cast aluminum___(General)___Al-33% Cu',
     'Ferrous alloy___Cast iron___Compacted or vermicular graphite___Compacted graphite cast iron',
     'Nonferrous alloy___Aluminum___Wrought aluminum___7xxx series, aluminum-zinc___7079',
     'Ferrous alloy___Steel___(General)___Steel, free machining',
     'Ceramic___Oxide-base material___Alumina, 90%',
     'Nonferrous alloy___Titanium___Alpha-beta___Ti-6Al-4V ELI',
     'Nonferrous alloy___Aluminum___Wrought aluminum___2xxx series, aluminum-copper___2024___Solution treated and aged',
     'Ferrous alloy___Cast iron___Gray (flake or lamellar graphite)___Class 50',
     'Nonferrous alloy___Copper___Cast copper___Bronze___Leaded tin bronze',
     'Nonferrous alloy___Refractory or reactive___Unclassified___Zircaloy 4/hafnium',
     'Nonferrous alloy___Magnesium___Wrought magnesium-rare earth___HM21A',
     'Ferrous alloy___Steel___Steel casting___Unclassified___ASTM A 148 grade 90-60___Normalized',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.4% C___Heat treated',
     'Nonferrous alloy___Miscellaneous___Unclassified___Hafnium',
     'Ferrous alloy___Cast iron___White (carbidic)___White cast iron',
     'Ferrous alloy___Steel___High-strength steel___Low-alloy (HSLA)___Low-carbon steel, dual phase___Annealed',
     'Nonferrous alloy___Magnesium___Cast magnesium-aluminum-zinc___AZ31B',
     'Nonferrous alloy___Superalloy___Nickel-base___Hastelloy X___Solution treated and aged',
     'Nonferrous alloy___Copper___Wrought copper___Copper___AMZIRC',
     'Nonferrous alloy___Zinc___Zinc-aluminum___AG40A___Aged',
     'Nonferrous alloy___Nickel___Nickel-copper___Monel 400___Cold worked',
     'Nonferrous alloy___Titanium___Alpha-beta___Ti-6Al-6V-2Sn___Solution treated and aged',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.44% C___Hardened',
     'Nonferrous alloy___Lead___Lead-tin___Pb-2.6Sn-5.25 Sb-0.5As',
     'Nonferrous alloy___Copper___Cast copper___Brass___Cu-37.83Zn___As fabricated',
     'Nonferrous alloy___Magnesium___Cast magnesium-aluminum-zinc___AZ92A',
     'Nonferrous alloy___Titanium___Alpha-beta___Ti-6Al-4V___Solution treated and aged',
     'Ferrous alloy___Cast iron___Malleable (temper graphite)___Malleable cast iron___Quenched',
     'Nonferrous alloy___Aluminum___Wrought aluminum___5xxx series, aluminum-magnesium___5456___Stress relieved',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.2% C, 1.75% Mn',
     'Nonferrous alloy___Copper___Cast copper___Bronze___10% tin bronze',
     'Ferrous alloy___Steel___Carbon steel___(General)___Low-carbon steel___Unspecified condition',
     'Nonferrous alloy___Aluminum___Cast aluminum___2xx.x series, aluminum-copper___213.0',
     'Nonferrous alloy___Aluminum___Wrought aluminum___3xxx series, aluminum-manganese___3003___Annealed',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.5% C, silicon deoxidized',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 1.69% C___Ultrahigh Carbon (1.69 Mass% C) Steel',
     'Nonferrous alloy___Lead___Lead-tin___Lead-base bearing alloy',
     'Ferrous alloy___Steel___Carbon steel___Nonresulfurized___1018___Misc. condition(s)',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.6% C___Annealed',
     'Nonferrous alloy___Aluminum___Wrought aluminum___5xxx series, aluminum-magnesium___5657___Annealed',
     'Ferrous alloy___Steel___Carbon steel___(General)___Structural steel',
     'Nonferrous alloy___Copper___Wrought copper___Copper___Arsenical tough pitch copper',
     'Nonferrous alloy___Zinc___Commercially pure or low-alloyed___Zn-0.1Ti-0.1Cu',
     'Ferrous alloy___Cast iron___Gray (flake or lamellar graphite)___Gray cast iron, 3.5% C, 1% P',
     'Nonferrous alloy___Nickel___(General)___Nickel-base austenitic alloy',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.2% C___Normalized',
     'Ferrous alloy___Cast iron___Ductile (nodular or spheroidal graphite)___Ductile iron___Carbidic austempered ductile iron (CADI)___Carbidic austempered ductile iron (CADI, 0.71% V)',
     'Ferrous alloy___Steel___Carbon steel___(General)___Eutectoid steel, 0.8% C',
     'Nonferrous alloy___Aluminum___Wrought aluminum___6xxx series, aluminum-magnesium-silicon___6351',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 1% C, high-purity',
     'Ferrous alloy___Steel___High-strength steel___Low-alloy (HSLA)___HSLA steel___Hot worked',
     'Ferrous alloy___Steel___High-strength steel___Ultrahigh-strength___AF 1410',
     'Ferrous alloy___Steel___Stainless steel___Ferritic___26-1 ferritic stainless',
     'Ferrous alloy___Steel___Tool steel___Hot-work___H23',
     'Nonferrous alloy___Copper___Cast copper___Copper___Oxygen-free high conductivity (OFHC) copper',
     'Nonferrous alloy___Titanium___Alpha-beta___Ti-6Al-4V___Unspecified condition',
     'Nonferrous alloy___Aluminum___Wrought aluminum___7xxx series, aluminum-zinc___7178',
     'Nonferrous alloy___Superalloy___Nickel-base___IN-100___Misc. condition(s)',
     'Ferrous alloy___Steel___Alloy steel___Nickel-chromium-molybdenum___8740___Tempered',
     'Ferrous alloy___Steel___Carbon steel___(General)___Carbon steel, 0.25% C___Tempered'
]

4.2 python处理代码:

data = {}
# 
def f(key, val):
    if key not in val:
        val[key] = {}
    return val
# 将数据进行归类,形如:{'Ferrous alloy': {'Steel': {'Stainless steel': {'Austenitic': {'316': {'Heat treated': {}}}}}}}
for i in a:
    ks = i.split("___")
    val = data
    s = ''
    for p in ks:
        if s:
            val = val[s]
            val = f(p, val)
        else:
            val = f(p, val)
        s = p
# 递归函数,处理子集children
def judge(a):
    l = []
    for i in a:
        attr = {"label": i}
        if a[i]:
            attr["children"] = judge(a[i])
        l.append(attr)
        # 当a为空时,跳出递归函数
        if a:
            return l

print(judge(data))

4.3 处理结果集

[{'label': 'Ferrous alloy', 'children': [{'label': 'Steel', 'children': [{'label': 'Stainless steel', 'children': [{'label': 'Austenitic', 'children': [{'label': '316', 'children': [{'label': 'Heat treated'}]}, {'label': 'AL-6XN'}]}, {'label': 'Ferritic', 'children': [{'label': '26-1 ferritic stainless'}]}]}, {'label': 'Steel casting', 'children': [{'label': 'Low-alloy', 'children': [{'label': 'Steel, 0.15% C'}]}, {'label': 'Unclassified', 'children': [{'label': 'ASTM A 148 grade 90-60', 'children': [{'label': 'Normalized'}]}]}]}, {'label': 'Carbon steel', 'children': [{'label': 'Nonresulfurized', 'children': [{'label': '1065'}, {'label': '1018', 'children': [{'label': 'Misc. condition(s)'}]}]}, {'label': 'High-manganese', 'children': [{'label': 'Carbon steel, 0.2% C, 1.5% Mn'}]}, {'label': '(General)', 'children': [{'label': 'Carbon steel, 0.4% C', 'children': [{'label': 'Heat treated'}]}, {'label': 'Carbon steel, 0.44% C', 'children': [{'label': 'Hardened'}]}, {'label': 'Carbon steel, 0.2% C, 1.75% Mn'}, {'label': 'Low-carbon steel', 'children': [{'label': 'Unspecified condition'}]}, {'label': 'Carbon steel, 0.5% C, silicon deoxidized'}, {'label': 'Carbon steel, 1.69% C', 'children': [{'label': 'Ultrahigh Carbon (1.69 Mass% C) Steel'}]}, {'label': 'Carbon steel, 0.6% C', 'children': [{'label': 'Annealed'}]}, {'label': 'Structural steel'}, {'label': 'Carbon steel, 0.2% C', 'children': [{'label': 'Normalized'}]}, {'label': 'Eutectoid steel, 0.8% C'}, {'label': 'Carbon steel, 1% C, high-purity'}, {'label': 'Carbon steel, 0.25% C', 'children': [{'label': 'Tempered'}]}]}]}, {'label': '(General)', 'children': [{'label': 'Steel, free machining'}]}, {'label': 'High-strength steel', 'children': [{'label': 'Low-alloy (HSLA)', 'children': [{'label': 'Low-carbon steel, dual phase', 'children': [{'label': 'Annealed'}]}, {'label': 'HSLA steel', 'children': [{'label': 'Hot worked'}]}]}, {'label': 'Ultrahigh-strength', 'children': [{'label': 'AF 1410'}]}]}, {'label': 'Tool steel', 'children': [{'label': 'Hot-work', 'children': [{'label': 'H23'}]}]}, {'label': 'Alloy steel', 'children': [{'label': 'Nickel-chromium-molybdenum', 'children': [{'label': '8740', 'children': [{'label': 'Tempered'}]}]}]}]}, {'label': 'Iron-base superalloy', 'children': [{'label': 'Discaloy', 'children': [{'label': 'Solution treated and aged'}]}]}, {'label': 'Cast iron', 'children': [{'label': 'Compacted or vermicular graphite', 'children': [{'label': 'Compacted graphite cast iron'}]}, {'label': 'Gray (flake or lamellar graphite)', 'children': [{'label': 'Class 50'}, {'label': 'Gray cast iron, 3.5% C, 1% P'}]}, {'label': 'White (carbidic)', 'children': [{'label': 'White cast iron'}]}, {'label': 'Malleable (temper graphite)', 'children': [{'label': 'Malleable cast iron', 'children': [{'label': 'Quenched'}]}]}, {'label': 'Ductile (nodular or spheroidal graphite)', 'children': [{'label': 'Ductile iron', 'children': [{'label': 'Carbidic austempered ductile iron (CADI)', 'children': [{'label': 'Carbidic austempered ductile iron (CADI, 0.71% V)'}]}]}]}]}]}, {'label': 'Nonferrous alloy', 'children': [{'label': 'Aluminum', 'children': [{'label': 'Wrought aluminum', 'children': [{'label': '2xxx series, aluminum-copper', 'children': [{'label': '2219', 'children': [{'label': 'Al 2219'}]}, {'label': '2024', 'children': [{'label': 'Solution treated and aged'}]}]}, {'label': '7xxx series, aluminum-zinc', 'children': [{'label': '7079'}, {'label': '7178'}]}, {'label': '5xxx series, aluminum-magnesium', 'children': [{'label': '5456', 'children': [{'label': 'Stress relieved'}]}, {'label': '5657', 'children': [{'label': 'Annealed'}]}]}, {'label': '3xxx series, aluminum-manganese', 'children': [{'label': '3003', 'children': [{'label': 'Annealed'}]}]}, {'label': '6xxx series, aluminum-magnesium-silicon', 'children': [{'label': '6351'}]}]}, {'label': 'Cast aluminum', 'children': [{'label': '(General)', 'children': [{'label': 'Al-33% Cu'}]}, {'label': '2xx.x series, aluminum-copper', 'children': [{'label': '213.0'}]}]}]}, {'label': 'Copper', 'children': [{'label': 'Wrought copper', 'children': [{'label': 'Copper', 'children': [{'label': 'C10100'}, {'label': 'AMZIRC'}, {'label': 'Arsenical tough pitch copper'}]}]}, {'label': 'Cast copper', 'children': [{'label': 'Bronze', 'children': [{'label': 'Leaded tin bronze'}, {'label': '10% tin bronze'}]}, {'label': 'Brass', 'children': [{'label': 'Cu-37.83Zn', 'children': [{'label': 'As fabricated'}]}]}, {'label': 'Copper', 'children': [{'label': 'Oxygen-free high conductivity (OFHC) copper'}]}]}]}, {'label': 'Titanium', 'children': [{'label': 'Alpha-beta', 'children': [{'label': 'Ti-6Al-4V ELI'}, {'label': 'Ti-6Al-6V-2Sn', 'children': [{'label': 'Solution treated and aged'}]}, {'label': 'Ti-6Al-4V', 'children': [{'label': 'Solution treated and aged'}, {'label': 'Unspecified condition'}]}]}]}, {'label': 'Refractory or reactive', 'children': [{'label': 'Unclassified', 'children': [{'label': 'Zircaloy 4/hafnium'}]}]}, {'label': 'Magnesium', 'children': [{'label': 'Wrought magnesium-rare earth', 'children': [{'label': 'HM21A'}]}, {'label': 'Cast magnesium-aluminum-zinc', 'children': [{'label': 'AZ31B'}, {'label': 'AZ92A'}]}]}, {'label': 'Miscellaneous', 'children': [{'label': 'Unclassified', 'children': [{'label': 'Hafnium'}]}]}, {'label': 'Superalloy', 'children': [{'label': 'Nickel-base', 'children': [{'label': 'Hastelloy X', 'children': [{'label': 'Solution treated and aged'}]}, {'label': 'IN-100', 'children': [{'label': 'Misc. condition(s)'}]}]}]}, {'label': 'Zinc', 'children': [{'label': 'Zinc-aluminum', 'children': [{'label': 'AG40A', 'children': [{'label': 'Aged'}]}]}, {'label': 'Commercially pure or low-alloyed', 'children': [{'label': 'Zn-0.1Ti-0.1Cu'}]}]}, {'label': 'Nickel', 'children': [{'label': 'Nickel-copper', 'children': [{'label': 'Monel 400', 'children': [{'label': 'Cold worked'}]}]}, {'label': '(General)', 'children': [{'label': 'Nickel-base austenitic alloy'}]}]}, {'label': 'Lead', 'children': [{'label': 'Lead-tin', 'children': [{'label': 'Pb-2.6Sn-5.25 Sb-0.5As'}, {'label': 'Lead-base bearing alloy'}]}]}]}, {'label': 'Ceramic', 'children': [{'label': 'Oxide-base material', 'children': [{'label': 'Alumina, 90%'}]}]}]

上一篇 下一篇

猜你喜欢

热点阅读