JavaScript
const data = [
{ id: 1, name: '노트북', price: 1200000, stock: 15 },
{ id: 2, name: '모니터', price: 350000, stock: 32 },
{ id: 3, name: '키보드', price: 89000, stock: 120 },
{ id: 4, name: '마우스', price: 45000, stock: 200 }
];
const view = new wijmo.collections.CollectionView(data, { trackChanges: true });
const grid = new wijmo.grid.FlexGrid('#myGrid', {
itemsSource: view,
allowAddNew: true,
allowDelete: true
});
// 변경 건수 요약
function updateSummary() {
document.getElementById('summary').textContent =
'추가 ' + view.itemsAdded.length +
'건 / 수정 ' + view.itemsEdited.length +
'건 / 삭제 ' + view.itemsRemoved.length + '건';
}
// 변경 목록이 바뀔 때마다 요약과 그리드를 갱신
[view.itemsAdded, view.itemsEdited, view.itemsRemoved].forEach(function (list) {
list.collectionChanged.addHandler(function () {
updateSummary();
grid.invalidate();
});
});
// 추가·수정된 행 강조
grid.formatItem.addHandler(function (s, e) {
if (e.panel === s.cells) {
const item = s.rows[e.row].dataItem;
if (view.itemsAdded.indexOf(item) > -1) {
e.cell.style.backgroundColor = '#d9f2d9';
} else if (view.itemsEdited.indexOf(item) > -1) {
e.cell.style.backgroundColor = '#fff3cd';
} else {
e.cell.style.backgroundColor = '';
}
}
});
// 변경분만 서버로 일괄 전송
async function saveChanges() {
const changes = {
added: view.itemsAdded.slice(),
edited: view.itemsEdited.slice(),
removed: view.itemsRemoved.slice()
};
const count = changes.added.length + changes.edited.length + changes.removed.length;
const payload = document.getElementById('payload');
if (count === 0) {
payload.textContent = '저장할 변경 사항이 없습니다.';
return;
}
payload.textContent = JSON.stringify(changes, null, 2);
try {
// 데모용 테스트 엔드포인트 — 실제 서비스에서는 자체 API 주소로 교체
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(changes)
});
if (!res.ok) throw new Error('HTTP ' + res.status);
view.clearChanges(); // 전송 성공 후에만 추적 목록 초기화
updateSummary();
grid.invalidate();
} catch (err) {
payload.textContent += '\n\n전송 실패: ' + err.message + ' (변경 이력은 유지됩니다)';
}
}
document.getElementById('btnSave').addEventListener('click', saveChanges);
updateSummary();