I have coded this mess of JavaScript and a bit of jQuery. Everything here works perfect as intended, however it is a real mess. What would be the best way to clean this up with best practices and such?
$.getJSON("People.json",
function(data) {
$.each(data.People, function(i, PersonObj) {
var Person = PersonObj[Object.keys(PersonObj)[0]];
content = '<tr>';
content += '<tbody>';
content += '<td>' + Person.Op + ' </td>';
// if the person has swiped off but not back on and one hour has elapsed
if (Person.DifHours > 1 && Person.ON === false) {
content += '<td style=" background: red;">' + Person.Name;
} else if (Person.OUT !== false) {
content += '<td style=" background: red;">' + Person.Name;
// If the person has swiped back on from dinner set to lime
} else if (Person.ON !== false) {
content += '<td style=" background: lime;">' + Person.Name;
// If the person has swipped of for dinner but 1 hour has not elapsed set to yellow
} else if (Person.OFF !== false) {
content += '<td style=" background: yellow;">' + Person.Name;
} else if (Person.Clock !== false) {
// If the person has clocked on set to lime
content += '<td style=" background: lime;">' + Person.Name;
} else {
content += '<td style=" background: Red;">' + Person.Name;
}
// putting values in to the table without formatting
content += '<td>' + Person.WorkHours + ' </td>';
content += '<td>' + Person.Start + ' </td>';
content += '<td>' + Person.End + ' </td>';
// if the person has not clocked in set the text to white
if (Person.Clock === false) {
content += '<td style=" background: white; color: white;">' + Person.Clock;
}
// if the person is late by 10 mins set to cyan
else if (Person.Late > 10) {
content += '<td style=" background: cyan;">' + Person.Clock;
// if the person is early by 10 mins set to orange
} else if (Person.Late < -10) {
content += '<td style=" background: Orange;">' + Person.Clock;
// Setting text color based on conditions
} else {
content += '<td style=" background: Lime;">' + Person.Clock + ' </td>';
}
// Setting text color based on conditions
if (Person.OFF === false) {
content += '<td style=" background: white; color: white;">' + Person.OFF;
} else {
content += '<td>' + Person.OFF + ' </td>';
}// Setting text color based on conditions
if (Person.ON === false) {
content += '<td style=" background: white; color: white;">' + Person.ON;
} else {
content += '<td>' + Person.ON + ' </td>';
}
if (Person.Clock == false){
content += '<td style=" background: white; color: white;">' + Person.OUT;
}else if (Person.DifHours > 1 && Person.ON == false && Person.HomeEarly > 10){
content += '<td style=" background: cyan;">' + Person.OFF;
}else if (Person.DifHours > 1 && Person.ON == false && Person.HomeEarly < - 10){
content += '<td style=" background: orange;">' + Person.OFF;
}else if(Person.DifHours > 1 && Person.ON == false){
content += '<td style=" background: lime;">' + Person.OFF;
}else if (Person.OUT === false) {
content += '<td style=" background: white; color: white;">' + Person.OUT;
}
//Setting home colum formatting based on if they are in or out and if they locked early or late
else if (Person.Home > 10) {
content += '<td style=" background: cyan;">' + Person.OUT;
} else if (Person.Home < -10) {
content += '<td style=" background: Orange;">' + Person.OUT;
}else {
content += '<td style=" background: Lime;">' + Person.OUT + ' </td>';
}
content += '</tr>';
content += '</tbody>';
content += '<p>';
$(content).appendTo("tbody");
});
});
Person.Name
won't have a<
in it (it isn't user input)? \$\endgroup\$td
element and then set its text, rather than concatenating the text into an HTML string). \$\endgroup\${...}
appears inside another{...}
. I'm not sure what you would call this situation. \$\endgroup\$