my lastSavedData is getting overwrite whenever i chang uiData, what is the issue?async…
- User
my lastSavedData is getting overwrite whenever i chang uiData, what is the issue?async function fetchData(url) { try { const res = await fetch(url); const json = await res.json(); return json } catch (e) { console.log(e) return [] } }
function createCell(data) { const cell = document.createElement('td'); cell.textContent = data; return cell }
function createRow(rowData, index) { const row = document.createElement('tr');
const selectBox = createCell('✓'); selectBox.classList.add('select-box'); selectBox.dataset.id = rowData['id'] row.appendChild(selectBox); row.dataset.rowId = rowData['id'] const rowNumber = createCell(index + 1); rowNumber.dataset.rowIndex = index + 1 row.appendChild(rowNumber) for (const key in rowData) { if (key === 'id') { continue } const cell = createCell(rowData[key]) cell.dataset.property = key if (editableFields.includes(key)) { cell.contentEditable = "true" } row.appendChild(cell); } return row}
function createTableBody(arr) { const fragment = document.createDocumentFragment();
const tbody = document.createElement('tbody'); fragment.appendChild(tbody) arr.forEach((el, i) => { const row = createRow(el, i) tbody.appendChild(row); }); return fragment}
function addNewEntry() { const item = { "id": new Date().getTime(), "chemical_name": "", "vendor": "", "density": "", "viscosity": "", "packaging": "", "pack_size": "", "unit": "", "quantity": 0 }
const tbody = table.lastElementChild; const row = createRow(item, tbody.children.length) tbody.appendChild(row)}
function selectRow(el) { const cell = el
if (!cell.classList.contains('select-box')) { return } const row = cell.parentElement if (row.classList.contains('active')) { row.classList.remove('active') selectedRow = null selectedRowId = null } else { const rows = table.lastElementChild.children for (let i = 0; i < rows.length; i++) { rows[i].classList.remove('active') } row.classList.add('active') selectedRowId = parseInt(cell.dataset.id) selectedRow = row }}
function deleteRow() { selectedRow?.remove(); selectedRow = null
// const itemIndex = uiData.findIndex(el => el.id === selectedRowId) // uiData.splice(itemIndex, 1) selectedRowId = null;}
function moveRowUp(item = selectedRow) { if (selectedRowId === null) { return } const previousItem = item.previousElementSibling;
if (previousItem) { item.parentNode.insertBefore(item, previousItem); swapRowIndex(previousItem, item) }}
function moveRowDown(item = selectedRow) { if (selectedRowId === null) { return }
- Assistant
The issue you're experiencing with
lastSavedDatabeing overwritten whenever you changeuiDatais likely due to how JavaScript handles object references and arrays. In your code, when you assignuiDatatolastSavedData: - User
- Assistant