This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:web_development:tables:arrays [2021/12/10 06:28] mag |
en:web_development:tables:arrays [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Web Development Lesson | + | ====== Web Development Lesson |
===== Javascript Multidimensional Arrays ===== | ===== Javascript Multidimensional Arrays ===== | ||
==== Objective ==== | ==== Objective ==== | ||
Line 5: | Line 5: | ||
==== Setup ==== | ==== Setup ==== | ||
- | * We'll add this as a page in our project, | + | * In your ' |
* Set up the template using ' | * Set up the template using ' | ||
* Also add a Javascript file using ' | * Also add a Javascript file using ' | ||
Line 114: | Line 114: | ||
}</ | }</ | ||
* That's a lot of work for just displaying one line, but we don't have to go through the same thing every time. | * That's a lot of work for just displaying one line, but we don't have to go through the same thing every time. | ||
+ | |||
+ | ==== Functions ==== | ||
+ | * We want to use the ' | ||
+ | * A function is a block of code that can be called by a button click or from anywhere else in the Javascript code. Remember we did this for our checklists in the previous lesson? | ||
+ | * Add this function below the existing code. | ||
+ | < | ||
+ | | ||
+ | }</ | ||
+ | * Now move the ' | ||
+ | < | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "</ | ||
+ | }</ | ||
+ | * At this point, the function doesn' | ||
+ | * Add < | ||
+ | * Add a new line at the end with < | ||
+ | < | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "</ | ||
+ | return statsRow; | ||
+ | }</ | ||
==== Multidimensional Arrays ==== | ==== Multidimensional Arrays ==== | ||
Line 119: | Line 150: | ||
* First, let's update our ' | * First, let's update our ' | ||
< | < | ||
- | | + | |
- | [' | + | [' |
- | ['Juan Guillermo', | + | ['Juan Guillermo', |
- | [' | + | [' |
];</ | ];</ | ||
* Note that each line is an element in the larger list, so we separate them by < | * Note that each line is an element in the larger list, so we separate them by < | ||
Line 128: | Line 159: | ||
* So we are saying that we have an array of arrays (list of lists) called < | * So we are saying that we have an array of arrays (list of lists) called < | ||
* We want to show each player in turn, so we need data from < | * We want to show each player in turn, so we need data from < | ||
- | < | + | < |
for (p = 0; p < players.length; | for (p = 0; p < players.length; | ||
+ | |||
}</ | }</ | ||
- | * | + | * Remember this from when you created a list using Javascript? We've replaced |
- | * Inside the < | + | * Before we go any further, let's check that we have the correct information. |
- | * The first < | + | * Add a line inside the for loop as follows. |
- | * The third says that each time the loop finishes, we should increment p (add 1). | + | < |
- | * The second says to keep going while < | + | * This will print the value of < |
+ | * Open your 'team.html' code in Live Server, which should still just show the table header row. | ||
+ | * Now open your Developer Tools (click the menu in top right, then select 'More Tools' and ' | ||
+ | * Check that it has printed (in green) | ||
+ | * Let's take it a step further and change | ||
+ | * Check the console again (refresh the page if you need to) and you should | ||
* Now, for each player, we need to create the statsRow as for the single player. | * Now, for each player, we need to create the statsRow as for the single player. | ||
- | * We no longer | + | * We already |
- | < | + | < |
- | statsRow | + | statsRow = createRowText(players[p]);</code> |
- | | + | |
- | | + | |
- | statsRow += "<td>" + players[p][3] + "</td>"; | + | |
- | | + | * Let's move the 'add row to table' block of code inside the 'for loop' so we display each row when the data is ready (and before we lose it). |
- | * Place this code inside the for loop between | + | <code> for (p = 0; p < players.length; |
- | * Finally, we can add the row to the table. | + | console.log(players[p]); |
- | < | + | |
- | newRow = tableBody.insertRow(p); | + | |
- | newRow.innerHTML = statsRow;</ | + | |
- | * Notice | + | // add row to table |
+ | tableBody = document.getElementsByTagName(" | ||
+ | newRow | ||
+ | newRow.innerHTML = statsRow; | ||
+ | }</ | ||
+ | * Run the code now and you'll see that it's still not quite right. All the players are listed, but the order is backwards. | ||
+ | * This is because | ||
+ | * We want the row number to increment | ||
+ | < | ||
* There' | * There' | ||
* Our final code looks like this. | * Our final code looks like this. | ||
- | < | + | < |
- | [' | + | // generate data |
- | [' | + | |
- | ['Juan Guillermo', | + | [' |
- | [' | + | [' |
- | ]; | + | ['Juan Guillermo', |
- | + | [' | |
- | tableBody = document.getElementsByTagName(" | + | ]; |
- | // loop through all players | + | tableBody = document.getElementsByTagName(" |
- | for (p = 0; p < players.length; | + | |
- | // create | + | |
- | statsRow | + | for (p = 0; p < players.length; |
- | statsRow += "< | + | // console.log(players[p]); |
- | statsRow += "< | + | |
- | statsRow += "< | + | |
- | statsRow += "< | + | |
- | + | // add row to table | |
- | // add row to table | + | newRow |
- | newRow = tableBody.insertRow(p); | + | newRow.innerHTML = statsRow; |
- | | + | } |
+ | } | ||
+ | |||
+ | function createRowText(player) { | ||
+ | | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | | ||
+ | | ||
}</ | }</ | ||
- | * This looks a lot more efficient now, but you still might think that it's easier to create the table in HTML than Javascript. Ok. What about if we use the full team? Can you imagine building the table with 26 players? Creating the array is much easier. | + | * This looks a lot more efficient now, but you still might think that it's easier to create the table in HTML than Javascript. Ok. What about if we use the full team? Can you imagine building the table with 26 players? What if you're using data with thousands of entries? Creating the array is much easier. |
[[en: | [[en: |