Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get data from a table?

Tags:

javascript

How do I pull data (string) from a column called "Limit" in a table ("displayTable") in 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);      ... } 
like image 238
MrM Avatar asked Jul 14 '10 17:07

MrM


People also ask

How do you get data from a table?

SELECT statements An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';


2 Answers

This is how I accomplished reading a table in javascript. Basically I drilled down into the rows and then I was able to drill down into the individual cells for each row. This should give you an 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; */    } } 

UPDATED - TESTED 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> 
like image 89
webdad3 Avatar answered Oct 20 '22 05:10

webdad3


in this code data is a two dimensional array of table data

let oTable = document.getElementById('datatable-id'); let data = [...oTable.rows].map(t => [...t.children].map(u => u.innerText)) 
like image 29
Yosef Tukachinsky Avatar answered Oct 20 '22 06:10

Yosef Tukachinsky