This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
web_design:horario:arrays [2021/10/11 09:03] mag created |
web_design:horario:arrays [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Web Development Lesson 3 - Tablas ====== | ====== Web Development Lesson 3 - Tablas ====== | ||
===== Javascript Arrays and Loops ===== | ===== Javascript Arrays and Loops ===== | ||
+ | ==== Objective ==== | ||
+ | We'll continue building our table of stats on the Colombia team, but we'll do it more efficiently with javascript by using arrays and loops. | ||
+ | |||
==== Setup ==== | ==== Setup ==== | ||
+ | * We'll start with the work done in the previous class on [[https:// | ||
+ | **HTML** | ||
+ | < | ||
+ | < | ||
+ | <tr> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <th class=" | ||
+ | < | ||
+ | </tr> | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | **CSS** | ||
+ | < | ||
+ | border-width: | ||
+ | border-style: | ||
+ | } | ||
+ | |||
+ | td { | ||
+ | padding-left: | ||
+ | padding-right: | ||
+ | padding-top: | ||
+ | padding-bottom: | ||
+ | } | ||
+ | |||
+ | table { | ||
+ | border-collapse: | ||
+ | } | ||
+ | |||
+ | #carlos { | ||
+ | font-style: italic; | ||
+ | } | ||
+ | |||
+ | .age { | ||
+ | background-color: | ||
+ | } | ||
+ | |||
+ | #carlos .age { | ||
+ | font-weight: | ||
+ | } | ||
+ | |||
+ | td.age { | ||
+ | font-size: 150%; | ||
+ | }</ | ||
+ | |||
+ | |||
+ | ==== Arrays ==== | ||
+ | * Think of an array as a list. | ||
+ | * In Javascript, an array is designated by < | ||
+ | * In our example, each line (each player' | ||
+ | < | ||
+ | * Let's take a look at this statement in detail. | ||
+ | * The first word ' | ||
+ | * The < | ||
+ | * The first thing we see after the < | ||
+ | * The array contains 5 items separated by < | ||
+ | * Every Javascript statement must end with < | ||
+ | * Now let's display the list. | ||
+ | |||
+ | < | ||
+ | * After creating the array, on line 2, we create a string (text) called statsRow. You can tell it's a string because of the < | ||
+ | * The string contains all the text for a data cell, but instead of writing out the player' | ||
+ | * Remember that in computer progams, the first element is counted as < | ||
+ | * Notice that the strings are joined by using the < | ||
+ | < | ||
+ | * In the third line, we join the new string to the old one. < | ||
+ | < | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | * From the fourth line on, we simplify this to be new string < | ||
+ | < | ||
+ | // add row to table</ | ||
+ | * Now, add a blank line to show we're starting something a bit different. The next line begins with < | ||
+ | < | ||
+ | * The following line shows another variable being assigned a value. The value is an element with tag name < | ||
+ | * There may be more than one table (and therefore more than one < | ||
+ | * We then assign that element to a variable called < | ||
+ | < | ||
+ | * Here we insert a new row at position 0 (immediately after the < | ||
+ | * We then assign it to a variable called < | ||
+ | < | ||
+ | * In the final line of this code, we select our new row with < | ||
+ | * < | ||
+ | * We then use < | ||
+ | * In this case, it's < | ||
+ | * So this line pulls it all together to say 'put the string with our data into the new row of < | ||
+ | * Let's look at the complete code so far. | ||
+ | < | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | |||
+ | // add row to table | ||
+ | tableBody = document.getElementsByTagName(" | ||
+ | newRow | ||
+ | newRow.innerHTML = statsRow;</ | ||
+ | * That's a lot of work for just displaying one line, but we don't have to go through the same thing every time. | ||
+ | |||
+ | ==== Multidimensional Arrays ==== | ||
+ | * We really want our program to run through all our players. So we have a list of players each containing a list of stats. In other words, we have an array of arrays or a multidimensional array. Let's start with the data we already know. | ||
+ | * First, delete the Javascript code we made above. | ||
+ | < | ||
+ | players = [ | ||
+ | [' | ||
+ | [' | ||
+ | ['Juan Guillermo', | ||
+ | [' | ||
+ | ];</ | ||
+ | * Note that each line is an element in the larger list, so we separate them by < | ||
+ | * The only < | ||
+ | * 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 < | ||
+ | < | ||
+ | for (p = 0; p < players.length; | ||
+ | }</ | ||
+ | * It's called a loop because it will loop (iterate) through any code inside the < | ||
+ | * There are a few ways to do this, but we'll start with a < | ||
+ | * Inside the < | ||
+ | * The first < | ||
+ | * The third says that each time the loop finishes, we should increment p (add 1). | ||
+ | * The second says to keep going while < | ||
+ | * Now, for each player, we need to create the statsRow as for the single player. | ||
+ | * We no longer have the variable called ospinaRamirez, | ||
+ | < | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | * Place this code inside the for loop between the < | ||
+ | * Finally, we can add the row to the table. | ||
+ | < | ||
+ | newRow = tableBody.insertRow(p); | ||
+ | newRow.innerHTML = statsRow;</ | ||
+ | * Notice that we've changed the < | ||
+ | * There' | ||
+ | * Our final code looks like this. | ||
+ | < | ||
+ | [' | ||
+ | [' | ||
+ | ['Juan Guillermo', | ||
+ | [' | ||
+ | ]; | ||
+ | | ||
+ | tableBody = document.getElementsByTagName(" | ||
+ | |||
+ | // loop through all players | ||
+ | for (p = 0; p < players.length; | ||
+ | // create stats string | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | | ||
+ | // add row to table | ||
+ | newRow = tableBody.insertRow(p); | ||
+ | newRow.innerHTML = 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. | ||
+ | |||
+ | [[web_design: |