Table of Contents

Desarrollo Web Lección 4 - Tablas

Matrices multidimensionales de Javascript

Objetivo

Continuaremos construyendo nuestra tabla de estadísticas sobre el equipo de Colombia, pero lo haremos de manera más eficiente con javascript usando matrices y bucles.

Configuración

equipo.html

    <table>
        <thead>
            <tr>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>Nacionalidad</th>
                <th class="edad">Edad</th>
                <th>POsición</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

style.css

td, th {
  border-width: 1px;
  border-style: solid;
}

td {
  padding: 10px;
}

table {
  border-collapse: collapse;
}

#carlos {
  font-style: italic;
}

.age {
  background-color: yellow;
}

#carlos .age {
  font-weight: 900;
}

td.age {
  font-size: 150%;
}

equipo.js

window.onload = function () {

}

Matrices

window.onload = function () {
    // generar datos del jugador
    jugador = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero'];
}
    // crea texto de fila
    statsRow = "<tr>";
    statsRow += "<td>" + jugador[0] + "</td>";
    statsRow += "<td>" + jugador[1] + "</td>";
    statsRow += "<td>" + jugador[2] + "</td>";
    statsRow += "<td>" + jugador[3] + "</td>";
    statsRow += "<td>" + jugador[4] + "</td>";
    statsRow += "</tr>";
    // agregar fila a la tabla
    tableBody = document.getElementsByTagName("tbody")[0];
    newRow = tableBody.insertRow(0);
    newRow.innerHTML = statsRow;
window.onload = function () {
    // generar datos
    jugador = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero'];

    // crea texto de fila
    statsRow = "<tr>";
    statsRow += "<td>" + jugador[0] + "</td>";
    statsRow += "<td>" + jugador[1] + "</td>";
    statsRow += "<td>" + jugador[2] + "</td>";
    statsRow += "<td>" + jugador[3] + "</td>";
    statsRow += "<td>" + jugador[4] + "</td>";
    statsRow += "</tr>";

    // agregar fila a la tabla
    tableBody = document.getElementsByTagName("tbody")[0];
    newRow = tableBody.insertRow(0);
    newRow.innerHTML = statsRow;
}

Funciones

funcion crearTextoFila() {
    
}
function crearTextoFila() {
    statsRow = "<tr>";
    statsRow += "<td>" + jugador[0] + "</td>";
    statsRow += "<td>" + jugador[1] + "</td>";
    statsRow += "<td>" + jugador[2] + "</td>";
    statsRow += "<td>" + jugador[3] + "</td>";
    statsRow += "<td>" + jugador[4] + "</td>";
    statsRow += "</tr>";
}
function crearTextoFila(jugador) {
    statsRow = "<tr>";
    statsRow += "<td>" + jugador[0] + "</td>";
    statsRow += "<td>" + jugador[1] + "</td>";
    statsRow += "<td>" + jugador[2] + "</td>";
    statsRow += "<td>" + jugador[3] + "</td>";
    statsRow += "<td>" + jugador[4] + "</td>";
    statsRow += "</tr>";
    return statsRow;
}

Matrices Multidimensionales

    jugadores = [
        ['David', 'Ospina Ramírez', 'Colombia', 33, 'Portero'],
        ['Carlos', 'Eccehomo Cuesta Figueroa', 'Colombia', 22, 'Defensa'],
        ['Juan Guillermo', 'Cuadrado Bello', 'Colombia', 33, 'Centrocampista'],
        ['Radamel Falcao', 'García Zárate', 'Colombia', 35, 'Delantero']
              ];
    // crear texto
    for (j = 0; j <jugadores.length; j++) {

    }
        console.log(j);
        // crea texto de fila
        statsRow = crearTextoFila(jugadores[j]);
    for (j = 0; j <jugadores.length; j++) {
        console.log(jugadores[j]);
        // crea texto de fila
        statsRow = createRowText(jugadores[j]);

        // agregar fila a la tabla
        tableBody = document.getElementsByTagName("tbody")[0];
        newRow = tableBody.insertRow(0);
        newRow.innerHTML = statsRow;
    }
        newRow = tableBody.insertRow(j);
window.onload = funcion () {
    // generar datos
    jugadores = [
        ['David', 'Ospina Ramírez', 'Colombia', 33, 'Portero'],
        ['Carlos', 'Eccehomo Cuesta Figueroa', 'Colombia', 22, 'Defensa'],
        ['Juan Guillermo', 'Cuadrado Bello', 'Colombia', 33, 'Centrocampista'],
        ['Radamel Falcao', 'García Zárate', 'Colombia', 35, 'Delantero']
              ];

    tableBody = document.getElementsByTagName("tbody")[0];

    // mostrar datos
    for (j = 0; j <jugadores.length; j++) {
        // console.log(jugadores[j]);
        // crea texto de fila
        statsRow = createRowText(jugadores[j]);

        // agregar fila a la tabla
        newRow = tableBody.insertRow(j);
        newRow.innerHTML = statsRow;
    }
}

función crearTextoFila(jugador) {
    statsRow = "<tr>";
    statsRow += "<td>" + jugador[0] + "</td>";
    statsRow += "<td>" + jugador[1] + "</td>";
    statsRow += "<td>" + jugador[2] + "</td>";
    statsRow += "<td>" + jugador[3] + "</td>";
    statsRow += "<td>" + jugador[4] + "</td>";
    statsRow += "</tr>";
    return statsRow;
}

Final Code

Siguiente: Ejercicios