공통코드 셀렉트 박스 커밋밋
This commit is contained in:
parent
d18c97c992
commit
fbda37d9c2
@ -149,4 +149,15 @@ router.get("/select-options", async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 부모 코드명을 받아서 하위 자식들을 조회하는 엔드포인트
|
||||
router.get("/code-select-options", async (req, res, next) => {
|
||||
try {
|
||||
const { parentCodeName } = req.query;
|
||||
const options = await CommonService.getCodeSelectOptionsByParentName(parentCodeName);
|
||||
res.json(options);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
@ -1,4 +1,4 @@
|
||||
const { MenuInfo, CommCode, ProductGroup, sequelize } = require("../models"); // sequelize 임포트 추가
|
||||
const { MenuInfo, CommCode, sequelize } = require("../models"); // sequelize 임포트 추가
|
||||
const { Op } = require("sequelize");
|
||||
|
||||
|
||||
@ -237,6 +237,36 @@ const getSelectOptions = async (modelName, orderField = 'name') => {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// 부모 코드명을 받아서 하위 자식들을 조회하는 함수
|
||||
const getCodeSelectOptionsByParentName = async (parentCodeName) => {
|
||||
try {
|
||||
// 부모 코드명을 사용하여 부모 코드의 ID를 찾음
|
||||
const parentCode = await CommCode.findOne({
|
||||
where: { code_name: parentCodeName },
|
||||
raw: true,
|
||||
});
|
||||
|
||||
if (!parentCode) {
|
||||
throw new Error('Parent code not found');
|
||||
}
|
||||
|
||||
// 부모 코드의 ID를 사용하여 자식 코드를 조회
|
||||
const options = await CommCode.findAll({
|
||||
where: { parent_code_id: parentCode.id },
|
||||
order: [['code_name', 'ASC']],
|
||||
raw: true,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: options,
|
||||
};
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
//////========================================공통코드==============================================//////
|
||||
|
||||
module.exports = {
|
||||
@ -248,6 +278,6 @@ module.exports = {
|
||||
saveCode,
|
||||
deleteCode,
|
||||
toggleCodeActive,
|
||||
getProductGroups,
|
||||
getSelectOptions, // getSelectOptions 함수 추가
|
||||
getCodeSelectOptionsByParentName, // getCodeSelectOptionsByParentName 함수 추가
|
||||
};
|
@ -37,6 +37,7 @@ export const ProductForm: React.FC<ProductFormProps> = ({
|
||||
});
|
||||
|
||||
const [productGroups, setProductGroups] = useState([]);
|
||||
const [codeOptions, setCodeOptions] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchSelectOptions = async (modelName, orderField) => {
|
||||
@ -51,7 +52,20 @@ export const ProductForm: React.FC<ProductFormProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
const fetchCodeSelectOptions = async (parentCodeName) => {
|
||||
try {
|
||||
const response = await api.get("/api/v1/app/common/code-select-options", {
|
||||
params: { parentCodeName },
|
||||
});
|
||||
console.log("Fetched code select options:", response.data.data); // 디버깅 로그 추가
|
||||
setCodeOptions(response.data.data);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch code select options:", error);
|
||||
}
|
||||
};
|
||||
|
||||
fetchSelectOptions("ProductGroup", "product_group_name");
|
||||
fetchCodeSelectOptions("화폐단위"); // parentCodeName을 실제 부모 코드명으로 대체
|
||||
}, []);
|
||||
|
||||
return (
|
||||
@ -84,7 +98,35 @@ export const ProductForm: React.FC<ProductFormProps> = ({
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="code_option_id"
|
||||
validation={{
|
||||
required: "코드 옵션은 필수 입력값입니다",
|
||||
}}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>코드 옵션</FormLabel>
|
||||
<FormControl>
|
||||
<Select onValueChange={field.onChange} value={field.value}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="코드 옵션을 선택하세요" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{codeOptions.map((option) => (
|
||||
<SelectItem key={option.id} value={option.id}>
|
||||
{option.code_name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="product_code"
|
||||
|
Loading…
Reference in New Issue
Block a user