# 查询返回树状结构

以下语句效果是:查询已启用的菜单,并自动将子菜单合并到父菜单的children字段下

res = await vk.baseDao.selects({
  dbName: "opendb-admin-menus",
  pageIndex: 1,
  pageSize: 500,
  whereJson:{
    enable: true,
    parent_id: _.in([null, ""]),
    menu_id: _.exists(true)
  },
  sortArr: [{ name: "sort", type: "asc" }],
  // 树状结构参数
  treeProps: {
    id: "menu_id",          // 唯一标识字段,默认为 _id
    parent_id: "parent_id", // 父级标识字段,默认为 parent_id
    children: "children",   // 自定义返回的下级字段名,默认为 children
    level: 3,               // 查询返回的树的最大层级。超过设定层级的节点不会返回。默认10级,最大15,最小1
    limit: 500,             // 每一级最大返回的数据。
    whereJson: {
      enable: true
    }
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

注意:

  1. treeProps 内的 whereJson 若需要用到 orand

_.or 需写成 _.$.or

_.and 需写成 _.$.and

  1. foreignDB 属性需写在主表下,无需写在 treeProps 内。(子表会继承主表的 foreignDB 属性)