일단 화면 등록폼 아이템화 까지.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<html layout:decorate="~{layout/layout-none}" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<div layout:fragment="content" th:classappend="'mypage-wrap'">
|
||||
<div layout:fragment="content" th:classappend="'mypage-wrap btm-fixbtn'">
|
||||
<main id="content" role="main">
|
||||
<div class="inner">
|
||||
<hgroup class="tit-group">
|
||||
@@ -14,34 +14,18 @@
|
||||
</hgroup>
|
||||
|
||||
<section class="regist-item-area">
|
||||
<p class="total" th:style="${currentList.totalElements == 0 ? 'display:none':''}">
|
||||
<span>총 <strong>1</strong>건</span>
|
||||
<button type="button" class="btn-reset">
|
||||
<p class="total" th:style="${currentList.totalElements == 0 ? 'display:none':''}" id="listHeader">
|
||||
<span>총 <strong id="listCount">1</strong>건</span>
|
||||
<button type="button" class="btn-reset" onclick="formControl.clearAllItems()">
|
||||
<i class="ico-reset ico-small" aria-hidden="true"></i>
|
||||
<span>초기화</span>
|
||||
</button>
|
||||
</p>
|
||||
<div class="item-list" th:style="${currentList.totalElements == 0 ? 'display:none':''}" id="listBody">
|
||||
<div class="list-item">
|
||||
<div class="item-tit-group">
|
||||
<p class="item-name">송이<span>400평</span> </p>
|
||||
<div class="btn-group">
|
||||
<button type="button" class="delete"><span class="hide-txt">삭제</span></button>
|
||||
<button type="button" class="edit"><span class="hide-txt">수정</span></button>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="detail-list">
|
||||
<li><span>서화면 서화리 161-1</span></li>
|
||||
<li><p>지목</p><span>임야</span></li>
|
||||
<li><p>가능면적</p><span>600평</span></li>
|
||||
<li><p>주생산물 수확량 / 판매량</p><span>35kg / 10kg</span></li>
|
||||
<li><p>부생산물 수확량 / 판매량</p><span>7kg / 2kg</span></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<button type="button" class="btn btn-m btn-secondary btn-full" style="margin-bottom:1.2rem" id="addItemBtn" onclick="formControl.addOpen()">
|
||||
<button type="button" class="btn btn-m btn-secondary btn-full" style="margin-bottom:1.2rem" id="addItemBtn" onclick="formControl.openAddForm()">
|
||||
<i class="ico-add ico-small" aria-hidden="true"></i>
|
||||
<span class="btn-txt">추가 등록</span>
|
||||
</button>
|
||||
@@ -53,7 +37,7 @@
|
||||
|
||||
<div class="component-input align-center" th:if="${currentList.totalElements == 0}" id="forestProductStatusDiv">
|
||||
<label class="checkbox-inner">
|
||||
<input type="checkbox" id="forestProductStatus" name="forestProductStatus" th:value="${MonthStatus.NOTHING}" onchange="formControl.handleCheckboxChange()">
|
||||
<input type="checkbox" id="forestProductStatus" name="forestProductStatus" th:value="${MonthStatus.NOTHING}" onchange="formControl.handleNoDataToggle()">
|
||||
<span class="checkbox-item">
|
||||
<span class="checkbox-txt">이번달 등록내용 없음</span>
|
||||
</span>
|
||||
@@ -102,6 +86,7 @@
|
||||
<div class="modal-content-inner">
|
||||
|
||||
<form id="forestForm">
|
||||
<input type="hidden" name="id" id="id">
|
||||
<article class="alert-box full green">
|
||||
<ul>
|
||||
<li>임업과 관련하여 재배 및 채취된 모든 임산물(목재 포함)을 기록해주세요.</li>
|
||||
@@ -170,7 +155,7 @@
|
||||
</label>
|
||||
</div>
|
||||
<div class="input-field">
|
||||
<input type="text" id="availableArea" placeholder="가능 면적을 입력해 주세요" name="availableArea" class="limitDecimalLength">
|
||||
<input type="number" id="availableArea" placeholder="가능 면적을 입력해 주세요" name="availableArea" class="limitDecimalLength">
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
@@ -201,7 +186,7 @@
|
||||
</label>
|
||||
</div>
|
||||
<div class="input-field">
|
||||
<input type="text" id="landArea" placeholder="작업 면적을 입력해 주세요" name="landArea" class="limitDecimalLength">
|
||||
<input type="number" id="landArea" placeholder="작업 면적을 입력해 주세요" name="landArea" class="limitDecimalLength">
|
||||
</div>
|
||||
<ul class="guide-info">
|
||||
<li>예 : 180, 6</li>
|
||||
@@ -398,7 +383,7 @@
|
||||
</section>
|
||||
<div class="component-btns">
|
||||
<div class="btns-row">
|
||||
<button type="button" class="btn btn-full btn-l btn-primary" onclick="formControl.addData()">
|
||||
<button type="button" class="btn btn-full btn-l btn-primary" onclick="formControl.saveFormData()">
|
||||
<span class="btn-txt">등록</span>
|
||||
</button>
|
||||
</div>
|
||||
@@ -429,151 +414,241 @@
|
||||
|
||||
<section layout:fragment="script">
|
||||
<script th:inline="javascript">
|
||||
// ✅ ENV 구조 (전역 설정 및 상태)
|
||||
let ENV = {
|
||||
httpUrl: {
|
||||
root: /*[[#{uri.front.info.monthly.path}]]*/'rootUrl'
|
||||
root: /*[[#{uri.front.info.monthly.path}]]*/ 'rootUrl'
|
||||
},
|
||||
module:{
|
||||
addPopup : null
|
||||
module: {
|
||||
modalPopup: null
|
||||
},
|
||||
data: {
|
||||
addProducts: []
|
||||
purposeList: /*[[${purposeList}]]*/ [],
|
||||
unitList: /*[[${unitList}]]*/ [],
|
||||
productList: []
|
||||
},
|
||||
target:{
|
||||
nothingCheck : document.getElementById("forestProductStatus"),
|
||||
addBtn:{
|
||||
elementRefs: {
|
||||
noDataCheckbox: document.getElementById("forestProductStatus"),
|
||||
addButton: {
|
||||
button: null,
|
||||
icon : null
|
||||
icon: null
|
||||
}
|
||||
},
|
||||
submitted: false,
|
||||
isSubmitted: false
|
||||
};
|
||||
let formControl = {
|
||||
init(){
|
||||
this._bindUI();
|
||||
this._listener();
|
||||
},
|
||||
_bindUI(){
|
||||
ENV.module.addPopup = document.querySelector(".addPopup").ui;
|
||||
// ENV.target.addBtn.button = document.getElementById("addItemBtn");
|
||||
// ENV.target.addBtn.icon = button.querySelector("i");
|
||||
|
||||
|
||||
const formControl = {
|
||||
init() {
|
||||
this.cacheElements();
|
||||
this.bindDecimalLimit();
|
||||
},
|
||||
_listener(){
|
||||
//소수점
|
||||
//모달창
|
||||
cacheElements() {
|
||||
ENV.module.modalPopup = document.querySelector(".addPopup").ui;
|
||||
},
|
||||
//숫자입력
|
||||
bindDecimalLimit() {
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const inputs = document.querySelectorAll(".limitDecimalLength");
|
||||
|
||||
inputs.forEach(input => {
|
||||
document.querySelectorAll(".limitDecimalLength").forEach(input => {
|
||||
input.addEventListener("input", () => {
|
||||
let val = input.value;
|
||||
|
||||
val = val.replace(/[^0-9.]/g, '');
|
||||
let val = input.value.replace(/[^0-9.]/g, '');
|
||||
const parts = val.split('.');
|
||||
if (parts.length > 2) {
|
||||
val = parts[0] + '.' + parts.slice(1).join('');
|
||||
}
|
||||
if (parts.length === 2) {
|
||||
parts[1] = parts[1].slice(0, 2);
|
||||
val = parts[0] + '.' + parts[1];
|
||||
}
|
||||
if (val.length > 10) {
|
||||
val = val.slice(0, 10);
|
||||
}
|
||||
|
||||
input.value = val;
|
||||
if (parts.length > 2) val = parts[0] + '.' + parts.slice(1).join('');
|
||||
if (parts[1]) parts[1] = parts[1].slice(0, 2);
|
||||
if (val.length > 10) val = val.slice(0, 10);
|
||||
input.value = parts.join('.');
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
checkNextBtn(){
|
||||
|
||||
//등록없음 선택에 따른 추가하기 버튼 잠금
|
||||
toggleAddButtonState(isEnabled) {
|
||||
const btn = document.getElementById("addItemBtn");
|
||||
const icon = btn.querySelector("i");
|
||||
btn.disabled = !isEnabled;
|
||||
icon.classList.toggle("ico-add", isEnabled);
|
||||
icon.classList.toggle("ico-add-disabled", !isEnabled);
|
||||
},
|
||||
// button
|
||||
//TODO 하나로 바꿔야함
|
||||
handleCheckboxChange(){
|
||||
const isChecked = document.getElementById("forestProductStatus").checked;
|
||||
this.addButtonOnOff(!isChecked);
|
||||
//등록없음 버튼
|
||||
handleNoDataToggle() {
|
||||
const isChecked = ENV.elementRefs.noDataCheckbox.checked;
|
||||
this.toggleAddButtonState(!isChecked);
|
||||
},
|
||||
addButtonOnOff(isEnabled){
|
||||
const button = document.getElementById("addItemBtn");
|
||||
const icon = button.querySelector("i");
|
||||
// 등록폼 추가
|
||||
openAddForm() {
|
||||
const modalContent = document.querySelector('.modal-content');
|
||||
setTimeout(() => { modalContent.scrollTop = 0; }, 0);
|
||||
|
||||
if (isEnabled) {
|
||||
button.disabled = false;
|
||||
icon.classList.remove("ico-add-disabled");
|
||||
icon.classList.add("ico-add");
|
||||
} else {
|
||||
button.disabled = true;
|
||||
icon.classList.remove("ico-add");
|
||||
icon.classList.add("ico-add-disabled");
|
||||
['landTypeSelect', 'mainYieldUnitCodeSelect', 'mainSaleUnitCodeSelect', 'subYieldUnitCodeSelect', 'subSaleUnitCodeSelect']
|
||||
.forEach(id => etUI.components.SelectBox().core.init(document.getElementById(id)));
|
||||
|
||||
ENV.module.modalPopup.open();
|
||||
},
|
||||
// selectBox선택값으로 변경
|
||||
setSelectBoxValue(selectBoxId, value) {
|
||||
const selectBox = document.getElementById(selectBoxId);
|
||||
const hiddenInput = selectBox.querySelector('input[type="hidden"]');
|
||||
const label = selectBox.querySelector('.select-txt');
|
||||
const option = selectBox.querySelector(`.option[data-value="${value}"]`);
|
||||
|
||||
if (option && hiddenInput && label) {
|
||||
selectBox.querySelectorAll('.option').forEach(opt => opt.classList.remove('current'));
|
||||
option.classList.add('current');
|
||||
hiddenInput.value = value;
|
||||
label.textContent = option.textContent;
|
||||
}
|
||||
},
|
||||
// button
|
||||
addOpen(){
|
||||
ENV.module.addPopup.open();
|
||||
},
|
||||
addData(){
|
||||
//등록 아이템 저장
|
||||
saveFormData() {
|
||||
const form = document.getElementById('forestForm');
|
||||
const selects = ['landType', 'mainYieldUnitCode', 'mainSaleUnitCode', 'subYieldUnitCode', 'subSaleUnitCode'];
|
||||
selects.forEach(id => {
|
||||
const sel = document.getElementById(id + 'Select');
|
||||
if (sel && sel.value) form[id].value = sel.value;
|
||||
});
|
||||
|
||||
const landTypeSelect = document.getElementById('ndTypeSelect');
|
||||
const mainYieldUnitCodeSelect = document.getElementById('mainYieldUnitCodeSelect');
|
||||
const mainSaleUnitCodeSelect = document.getElementById('mainSaleUnitCodeSelect');
|
||||
const subYieldUnitCodeSelect = document.getElementById('subYieldUnitCodeSelect');
|
||||
const subSaleUnitCodeSelect = document.getElementById('subSaleUnitCodeSelect');
|
||||
const errors = [];
|
||||
if (!form.cropName.value) errors.push({ field: 'cropName', message: '재배 작물명을 입력해주세요.' });
|
||||
if (form.mainYield.value && !form.mainYieldUnitCode.value) errors.push({ field: 'mainYieldUnitCode', message: '단위를 선택해주세요.' });
|
||||
if (form.mainSaleAmount.value && !form.mainSaleUnitCode.value) errors.push({ field: 'mainSaleUnitCode', message: '단위를 선택해주세요.' });
|
||||
if (form.subYield.value && !form.subYieldUnitCode.value) errors.push({ field: 'subYieldUnitCode', message: '단위를 선택해주세요.' });
|
||||
if (form.subSaleAmount.value && !form.subSaleUnitCode.value) errors.push({ field: 'subSaleUnitCode', message: '단위를 선택해주세요.' });
|
||||
|
||||
if (landTypeSelect && landTypeSelect.value) {
|
||||
form.landType.value = landTypeSelect.value;
|
||||
}
|
||||
if (mainYieldUnitCodeSelect && mainYieldUnitCodeSelect.value) {
|
||||
form.mainYieldUnitCode.value = mainYieldUnitCodeSelect.value;
|
||||
}
|
||||
if (mainSaleUnitCodeSelect && mainSaleUnitCodeSelect.value) {
|
||||
form.mainSaleUnitCode.value = mainSaleUnitCodeSelect.value;
|
||||
}
|
||||
if (subYieldUnitCodeSelect && subYieldUnitCodeSelect.value) {
|
||||
form.subYieldUnitCode.value = subYieldUnitCodeSelect.value;
|
||||
}
|
||||
if (subSaleUnitCodeSelect && subSaleUnitCodeSelect.value) {
|
||||
form.subSaleUnitCode.value = subSaleUnitCodeSelect.value;
|
||||
}
|
||||
kri.form.error(errors);
|
||||
if (errors.length > 0) return;
|
||||
|
||||
const errorData = [];
|
||||
const formData = new FormData(form);
|
||||
const product = {};
|
||||
formData.forEach((value, key) => product[key] = value);
|
||||
product.id = product.id ? Number(product.id) : Math.floor(Math.random() * 1000000) + 1;
|
||||
|
||||
if(!form.cropName.value) {
|
||||
const data = {field:'cropName', message: '재배 작물명을 입력해주세요.'}
|
||||
errorData.push(data);
|
||||
}
|
||||
if(form.mainYield.value && !form.mainYieldUnitCode.value) {
|
||||
const data = {field:'mainYieldUnitCode', message: '단위를 선택해주세요.'}
|
||||
errorData.push(data);
|
||||
}
|
||||
if(form.mainSaleAmount.value && !form.mainSaleUnitCode.value) {
|
||||
const data = {field:'mainSaleUnitCode', message: '단위를 선택해주세요.'}
|
||||
errorData.push(data);
|
||||
}
|
||||
const idx = ENV.data.productList.findIndex(item => item.id == product.id);
|
||||
if (idx !== -1) ENV.data.productList[idx] = product;
|
||||
else ENV.data.productList.push(product);
|
||||
|
||||
if(form.subYield.value && !form.subYieldUnitCode.value) {
|
||||
const data = {field:'subYieldUnitCode', message: '단위를 선택해주세요.'}
|
||||
errorData.push(data);
|
||||
}
|
||||
if(form.subSaleAmount.value && !form.subSaleUnitCode.value) {
|
||||
const data = {field:'subSaleUnitCode', message: '단위를 선택해주세요.'}
|
||||
errorData.push(data);
|
||||
}
|
||||
kri.form.error(errorData)
|
||||
ENV.module.modalPopup.close();
|
||||
setTimeout(() => productRenderer.addItem(product), 300);
|
||||
},
|
||||
// 선택 아이템 수정 (폼 노출)
|
||||
editItemById(id) {
|
||||
const product = ENV.data.productList.find(p => p.id == id);
|
||||
if (!product) return;
|
||||
|
||||
if(errorData.length > 0) {
|
||||
const formData = new FormData(form);
|
||||
const form = document.getElementById('forestForm');
|
||||
form.reset();
|
||||
|
||||
const data = {};
|
||||
formData.forEach((value, key) => {
|
||||
data[key] = value;
|
||||
});
|
||||
ENV.data.addProducts.push(data);
|
||||
}
|
||||
Object.entries(product).forEach(([key, val]) => {
|
||||
const field = form.elements.namedItem(key);
|
||||
if (field) field.value = val;
|
||||
});
|
||||
|
||||
// SelectBox 값 설정
|
||||
this.setSelectBoxValue('landTypeSelect', product.landType);
|
||||
this.setSelectBoxValue('mainYieldUnitCodeSelect', product.mainYieldUnitCode);
|
||||
this.setSelectBoxValue('mainSaleUnitCodeSelect', product.mainSaleUnitCode);
|
||||
this.setSelectBoxValue('subYieldUnitCodeSelect', product.subYieldUnitCode);
|
||||
this.setSelectBoxValue('subSaleUnitCodeSelect', product.subSaleUnitCode);
|
||||
|
||||
this.openAddForm();
|
||||
},
|
||||
// 선택 아이템 삭제
|
||||
deleteItemById(id) {
|
||||
ENV.data.productList = ENV.data.productList.filter(p => p.id != id);
|
||||
const target = document.querySelector(`#listBody div[data-id='${id}']`);
|
||||
if (target) target.remove();
|
||||
this.updateListHeader();
|
||||
},
|
||||
// 전체 초기화
|
||||
clearAllItems() {
|
||||
location.reload();
|
||||
// ENV.data.productList = [];
|
||||
// document.getElementById('listBody').innerHTML = '';
|
||||
// this.updateListHeader();
|
||||
},
|
||||
// 아이템 겟수 처리
|
||||
updateListHeader() {
|
||||
const header = document.getElementById('listHeader');
|
||||
const count = document.getElementById('listCount');
|
||||
const total = ENV.data.productList.length;
|
||||
header.style.display = total > 0 ? '' : 'none';
|
||||
count.textContent = total;
|
||||
},
|
||||
// 지목 코드
|
||||
getPurposeLabel(code) {
|
||||
const found = ENV.data.purposeList.find(i => i.id === code);
|
||||
return found ? found.codeText : '-';
|
||||
},
|
||||
// 단위 코드
|
||||
getUnitLabel(code) {
|
||||
const found = ENV.data.unitList.find(i => i.id === code);
|
||||
return found ? found.codeText : '-';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const productRenderer = {
|
||||
//화면에 아이템 추가
|
||||
addItem(product) {
|
||||
const listBody = document.getElementById('listBody');
|
||||
const form = document.getElementById('forestForm');
|
||||
form.reset();
|
||||
listBody.style.display = '';
|
||||
|
||||
const format = val => Number(val).toLocaleString() || '-';
|
||||
const getUnit = formControl.getUnitLabel;
|
||||
const getPurpose = formControl.getPurposeLabel;
|
||||
|
||||
const tag1 = product.landArea ? `<span>${format(product.landArea)}평</span>` : '';
|
||||
|
||||
let tag2 = '';
|
||||
if (product.town && product.streetNum) tag2 = `<li><span>${product.town} ${product.streetNum}</span></li>`;
|
||||
else if (product.town) tag2 = `<li><span>${product.town}</span></li>`;
|
||||
else if (product.streetNum) tag2 = `<li><span>${product.streetNum}</span></li>`;
|
||||
|
||||
const tag3 = product.landType ? `<li><p>지목</p><span>${getPurpose(product.landType)}</span></li>` : '';
|
||||
const tag4 = product.availableArea ? `<li><p>가능면적</p><span>${format(product.availableArea)}평</span></li>` : '';
|
||||
|
||||
let tag5 = '';
|
||||
if (product.mainYield && product.mainSaleAmount) {
|
||||
tag5 = `<li><p>주생산물 수확량 / 판매량</p><span>${format(product.mainYield)}${getUnit(product.mainYieldUnitCode)} / ${format(product.mainSaleAmount)}${getUnit(product.mainSaleUnitCode)}</span></li>`;
|
||||
} else if (product.mainYield) {
|
||||
tag5 = `<li><p>주생산물 수확량</p><span>${format(product.mainYield)}${getUnit(product.mainYieldUnitCode)}</span></li>`;
|
||||
} else if (product.mainSaleAmount) {
|
||||
tag5 = `<li><p>주생산물 판매량</p><span>${format(product.mainSaleAmount)}${getUnit(product.mainSaleUnitCode)}</span></li>`;
|
||||
}
|
||||
|
||||
let tag6 = '';
|
||||
if (product.subYield && product.subSaleAmount) {
|
||||
tag6 = `<li><p>부생산물 수확량 / 판매량</p><span>${format(product.subYield)}${getUnit(product.subYieldUnitCode)} / ${format(product.subSaleAmount)}${getUnit(product.subSaleUnitCode)}</span></li>`;
|
||||
} else if (product.subYield) {
|
||||
tag6 = `<li><p>부생산물 수확량</p><span>${format(product.subYield)}${getUnit(product.subYieldUnitCode)}</span></li>`;
|
||||
} else if (product.subSaleAmount) {
|
||||
tag6 = `<li><p>부생산물 판매량</p><span>${format(product.subSaleAmount)}${getUnit(product.subSaleUnitCode)}</span></li>`;
|
||||
}
|
||||
|
||||
const div = document.createElement('div');
|
||||
div.className = 'list-item';
|
||||
div.dataset.id = product.id;
|
||||
div.innerHTML = `
|
||||
<div class="item-tit-group">
|
||||
<p class="item-name">${product.cropName} ${tag1}</p>
|
||||
<div class="btn-group">
|
||||
<button type="button" class="delete" onclick="formControl.deleteItemById(${product.id})"><span class="hide-txt">삭제</span></button>
|
||||
<button type="button" class="edit" onclick="formControl.editItemById(${product.id})"><span class="hide-txt">수정</span></button>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="detail-list">${tag2}${tag3}${tag4}${tag5}${tag6}</ul>
|
||||
`;
|
||||
|
||||
const existing = listBody.querySelector(`[data-id="${product.id}"]`);
|
||||
existing ? listBody.replaceChild(div, existing) : listBody.appendChild(div);
|
||||
formControl.updateListHeader();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user