当前位置:   article > 正文

ant design 的 tree 如何作为角色中的权限选择之一

ant design 的 tree 如何作为角色中的权限选择之一

在这里插入图片描述
在这里插入图片描述
这种功能如何弄呢?

编辑的时候要让权限能选中哦。

<ProForm.Item name="permissions" label={intl.formatMessage({ id: 'permission_choose' })}>
          <Spin spinning={loading}>
            <Tree
              checkable
              onExpand={onExpand}
              expandedKeys={expandedKeys}
              autoExpandParent={autoExpandParent}
              onCheck={onCheck}
              checkedKeys={checkedKeys}
              onSelect={onSelect}
              selectedKeys={selectedKeys}
              treeData={permissionGroups} // Use filtered top-level groups
              fieldNames={{ title: 'name', key: '_id', children: 'children' }}
            />
          </Spin>
        </ProForm.Item>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

首先这个地方,你要使用 tree 这个组件。

主要是这个 permissionGroups 的数据从哪里来

给看一下响应出来的数据:

在这里插入图片描述
这是具体数据:

{
    "success": true,
    "data": [
        {
            "_id": "66b1b54ef8871ea52a7e3de9",
            "name": "认证管理",
            "createdAt": "2024-08-06T05:31:58.495Z",
            "updatedAt": "2024-08-10T02:24:31.070Z",
            "__v": 0,
            "children": [
                {
                    "_id": "66b1b00bb5d937a0aef34034",
                    "name": "权限",
                    "createdAt": "2024-08-06T05:09:31.292Z",
                    "updatedAt": "2024-08-10T02:24:41.759Z",
                    "__v": 0,
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "children": [
                        {
                            "_id": "66adee8cd22d6d5b1ce00780",
                            "name": "更新权限",
                            "path": "/permissions/:id",
                            "action": "PUT",
                            "permissionGroup": "66b1b00bb5d937a0aef34034",
                            "createdAt": "2024-08-03T08:47:08.777Z",
                            "updatedAt": "2024-08-10T02:38:15.837Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b1a12b0e10340bd8bbeba0",
                            "name": "删除权限",
                            "path": "/permissions",
                            "action": "DELETE",
                            "createdAt": "2024-08-06T04:06:03.752Z",
                            "updatedAt": "2024-08-10T02:31:07.287Z",
                            "__v": 0,
                            "permissionGroup": "66b1b00bb5d937a0aef34034"
                        },
                        {
                            "_id": "66b1c55141364c27c464f858",
                            "name": "查看权限",
                            "path": "/permissions",
                            "action": "GET",
                            "permissionGroup": "66b1b00bb5d937a0aef34034",
                            "createdAt": "2024-08-06T06:40:17.991Z",
                            "updatedAt": "2024-08-10T08:03:27.245Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6cf51aa92a3526285b14d",
                            "name": "添加权限",
                            "path": "/permissions",
                            "action": "POST",
                            "createdAt": "2024-08-10T02:24:17.940Z",
                            "updatedAt": "2024-08-10T02:30:22.189Z",
                            "__v": 0,
                            "permissionGroup": "66b1b00bb5d937a0aef34034"
                        }
                    ]
                },
                {
                    "_id": "66b6d2c9b9ad87dfa985f34f",
                    "name": "用户",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-10T02:39:05.563Z",
                    "updatedAt": "2024-08-10T02:39:05.563Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b6d339b9ad87dfa985f3dd",
                            "name": "添加用户",
                            "path": "/users",
                            "action": "POST",
                            "permissionGroup": "66b6d2c9b9ad87dfa985f34f",
                            "createdAt": "2024-08-10T02:40:57.583Z",
                            "updatedAt": "2024-08-10T02:41:30.112Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d352b9ad87dfa985f3f0",
                            "name": "查看用户",
                            "path": "/users",
                            "action": "GET",
                            "permissionGroup": "66b6d2c9b9ad87dfa985f34f",
                            "createdAt": "2024-08-10T02:41:22.895Z",
                            "updatedAt": "2024-08-10T08:03:22.477Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d368b9ad87dfa985f416",
                            "name": "删除用户",
                            "path": "/users",
                            "action": "DELETE",
                            "permissionGroup": "66b6d2c9b9ad87dfa985f34f",
                            "createdAt": "2024-08-10T02:41:44.912Z",
                            "updatedAt": "2024-08-10T02:41:44.912Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d37bb9ad87dfa985f429",
                            "name": "更新用户",
                            "path": "/users/:id",
                            "action": "PUT",
                            "permissionGroup": "66b6d2c9b9ad87dfa985f34f",
                            "createdAt": "2024-08-10T02:42:03.242Z",
                            "updatedAt": "2024-08-10T02:45:40.000Z",
                            "__v": 0
                        }
                    ]
                },
                {
                    "_id": "66b6d2ddb9ad87dfa985f362",
                    "name": "菜单",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-10T02:39:25.628Z",
                    "updatedAt": "2024-08-10T02:39:25.628Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b6d440b9ad87dfa985f488",
                            "name": "添加菜单",
                            "path": "/menus",
                            "action": "POST",
                            "permissionGroup": "66b6d2ddb9ad87dfa985f362",
                            "createdAt": "2024-08-10T02:45:20.021Z",
                            "updatedAt": "2024-08-10T02:45:20.021Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d46cb9ad87dfa985f4c1",
                            "name": "删除菜单",
                            "path": "/menus",
                            "action": "DELETE",
                            "permissionGroup": "66b6d2ddb9ad87dfa985f362",
                            "createdAt": "2024-08-10T02:46:04.896Z",
                            "updatedAt": "2024-08-10T02:46:04.896Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d47db9ad87dfa985f4d4",
                            "name": "更新菜单",
                            "path": "/menus/:id",
                            "action": "PUT",
                            "permissionGroup": "66b6d2ddb9ad87dfa985f362",
                            "createdAt": "2024-08-10T02:46:21.612Z",
                            "updatedAt": "2024-08-10T02:46:52.140Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d48bb9ad87dfa985f4e7",
                            "name": "查看菜单",
                            "path": "/menus",
                            "action": "GET",
                            "permissionGroup": "66b6d2ddb9ad87dfa985f362",
                            "createdAt": "2024-08-10T02:46:35.896Z",
                            "updatedAt": "2024-08-10T08:03:13.698Z",
                            "__v": 0
                        }
                    ]
                },
                {
                    "_id": "66b6d2e9b9ad87dfa985f377",
                    "name": "角色",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-10T02:39:37.339Z",
                    "updatedAt": "2024-08-10T02:39:37.339Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b6d39eb9ad87dfa985f43c",
                            "name": "添加角色",
                            "path": "/roles",
                            "action": "POST",
                            "permissionGroup": "66b6d2e9b9ad87dfa985f377",
                            "createdAt": "2024-08-10T02:42:38.531Z",
                            "updatedAt": "2024-08-10T02:42:38.531Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d3dfb9ad87dfa985f44f",
                            "name": "删除角色",
                            "path": "/roles",
                            "action": "DELETE",
                            "permissionGroup": "66b6d2e9b9ad87dfa985f377",
                            "createdAt": "2024-08-10T02:43:43.882Z",
                            "updatedAt": "2024-08-10T02:43:43.882Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d3fab9ad87dfa985f462",
                            "name": "更新角色",
                            "path": "/roles/:id",
                            "action": "PUT",
                            "permissionGroup": "66b6d2e9b9ad87dfa985f377",
                            "createdAt": "2024-08-10T02:44:10.845Z",
                            "updatedAt": "2024-08-10T02:45:31.647Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d40db9ad87dfa985f475",
                            "name": "查看角色",
                            "path": "/roles",
                            "action": "GET",
                            "permissionGroup": "66b6d2e9b9ad87dfa985f377",
                            "createdAt": "2024-08-10T02:44:29.797Z",
                            "updatedAt": "2024-08-10T08:03:18.669Z",
                            "__v": 0
                        }
                    ]
                },
                {
                    "_id": "66b6d2fdb9ad87dfa985f38e",
                    "name": "数据权限",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-10T02:39:57.756Z",
                    "updatedAt": "2024-08-10T02:39:57.756Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b6d544b9ad87dfa985f559",
                            "name": "添加数据权限",
                            "path": "/data-permissions",
                            "action": "POST",
                            "permissionGroup": "66b6d2fdb9ad87dfa985f38e",
                            "createdAt": "2024-08-10T02:49:40.379Z",
                            "updatedAt": "2024-08-10T02:49:40.379Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d559b9ad87dfa985f56c",
                            "name": "删除数据权限",
                            "path": "/data-permissions",
                            "action": "DELETE",
                            "permissionGroup": "66b6d2fdb9ad87dfa985f38e",
                            "createdAt": "2024-08-10T02:50:01.137Z",
                            "updatedAt": "2024-08-10T02:50:01.137Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d578b9ad87dfa985f57f",
                            "name": "更新数据权限",
                            "path": "/data-permissions/:id",
                            "action": "PUT",
                            "permissionGroup": "66b6d2fdb9ad87dfa985f38e",
                            "createdAt": "2024-08-10T02:50:32.533Z",
                            "updatedAt": "2024-08-10T02:50:32.533Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d586b9ad87dfa985f592",
                            "name": "查看数据权限",
                            "path": "/data-permissions",
                            "action": "GET",
                            "permissionGroup": "66b6d2fdb9ad87dfa985f38e",
                            "createdAt": "2024-08-10T02:50:46.780Z",
                            "updatedAt": "2024-08-10T08:03:04.925Z",
                            "__v": 0
                        }
                    ]
                },
                {
                    "_id": "66b6d314b9ad87dfa985f3a7",
                    "name": "权限组",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-10T02:40:20.528Z",
                    "updatedAt": "2024-08-10T02:40:20.528Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b6d4bdb9ad87dfa985f50d",
                            "name": "添加权限组",
                            "path": "/permission-groups",
                            "action": "POST",
                            "permissionGroup": "66b6d314b9ad87dfa985f3a7",
                            "createdAt": "2024-08-10T02:47:25.139Z",
                            "updatedAt": "2024-08-10T02:47:25.139Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d500b9ad87dfa985f520",
                            "name": "删除权限组",
                            "path": "/permission-groups",
                            "action": "DELETE",
                            "permissionGroup": "66b6d314b9ad87dfa985f3a7",
                            "createdAt": "2024-08-10T02:48:32.481Z",
                            "updatedAt": "2024-08-10T02:48:32.481Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d519b9ad87dfa985f533",
                            "name": "更新权限组",
                            "path": "/permission-groups/:id",
                            "action": "PUT",
                            "permissionGroup": "66b6d314b9ad87dfa985f3a7",
                            "createdAt": "2024-08-10T02:48:57.720Z",
                            "updatedAt": "2024-08-10T02:48:57.720Z",
                            "__v": 0
                        },
                        {
                            "_id": "66b6d52cb9ad87dfa985f546",
                            "name": "查看权限组",
                            "path": "/permission-groups",
                            "action": "GET",
                            "permissionGroup": "66b6d314b9ad87dfa985f3a7",
                            "createdAt": "2024-08-10T02:49:16.624Z",
                            "updatedAt": "2024-08-10T08:03:09.517Z",
                            "__v": 0
                        }
                    ]
                },
                {
                    "_id": "66b9ad348554e602536acc67",
                    "name": "认证管理菜单",
                    "parent": {
                        "_id": "66b1b54ef8871ea52a7e3de9",
                        "name": "认证管理",
                        "createdAt": "2024-08-06T05:31:58.495Z",
                        "updatedAt": "2024-08-10T02:24:31.070Z",
                        "__v": 0
                    },
                    "createdAt": "2024-08-12T06:35:32.560Z",
                    "updatedAt": "2024-08-12T06:35:32.560Z",
                    "__v": 0,
                    "children": [
                        {
                            "_id": "66b9ad528554e602536acc84",
                            "name": "授权管理菜单",
                            "path": "/auth",
                            "action": "GET",
                            "permissionGroup": "66b9ad348554e602536acc67",
                            "createdAt": "2024-08-12T06:36:02.754Z",
                            "updatedAt": "2024-08-12T06:36:02.754Z",
                            "__v": 0
                        }
                    ]
                }
            ]
        },
        {
            "_id": "66adec30d647a4fde5546b1c",
            "name": "材料类目",
            "createdAt": "2024-08-03T08:37:04.433Z",
            "updatedAt": "2024-08-10T02:24:51.188Z",
            "__v": 0,
            "children": [
                {
                    "_id": "66b6d7a5b9ad87dfa985f749",
                    "name": "添加材料类目",
                    "path": "/material-categories",
                    "action": "POST",
                    "permissionGroup": "66adec30d647a4fde5546b1c",
                    "createdAt": "2024-08-10T02:59:49.106Z",
                    "updatedAt": "2024-08-10T07:36:39.702Z",
                    "__v": 0
                },
                {
                    "_id": "66b6d7b4b9ad87dfa985f75c",
                    "name": "删除材料类目",
                    "path": "/material-categories",
                    "action": "DELETE",
                    "permissionGroup": "66adec30d647a4fde5546b1c",
                    "createdAt": "2024-08-10T03:00:04.930Z",
                    "updatedAt": "2024-08-10T07:36:36.183Z",
                    "__v": 0
                },
                {
                    "_id": "66b6d7c4b9ad87dfa985f76f",
                    "name": "更新材料类目",
                    "path": "/material-categories/:id",
                    "action": "PUT",
                    "permissionGroup": "66adec30d647a4fde5546b1c",
                    "createdAt": "2024-08-10T03:00:20.075Z",
                    "updatedAt": "2024-08-10T07:36:32.789Z",
                    "__v": 0
                },
                {
                    "_id": "66b6d7d0b9ad87dfa985f782",
                    "name": "查看材料类目",
                    "path": "/material-categories",
                    "action": "GET",
                    "permissionGroup": "66adec30d647a4fde5546b1c",
                    "createdAt": "2024-08-10T03:00:32.932Z",
                    "updatedAt": "2024-08-10T08:02:59.634Z",
                    "__v": 0
                }
            ]
        }
    ],
    "total": 2
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426

组件好办,主要是后端这个数据结构,要对上。

name children ,这些要跟组件的参数对上。

无论你的后端如何写,你返回的数据一定要对上:

          fieldNames={{ title: 'name', key: '_id', children: 'children' }}
  • 1

这是第一步。

下一篇https://editor.csdn.net/md/?articleId=141324925

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1007909
推荐阅读
相关标签
  

闽ICP备14008679号