I have build a dynamic HTML table, that is getting data from Localstorage. My problem is that the table is not updating when I am updating my class (I get the old table and I make changes in the employeelist email, it won't update in the new table) I do not get any errors. But when I check my Localstorage in my console, it shows the old one....
constructor(name, gender, department, yy, email, skills) {
this.name = name;
this.gender = gender;
this.department = department;
this.yy = yy;
this.email = email;
this.skills = skills
}
}
//Employee Database "Localstorage"
if(localStorage.getItem("Employee") == null) {
var employeeList = [];
employeeList.push (new Employee("Simon", "Male", "HR", 1999, "SM@cbs.dk", "IT"));
employeeList.push (new Employee("Mads", "Male","IT", 1999, "MS@cbs.dk", "Finance"));
employeeList.push (new Employee("Jessica", "Female", "Sales",1998, "JT@cbs.dk", "HR"));
employeeList.push (new Employee("Benjamin", "Male","IT", 1997, "BN@cbs.dk", "Sales"));
var employeeListString = JSON.stringify(employeeList);
localStorage.setItem("Employee", employeeListString);
document.querySelector('#employees').appendChild(buildTable(employeeList));
} else {
var employeeList = JSON.parse(localStorage.getItem("Employee"));
}
//Function creates table for employeeList
function buildTable(data) {
let table = document.createElement("table");
// Create table head and body
table.appendChild(document.createElement("thead"));
table.appendChild(document.createElement("tbody"));
let fields = Object.keys(data[0]);
let headRow = document.createElement("tr");
fields.forEach(function (field) {
let headCell = document.createElement("th");
headCell.textContent = field;
headRow.appendChild(headCell);
});
table.querySelector("thead").appendChild(headRow);
data.forEach(function (object) {
let row = document.createElement("tr");
fields.forEach(function (field) {
let cell = document.createElement("td");
cell.textContent = object[field];
if (typeof object[field] == "number") {
cell.style.textAlign = "right";
}
row.appendChild(cell);
});
table.querySelector("tbody").appendChild(row);
});
return table;
}
document.querySelector("#employees").appendChild(buildTable(employeeList));