[BO] 공통코드 지역/세부지역, 업종대분류/업종중분류 - 계층형으로 일괄 변경
This commit is contained in:
@@ -470,3 +470,50 @@ const limitMaxLength = (el) => {
|
||||
el.value = el.value.replace(/\D/g, '').slice(0, maxLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 공통코드 기반의 2단계 셀렉트박스(부모/자식) UI를 구성하는 함수
|
||||
*
|
||||
* @param {Object} codeList - 공통코드 리스트 객체
|
||||
* @param {string} parentSelector - 부모 셀렉트박스 셀렉터 (ex. '#regionSubCode')
|
||||
* @param {string} childSelector - 자식 셀렉트박스 셀렉터 (ex. '#regionDetailCode')
|
||||
* @param {string} selectedParentCode - 초기 선택할 부모 코드값 (ex. 'code.region.sub.31')
|
||||
* @param {string} selectedChildCode - 초기 선택할 자식 코드값 (ex. 'code.region.detail.9')
|
||||
*/
|
||||
const setSelectBoxUiCode = ({
|
||||
codeList,
|
||||
parentSelector,
|
||||
childSelector,
|
||||
selectedParentCode = '',
|
||||
selectedChildCode = ''
|
||||
}) => {
|
||||
const $parent = $(parentSelector);
|
||||
const $child = $(childSelector);
|
||||
|
||||
if (!codeList?.nodes || !$parent.length || !$child.length) return;
|
||||
|
||||
const renderOptions = ($el, list, selected) => {
|
||||
$el.html('<option value="">전체</option>');
|
||||
list.forEach(item => {
|
||||
const val = item.id.split('.').pop();
|
||||
const isSelected = item.id === selected ? 'selected' : '';
|
||||
$el.append(`<option value="${item.id}" ${isSelected}>${val}. ${item.codeText}</option>`);
|
||||
});
|
||||
};
|
||||
|
||||
const loadChildren = (parentId, selectedChild = '') => {
|
||||
$child.html('<option value="">전체</option>');
|
||||
if (!parentId) return;
|
||||
|
||||
const parent = codeList.nodes.find(n => n.id === parentId);
|
||||
if (!parent?.nodes) return;
|
||||
renderOptions($child, parent.nodes, selectedChild);
|
||||
};
|
||||
|
||||
renderOptions($parent, codeList.nodes, selectedParentCode);
|
||||
if (selectedParentCode) loadChildren(selectedParentCode, selectedChildCode);
|
||||
|
||||
$parent.on('change', function () {
|
||||
loadChildren($(this).val());
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user