zoukankan      html  css  js  c++  java
  • Python 递归返回树形菜单JSON串 <flask>

    需求:菜单管理功能(增、删、改),多级树形菜单展示

    数据库表设计

    create table if not exists Menu(
        id serial  primary key ,
        title varchar (50) not null ,
        parent_id int ,
        url varchar (100),
        app_id int ,
        icon varchar (50),
        sortby int default 0,
        create_time timestamp,
        update_time timestamp,
        foreign key (parent_id) references Menu(id) on delete cascade on update cascade
    );

    Model 定义

    class Menu:
        def __init__(self, id, title, parent_id, url, app_id, icon, sort_by, create_time, update_time, subMenus):
            self._id = id
            self._title = title
            self._parent_id = parent_id
            self._url = url
            self._app_id = app_id
            self._icon = icon
            self._sortby = sort_by
            self._create_time = create_time
            self._update_time = update_time
            self._subMenus = subMenus
    
        @property
        def id(self):
            return self._id
        @property
        def title(self):
            return self._title
        @property
        def parent_id(self):
            return self._parent_id
        @property
        def url(self):
            return self._url
        @property
        def app_id(self):
            return self._app_id
        @property
        def icon(self):
            return self._icon
        @property
        def sortby(self):
            return self._sortby
        @property
        def subMenus(self):
            return self._subMenus

    理论上Models应该中的参数应该和数据库表字段对应,但是subMenus我们不需要存库,反给前端用的。

    核心代码

    def set_subMenus(id, menus):
            """
            :param id: 父id
            :param subMenu:子菜单列表
            :return: 没有子菜单返回None 有子菜单返回子菜单列表
            """
            try:
                _subMenus = []
    
                for menu in menus:
                    if menu.parent_id == id:
                        _subMenus.append(menu)
                for sub in _subMenus:
                    menu2 = _query_sub_menu_info(sub.id)
                    if len(menus):
                        sub._subMenus = set_subMenus(sub.id, menu2)
                    else:
                        sub.__delattr__('_subMenus')
                # 子菜单列表不为空
                if len(_subMenus):
                    # print(sub_list)
                    return _subMenus
                else:  # 没有子菜单了
                    return None
            except Exception as e:
                LOG.exception('error query_sub_menu_info !')
                raise e
    _query_sub_menu_info 方法是查询返回

    测试方法:
    def test_sub_menu_list(self):
            Menus = []
            rootMenu = GuardianModelDao._query_root_menu_info()
            for root in rootMenu:
                subMenu = GuardianModelDao._query_sub_menu_info(root.id)
                root = jsonpickle.loads(jsonpickle.encode(root))
                if root.subMenus is not None:
                    root.subMenus.append(GuardianModelDao.set_subMenus(root.id, subMenu))
                    Menus.append(root)
     

     

  • 相关阅读:
    k8spod资源的基础管理操作
    k8s名称空间资源
    bzoj5011: [Jx2017]颜色
    bzoj5010: [Fjoi2017]矩阵填数
    bzoj5008: 方师傅的房子
    bzoj5007: TCP协议
    bzoj5003: 与链 5004: 开锁魔法II 5005:乒乓游戏
    bzoj5020: [THUWC 2017]在美妙的数学王国中畅游
    bzoj5006: [THUWC2017 Bipartite]随机二分图
    bzoj4480: [Jsoi2013]快乐的jyy
  • 原文地址:https://www.cnblogs.com/tsbc/p/8432970.html
Copyright © 2011-2022 走看看