Displaying information extracted from an HTML table
This function below (used on a timeout due to my page loading time) displays information on my site. I grab this data from my table view that I load into the page. It is very clunky. As you can see in the code, certain values (current year, costume_exists boolean value) change the output of data being displayed. Is there an easier or cleaner way of doing this?
setTimeout(function() {
var table = document.getElementById("bands");
var icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
var $row = $(this).closest("tr");
var $year = $row.find(".year").text();
var $prize = $row.find(".prize").text();
var $band = $row.find(".band").text();
var $mp = $row.find(".mp").text();
var $ge_music = $row.find(".ge_music").text();
var $vp = $row.find(".vp").text();
var $ge_visual = $row.find(".ge_visual").text();
var $costume = $row.find(".costume").text();
var $total = $row.find(".total").text();
var $costumer = $row.find(".costumer").text();
var $designer = $row.find(".designer").text();
var $arranger = $row.find(".arranger").text();
var $choreographer = $row.find(".choreographer").text();
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
breakdown = "breakdown"
if ($year < 1991 && costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (playing_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else {
alert("No point breakdowns for " + $year + " are available.");
}
}
}
}
}
};
}, 700);
javascript jquery html formatting dom
add a comment |
This function below (used on a timeout due to my page loading time) displays information on my site. I grab this data from my table view that I load into the page. It is very clunky. As you can see in the code, certain values (current year, costume_exists boolean value) change the output of data being displayed. Is there an easier or cleaner way of doing this?
setTimeout(function() {
var table = document.getElementById("bands");
var icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
var $row = $(this).closest("tr");
var $year = $row.find(".year").text();
var $prize = $row.find(".prize").text();
var $band = $row.find(".band").text();
var $mp = $row.find(".mp").text();
var $ge_music = $row.find(".ge_music").text();
var $vp = $row.find(".vp").text();
var $ge_visual = $row.find(".ge_visual").text();
var $costume = $row.find(".costume").text();
var $total = $row.find(".total").text();
var $costumer = $row.find(".costumer").text();
var $designer = $row.find(".designer").text();
var $arranger = $row.find(".arranger").text();
var $choreographer = $row.find(".choreographer").text();
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
breakdown = "breakdown"
if ($year < 1991 && costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (playing_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else {
alert("No point breakdowns for " + $year + " are available.");
}
}
}
}
}
};
}, 700);
javascript jquery html formatting dom
add a comment |
This function below (used on a timeout due to my page loading time) displays information on my site. I grab this data from my table view that I load into the page. It is very clunky. As you can see in the code, certain values (current year, costume_exists boolean value) change the output of data being displayed. Is there an easier or cleaner way of doing this?
setTimeout(function() {
var table = document.getElementById("bands");
var icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
var $row = $(this).closest("tr");
var $year = $row.find(".year").text();
var $prize = $row.find(".prize").text();
var $band = $row.find(".band").text();
var $mp = $row.find(".mp").text();
var $ge_music = $row.find(".ge_music").text();
var $vp = $row.find(".vp").text();
var $ge_visual = $row.find(".ge_visual").text();
var $costume = $row.find(".costume").text();
var $total = $row.find(".total").text();
var $costumer = $row.find(".costumer").text();
var $designer = $row.find(".designer").text();
var $arranger = $row.find(".arranger").text();
var $choreographer = $row.find(".choreographer").text();
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
breakdown = "breakdown"
if ($year < 1991 && costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (playing_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else {
alert("No point breakdowns for " + $year + " are available.");
}
}
}
}
}
};
}, 700);
javascript jquery html formatting dom
This function below (used on a timeout due to my page loading time) displays information on my site. I grab this data from my table view that I load into the page. It is very clunky. As you can see in the code, certain values (current year, costume_exists boolean value) change the output of data being displayed. Is there an easier or cleaner way of doing this?
setTimeout(function() {
var table = document.getElementById("bands");
var icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
var $row = $(this).closest("tr");
var $year = $row.find(".year").text();
var $prize = $row.find(".prize").text();
var $band = $row.find(".band").text();
var $mp = $row.find(".mp").text();
var $ge_music = $row.find(".ge_music").text();
var $vp = $row.find(".vp").text();
var $ge_visual = $row.find(".ge_visual").text();
var $costume = $row.find(".costume").text();
var $total = $row.find(".total").text();
var $costumer = $row.find(".costumer").text();
var $designer = $row.find(".designer").text();
var $arranger = $row.find(".arranger").text();
var $choreographer = $row.find(".choreographer").text();
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
breakdown = "breakdown"
if ($year < 1991 && costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (costume_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else if (playing_exists) {
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music Playing:</b> ' + $mp + '<br>' +
'<b>General Effect Music:</b> ' + $ge_music + '<br>' +
'<b>Visual Performance:</b> ' + $vp + '<br>' +
'<b>General Effect - Visual:</b> ' + $ge_visual + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
swal({
title: 'Point Breakdown',
html: breakdown
})
} else {
alert("No point breakdowns for " + $year + " are available.");
}
}
}
}
}
};
}, 700);
javascript jquery html formatting dom
javascript jquery html formatting dom
edited 2 hours ago
200_success
128k15150412
128k15150412
asked 22 hours ago
Frank Doe
7214
7214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I don't have much experience with JavaScript, so I apologize if my suggestions aren't idiomatic (or runnable) JS. Here are a few things I see.
Consistent spacing
You switch between tabs of two spaces and four spaces. It's not clear to me why.
Use braces for multi-line for loops
Your second for loop is missing curly braces. It makes things easier to read to include curly braces if the for loop has code that spans multiple lines.
Use the boolean directly
You can assign the boolean value of an expression directly to a variable.
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
Becomes:
costume_exists = $costume.length > 1;
playing_exists = $mp.length > 0;
Use template literals
You can embed expressions into template strings directly. The parenthesis around the expression look to be unnecessary.
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
Becomes:
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
<b>Music:</b> {$ge_music}<br>
<b>Presentation:</b> {$ge_visual}<br>
<b>Costume:</b> {$costume}<br><br>
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
Reduce nesting
You can reduce nesting by inverting and combining your if-statements.
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
// code
}
}
}
Becomes:
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex != 7 || this.innerHTML.includes(icon)) {
return;
}
// code
}
More nesting can be reduced, like the table != null
check.
Avoid unneeded duplication
A lot of your lines are taken up by constructing separate (but very similar) strings depending on a few conditions. With template literals, we can use one string with inner expressions.
Define a string for the music portion as such:
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
I can't verify if that's valid syntax for JS, but you get the gist. Likewise with the costume portion.
It's not the exact same behavior, but this seems to be what you're going for:
breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
JavaScript after loading
This function below (used on a timeout due to my page loading time) displays information on my site.
Normally, you would do this as such:
window.onload = function ...
This also should work consistently, whereas using a pre-defined timeout duration may not work with extremely slow internet.
Use const
You can use the const
keyword instead of var
for the icon
variable.
const icon = "</i>";
Given that this is JavaScript, it will make zero difference performance-wise. But, if the value isn't meant to be changed, this gives an indication to those who read your code.
Performance
Currently you iterate over all cells in the table, $O(mn)$. I am not familiar enough with JavaScript to suggest something with better performance.
However I would certainly look into this if your tables become large.
Conclusion
Here is the code I ended up with:
window.onload = function() {
const table = document.getElementById("bands");
const icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex !== 7 || this.innerHTML.includes(icon)) {
return;
}
const $row = $(this).closest("tr");
const $year = $row.find(".year").text();
const $prize = $row.find(".prize").text();
const $band = $row.find(".band").text();
const $mp = $row.find(".mp").text();
const $ge_music = $row.find(".ge_music").text();
const $vp = $row.find(".vp").text();
const $ge_visual = $row.find(".ge_visual").text();
const $costume = $row.find(".costume").text();
const $total = $row.find(".total").text();
const $costumer = $row.find(".costumer").text();
const $designer = $row.find(".designer").text();
const $arranger = $row.find(".arranger").text();
const $choreographer = $row.find(".choreographer").text();
const costume_exists = $costume.length > 1;
const playing_exists = $mp.length > 0;
var breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
const music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
const costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
}
}
}
}
};
I have no way to verify that it works, but it looks like it should.
Hope this helps!
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those$row.find()
constants into a function to help make the main-loop more readable.
– Shelby115
1 hour ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210576%2fdisplaying-information-extracted-from-an-html-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't have much experience with JavaScript, so I apologize if my suggestions aren't idiomatic (or runnable) JS. Here are a few things I see.
Consistent spacing
You switch between tabs of two spaces and four spaces. It's not clear to me why.
Use braces for multi-line for loops
Your second for loop is missing curly braces. It makes things easier to read to include curly braces if the for loop has code that spans multiple lines.
Use the boolean directly
You can assign the boolean value of an expression directly to a variable.
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
Becomes:
costume_exists = $costume.length > 1;
playing_exists = $mp.length > 0;
Use template literals
You can embed expressions into template strings directly. The parenthesis around the expression look to be unnecessary.
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
Becomes:
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
<b>Music:</b> {$ge_music}<br>
<b>Presentation:</b> {$ge_visual}<br>
<b>Costume:</b> {$costume}<br><br>
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
Reduce nesting
You can reduce nesting by inverting and combining your if-statements.
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
// code
}
}
}
Becomes:
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex != 7 || this.innerHTML.includes(icon)) {
return;
}
// code
}
More nesting can be reduced, like the table != null
check.
Avoid unneeded duplication
A lot of your lines are taken up by constructing separate (but very similar) strings depending on a few conditions. With template literals, we can use one string with inner expressions.
Define a string for the music portion as such:
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
I can't verify if that's valid syntax for JS, but you get the gist. Likewise with the costume portion.
It's not the exact same behavior, but this seems to be what you're going for:
breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
JavaScript after loading
This function below (used on a timeout due to my page loading time) displays information on my site.
Normally, you would do this as such:
window.onload = function ...
This also should work consistently, whereas using a pre-defined timeout duration may not work with extremely slow internet.
Use const
You can use the const
keyword instead of var
for the icon
variable.
const icon = "</i>";
Given that this is JavaScript, it will make zero difference performance-wise. But, if the value isn't meant to be changed, this gives an indication to those who read your code.
Performance
Currently you iterate over all cells in the table, $O(mn)$. I am not familiar enough with JavaScript to suggest something with better performance.
However I would certainly look into this if your tables become large.
Conclusion
Here is the code I ended up with:
window.onload = function() {
const table = document.getElementById("bands");
const icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex !== 7 || this.innerHTML.includes(icon)) {
return;
}
const $row = $(this).closest("tr");
const $year = $row.find(".year").text();
const $prize = $row.find(".prize").text();
const $band = $row.find(".band").text();
const $mp = $row.find(".mp").text();
const $ge_music = $row.find(".ge_music").text();
const $vp = $row.find(".vp").text();
const $ge_visual = $row.find(".ge_visual").text();
const $costume = $row.find(".costume").text();
const $total = $row.find(".total").text();
const $costumer = $row.find(".costumer").text();
const $designer = $row.find(".designer").text();
const $arranger = $row.find(".arranger").text();
const $choreographer = $row.find(".choreographer").text();
const costume_exists = $costume.length > 1;
const playing_exists = $mp.length > 0;
var breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
const music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
const costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
}
}
}
}
};
I have no way to verify that it works, but it looks like it should.
Hope this helps!
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those$row.find()
constants into a function to help make the main-loop more readable.
– Shelby115
1 hour ago
add a comment |
I don't have much experience with JavaScript, so I apologize if my suggestions aren't idiomatic (or runnable) JS. Here are a few things I see.
Consistent spacing
You switch between tabs of two spaces and four spaces. It's not clear to me why.
Use braces for multi-line for loops
Your second for loop is missing curly braces. It makes things easier to read to include curly braces if the for loop has code that spans multiple lines.
Use the boolean directly
You can assign the boolean value of an expression directly to a variable.
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
Becomes:
costume_exists = $costume.length > 1;
playing_exists = $mp.length > 0;
Use template literals
You can embed expressions into template strings directly. The parenthesis around the expression look to be unnecessary.
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
Becomes:
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
<b>Music:</b> {$ge_music}<br>
<b>Presentation:</b> {$ge_visual}<br>
<b>Costume:</b> {$costume}<br><br>
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
Reduce nesting
You can reduce nesting by inverting and combining your if-statements.
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
// code
}
}
}
Becomes:
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex != 7 || this.innerHTML.includes(icon)) {
return;
}
// code
}
More nesting can be reduced, like the table != null
check.
Avoid unneeded duplication
A lot of your lines are taken up by constructing separate (but very similar) strings depending on a few conditions. With template literals, we can use one string with inner expressions.
Define a string for the music portion as such:
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
I can't verify if that's valid syntax for JS, but you get the gist. Likewise with the costume portion.
It's not the exact same behavior, but this seems to be what you're going for:
breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
JavaScript after loading
This function below (used on a timeout due to my page loading time) displays information on my site.
Normally, you would do this as such:
window.onload = function ...
This also should work consistently, whereas using a pre-defined timeout duration may not work with extremely slow internet.
Use const
You can use the const
keyword instead of var
for the icon
variable.
const icon = "</i>";
Given that this is JavaScript, it will make zero difference performance-wise. But, if the value isn't meant to be changed, this gives an indication to those who read your code.
Performance
Currently you iterate over all cells in the table, $O(mn)$. I am not familiar enough with JavaScript to suggest something with better performance.
However I would certainly look into this if your tables become large.
Conclusion
Here is the code I ended up with:
window.onload = function() {
const table = document.getElementById("bands");
const icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex !== 7 || this.innerHTML.includes(icon)) {
return;
}
const $row = $(this).closest("tr");
const $year = $row.find(".year").text();
const $prize = $row.find(".prize").text();
const $band = $row.find(".band").text();
const $mp = $row.find(".mp").text();
const $ge_music = $row.find(".ge_music").text();
const $vp = $row.find(".vp").text();
const $ge_visual = $row.find(".ge_visual").text();
const $costume = $row.find(".costume").text();
const $total = $row.find(".total").text();
const $costumer = $row.find(".costumer").text();
const $designer = $row.find(".designer").text();
const $arranger = $row.find(".arranger").text();
const $choreographer = $row.find(".choreographer").text();
const costume_exists = $costume.length > 1;
const playing_exists = $mp.length > 0;
var breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
const music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
const costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
}
}
}
}
};
I have no way to verify that it works, but it looks like it should.
Hope this helps!
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those$row.find()
constants into a function to help make the main-loop more readable.
– Shelby115
1 hour ago
add a comment |
I don't have much experience with JavaScript, so I apologize if my suggestions aren't idiomatic (or runnable) JS. Here are a few things I see.
Consistent spacing
You switch between tabs of two spaces and four spaces. It's not clear to me why.
Use braces for multi-line for loops
Your second for loop is missing curly braces. It makes things easier to read to include curly braces if the for loop has code that spans multiple lines.
Use the boolean directly
You can assign the boolean value of an expression directly to a variable.
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
Becomes:
costume_exists = $costume.length > 1;
playing_exists = $mp.length > 0;
Use template literals
You can embed expressions into template strings directly. The parenthesis around the expression look to be unnecessary.
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
Becomes:
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
<b>Music:</b> {$ge_music}<br>
<b>Presentation:</b> {$ge_visual}<br>
<b>Costume:</b> {$costume}<br><br>
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
Reduce nesting
You can reduce nesting by inverting and combining your if-statements.
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
// code
}
}
}
Becomes:
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex != 7 || this.innerHTML.includes(icon)) {
return;
}
// code
}
More nesting can be reduced, like the table != null
check.
Avoid unneeded duplication
A lot of your lines are taken up by constructing separate (but very similar) strings depending on a few conditions. With template literals, we can use one string with inner expressions.
Define a string for the music portion as such:
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
I can't verify if that's valid syntax for JS, but you get the gist. Likewise with the costume portion.
It's not the exact same behavior, but this seems to be what you're going for:
breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
JavaScript after loading
This function below (used on a timeout due to my page loading time) displays information on my site.
Normally, you would do this as such:
window.onload = function ...
This also should work consistently, whereas using a pre-defined timeout duration may not work with extremely slow internet.
Use const
You can use the const
keyword instead of var
for the icon
variable.
const icon = "</i>";
Given that this is JavaScript, it will make zero difference performance-wise. But, if the value isn't meant to be changed, this gives an indication to those who read your code.
Performance
Currently you iterate over all cells in the table, $O(mn)$. I am not familiar enough with JavaScript to suggest something with better performance.
However I would certainly look into this if your tables become large.
Conclusion
Here is the code I ended up with:
window.onload = function() {
const table = document.getElementById("bands");
const icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex !== 7 || this.innerHTML.includes(icon)) {
return;
}
const $row = $(this).closest("tr");
const $year = $row.find(".year").text();
const $prize = $row.find(".prize").text();
const $band = $row.find(".band").text();
const $mp = $row.find(".mp").text();
const $ge_music = $row.find(".ge_music").text();
const $vp = $row.find(".vp").text();
const $ge_visual = $row.find(".ge_visual").text();
const $costume = $row.find(".costume").text();
const $total = $row.find(".total").text();
const $costumer = $row.find(".costumer").text();
const $designer = $row.find(".designer").text();
const $arranger = $row.find(".arranger").text();
const $choreographer = $row.find(".choreographer").text();
const costume_exists = $costume.length > 1;
const playing_exists = $mp.length > 0;
var breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
const music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
const costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
}
}
}
}
};
I have no way to verify that it works, but it looks like it should.
Hope this helps!
I don't have much experience with JavaScript, so I apologize if my suggestions aren't idiomatic (or runnable) JS. Here are a few things I see.
Consistent spacing
You switch between tabs of two spaces and four spaces. It's not clear to me why.
Use braces for multi-line for loops
Your second for loop is missing curly braces. It makes things easier to read to include curly braces if the for loop has code that spans multiple lines.
Use the boolean directly
You can assign the boolean value of an expression directly to a variable.
if ($costume.length > 1) {
costume_exists = true;
} else {
costume_exists = false;
}
if ($mp.length > 0) {
playing_exists = true;
} else {
playing_exists = false;
}
Becomes:
costume_exists = $costume.length > 1;
playing_exists = $mp.length > 0;
Use template literals
You can embed expressions into template strings directly. The parenthesis around the expression look to be unnecessary.
breakdown = ('<h3>' + $band + " " + $year + '</h3>' +
'<i>' + getOrdinal($prize) + ' Prize' + '</i><br><br>' +
'<b>Music:</b> ' + $ge_music + '<br>' +
'<b>Presentation:</b> ' + $ge_visual + '<br>' +
'<b>Costume:</b> ' + $costume + '<br><br>' +
'<b>Total Points:</b> ' + $total + '<br><br>' +
'<b>Costumer:</b> ' + $costumer + '<br>' +
'<b>Costume/Set Designer:</b> ' + $designer + '<br>' +
'<b>Music Arranger:</b> ' + $arranger + '<br>' +
'<b>Choreographer:</b> ' + $choreographer + '<br>')
Becomes:
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
<b>Music:</b> {$ge_music}<br>
<b>Presentation:</b> {$ge_visual}<br>
<b>Costume:</b> {$costume}<br><br>
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
Reduce nesting
You can reduce nesting by inverting and combining your if-statements.
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex == 7) {
if (!this.innerHTML.includes(icon)) {
// code
}
}
}
Becomes:
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex != 7 || this.innerHTML.includes(icon)) {
return;
}
// code
}
More nesting can be reduced, like the table != null
check.
Avoid unneeded duplication
A lot of your lines are taken up by constructing separate (but very similar) strings depending on a few conditions. With template literals, we can use one string with inner expressions.
Define a string for the music portion as such:
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
I can't verify if that's valid syntax for JS, but you get the gist. Likewise with the costume portion.
It's not the exact same behavior, but this seems to be what you're going for:
breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
JavaScript after loading
This function below (used on a timeout due to my page loading time) displays information on my site.
Normally, you would do this as such:
window.onload = function ...
This also should work consistently, whereas using a pre-defined timeout duration may not work with extremely slow internet.
Use const
You can use the const
keyword instead of var
for the icon
variable.
const icon = "</i>";
Given that this is JavaScript, it will make zero difference performance-wise. But, if the value isn't meant to be changed, this gives an indication to those who read your code.
Performance
Currently you iterate over all cells in the table, $O(mn)$. I am not familiar enough with JavaScript to suggest something with better performance.
However I would certainly look into this if your tables become large.
Conclusion
Here is the code I ended up with:
window.onload = function() {
const table = document.getElementById("bands");
const icon = "</i>";
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
if (this.cellIndex !== 7 || this.innerHTML.includes(icon)) {
return;
}
const $row = $(this).closest("tr");
const $year = $row.find(".year").text();
const $prize = $row.find(".prize").text();
const $band = $row.find(".band").text();
const $mp = $row.find(".mp").text();
const $ge_music = $row.find(".ge_music").text();
const $vp = $row.find(".vp").text();
const $ge_visual = $row.find(".ge_visual").text();
const $costume = $row.find(".costume").text();
const $total = $row.find(".total").text();
const $costumer = $row.find(".costumer").text();
const $designer = $row.find(".designer").text();
const $arranger = $row.find(".arranger").text();
const $choreographer = $row.find(".choreographer").text();
const costume_exists = $costume.length > 1;
const playing_exists = $mp.length > 0;
var breakdown = "breakdown"
if ($year > 1991 && !costume_exists && !playing_exists) {
alert(`No point breakdowns for {$year} are available.`);
return;
}
const music = $year < 1991 && costume_exists
? `<b>Music:</b> {$ge_music}<br>`
: `<b>Music Playing:</b> {$mp}<br>`;
const costume = custume_exists
? `<b>Costume:</b> {$costume}<br><br>`
: '';
breakdown = `<h3>{$band} {$year}</h3>
<i>{getOrdinal($prize)} Prize</i><br><br>
{music}
<b>Presentation:</b> {$ge_visual}<br>
{costume}
<b>Total Points: {$total}<br><br>
<b>Costumer:</b> $costumer<br>
<b>Costume/Set Designer:</b> {$designer}<br>
<b>Music Arranger:</b> {$arranger}<br>
<b>Choreographer:</b> {$choreographer}<br>`
swal({
title: 'Point Breakdown',
html: breakdown
})
}
}
}
}
};
I have no way to verify that it works, but it looks like it should.
Hope this helps!
edited 18 hours ago
answered 21 hours ago
esote
1,8671933
1,8671933
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those$row.find()
constants into a function to help make the main-loop more readable.
– Shelby115
1 hour ago
add a comment |
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those$row.find()
constants into a function to help make the main-loop more readable.
– Shelby115
1 hour ago
1
1
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those
$row.find()
constants into a function to help make the main-loop more readable.– Shelby115
1 hour ago
+1 A lot of great tips here. Was going to post an answer but I think you've got pretty much everything covered. Only other suggestion to add might be to pull all of those
$row.find()
constants into a function to help make the main-loop more readable.– Shelby115
1 hour ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210576%2fdisplaying-information-extracted-from-an-html-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown