2010-07-14 12 views
24

Me gustaría obtener datos (cadena) de una columna llamada "Límite" en una tabla ("displayTable") en javascript. ¿Cómo puedo hacer eso?¿Cómo obtengo datos de una tabla de datos en javascript?

var table = document.getElementById('displayTable');  
var rowCount = table.rows.length;  
for (var i = 1; i < rowCount - 1; i++) {  
    var row = table.rows[i]["Limit"].ToString(); 
    alert(row); 
    ... 
} 

Respuesta

28

Así es como logré leer una tabla en javascript. Básicamente profundicé en las filas y luego pude profundizar en las celdas individuales para cada fila. Esto debe darle una idea

//gets table 
var oTable = document.getElementById('myTable'); 

//gets rows of table 
var rowLength = oTable.rows.length; 

//loops through rows  
for (i = 0; i < rowLength; i++){ 

    //gets cells of current row 
    var oCells = oTable.rows.item(i).cells; 

    //gets amount of cells of current row 
    var cellLength = oCells.length; 

    //loops through each cell in current row 
    for(var j = 0; j < cellLength; j++){ 
     /* get your cell info here */ 
     /* var cellVal = oCells.item(j).innerHTML; */ 
    } 
} 

ACTUALIZADO - PROBADOS SCRIPT

<table id="myTable"> 
    <tr> 
     <td>A1</td> 
     <td>A2</td> 
     <td>A3</td> 
    </tr> 
    <tr> 
     <td>B1</td> 
     <td>B2</td> 
     <td>B3</td> 
    </tr> 
</table> 
<script> 
    //gets table 
    var oTable = document.getElementById('myTable'); 

    //gets rows of table 
    var rowLength = oTable.rows.length; 

    //loops through rows  
    for (i = 0; i < rowLength; i++){ 

     //gets cells of current row 
     var oCells = oTable.rows.item(i).cells; 

     //gets amount of cells of current row 
     var cellLength = oCells.length; 

     //loops through each cell in current row 
     for(var j = 0; j < cellLength; j++){ 

       // get your cell info here 

       var cellVal = oCells.item(j).innerHTML; 
       alert(cellVal); 
      } 
    } 
</script> 
+0

He progresado con el fragmento. innerHTML parece estar vacío, lo cual es extraño porque así es como agregué los datos cuando ingresé los datos en mi código de JavaScript. var cell4 = row.insertCell (3); cell4.innerHTML = límite; – MrM

+0

cell4.innerHTML = limit; ¿Qué es el límite? Creo que si asigna una variable a cell4.innerHTML, obtendrá lo que está en esa celda (suponiendo que configure todo correctamente y que haya datos reales). – webdad3

+0

OK Acabo de crear una página de prueba y probé el guión. Edité mi respuesta original para alertar a cada una de las celdas de la mesa. Desde aquí, creo que deberías poder obtener lo que estás necesitando. – webdad3

2

uso JSON & jQuery. Es mucho más fácil que oldschool javascript

function savedata1() { 

var obj = $('#myTable tbody tr').map(function() { 
var $row = $(this); 
var t1 = $row.find(':nth-child(1)').text(); 
var t2 = $row.find(':nth-child(2)').text(); 
var t3 = $row.find(':nth-child(3)').text(); 
return { 
    td_1: $row.find(':nth-child(1)').text(), 
    td_2: $row.find(':nth-child(2)').text(), 
    td_3: $row.find(':nth-child(3)').text() 
    }; 
}).get(); 
Cuestiones relacionadas