AdminJS 中实现级联下拉菜单的解决方案
问题背景
在使用AdminJS构建管理后台时,开发者经常会遇到需要实现级联下拉菜单的需求。例如,当用户选择了一个"分类"后,第二个下拉菜单"子分类"需要根据第一个下拉菜单的选择结果动态过滤显示内容。
核心思路
AdminJS默认的Reference组件并不直接支持级联过滤功能。要实现这一需求,我们需要自定义一个Reference组件,在加载选项时根据父级下拉菜单的选择值进行过滤。
实现步骤
1. 创建自定义Reference组件
我们需要基于AdminJS的默认Reference组件进行扩展,主要修改loadOptions
方法来实现级联过滤功能。
import React, { FC, useState, useEffect, useMemo, memo } from 'react'
import { FormGroup, FormMessage, SelectAsync, Label } from '@adminjs/design-system'
import { ApiClient, EditPropertyProps, RecordJSON, flat, useTranslation } from 'adminjs'
type CombinedProps = EditPropertyProps
type SelectRecordEnhanced = {
record: RecordJSON;
label: string;
value: any;
}
const EditReference: FC<CombinedProps> = (props) => {
const { tp } = useTranslation()
const { onChange, property, record } = props
const { reference: resourceId } = property
if (!resourceId) {
throw new Error(`Cannot reference resource in property '${property.path}'`)
}
const handleChange = (selected: SelectRecordEnhanced): void => {
if (selected) {
onChange(property.path, selected.value, selected.record)
} else {
onChange(property.path, null)
}
}
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const response = await api.resourceAction({
resourceId,
actionName: 'search',
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
const { records: optionRecords } = response.data
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
// ...其余组件代码保持不变
}
2. 关键点解析
-
props.record.params:这个对象包含了当前表单中所有字段的值,我们可以通过它获取到父级下拉菜单的选择值。
-
api.resourceAction:我们使用这个方法而不是默认的searchRecords,因为它支持传递额外的过滤参数。
-
filters.categoryId:这里假设后端API支持通过categoryId参数过滤子分类。实际使用时需要根据你的后端API进行调整。
3. 注册自定义组件
创建好自定义组件后,需要通过AdminJS的ComponentLoader将其注册为特定属性的编辑器:
import { ComponentLoader } from 'adminjs'
const componentLoader = new ComponentLoader()
const Components = {
EditSubcategory: componentLoader.add('EditSubcategory', EditReference),
// ...其他自定义组件
}
然后在资源配置中指定使用这个自定义组件:
{
resource: YourResource,
options: {
properties: {
subcategory: {
components: {
edit: Components.EditSubcategory,
}
}
}
}
}
注意事项
-
后端API支持:确保你的后端API支持通过查询参数过滤结果。例如,对于子分类的搜索接口应该能够处理
filters.categoryId
这样的参数。 -
性能考虑:级联下拉菜单会触发额外的API调用,可以考虑添加防抖机制来优化性能。
-
空值处理:当父级下拉菜单没有选择值时,应该合理处理子菜单的显示逻辑,可以显示空列表或提示信息。
-
错误处理:增强组件的健壮性,处理API调用失败的情况。
扩展思考
这种级联下拉的实现方式不仅适用于分类和子分类的场景,还可以应用于:
- 国家/省份/城市三级联动
- 产品类别/子类别选择
- 部门/团队/成员选择
- 任何需要根据前一个选择动态过滤后一个选项的场景
通过这种自定义组件的方式,我们可以灵活地扩展AdminJS的功能,满足各种复杂的业务需求。
- DDeepSeek-V3.1-TerminusDeepSeek-V3.1-Terminus是V3的更新版,修复语言问题,并优化了代码与搜索智能体性能。Python00
- QQwen3-Omni-30B-A3B-InstructQwen3-Omni是多语言全模态模型,原生支持文本、图像、音视频输入,并实时生成语音。00
GitCode-文心大模型-智源研究院AI应用开发大赛
GitCode&文心大模型&智源研究院强强联合,发起的AI应用开发大赛;总奖池8W,单人最高可得价值3W奖励。快来参加吧~0267cinatra
c++20实现的跨平台、header only、跨平台的高性能http库。C++00AudioFly
AudioFly is a text-to-audio generation model based on the LDM architecture. It produces high-fidelity sounds at 44.1 kHz sampling rate with strong alignment to text prompts, suitable for sound effects, music, and multi-event audio synthesis tasks.Python00- HHunyuan-MT-7B腾讯混元翻译模型主要支持33种语言间的互译,包括中国五种少数民族语言。00
GOT-OCR-2.0-hf
阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile06
- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
热门内容推荐
最新内容推荐
项目优选









