score:15

Accepted answer

something like this?

$(function() {

  var headers = $("span",$("#tblversions")).map(function() { 
    return this.innerhtml;
  }).get();

  var rows = $("tbody tr",$("#tblversions")).map(function() { 
    return [$("td:eq(0) input:checkbox:checked",this).map(function() { 
      return this.innerhtml;     
    }).get()];
  }).get();

  alert(rows);
});

score:0

i would think it would make more sense to get a json array back from the ajax call and generate your table/chart from that. with jquery templates this isn't hard at all.

score:0

use this line of code:

var arrays = [];
$('table').eq(0).find('tr').each((r,row) => arrays.push($(row).find('td,th').map((c,cell) => $(cell).text()).toarray()))

score:1

something along the lines of:

var tharray = new array();
var contentarray = new array();

$('th').each(function(index) {
  tharray[index] =    $(this).html();
})


$('tr').each(function(indexparent) {
  contentarray['row'+indexparent] = new array();
    $(this).children().each(function(indexchild) {
      contentarray['row'+indexparent]['col'+indexchild] = $(this).html();
    });
});

this gives you two arrays, tharray which is an array of your headings and contentarray which is a 2d array containing rows and columns: contentarray['row1']['col0'] returns " value 1,1"

actually, contentarray contains the th's as well... referenced 'row0'

score:1

does it make more sense to throw a json object back from the ajax query and then render a table and a chart from there?

yes, absolutely. return json in response to your ajax request, then you can render the table using something like jquery templates and use the same underlying data to generate your chart as well.

score:1

here's a modification of jerome wagner's answer that uses recursive maps instead of a map inside an 'each':

http://jsbin.com/oveva3/383/edit

  var headers = $("th",$("#meme")).map(function() { 
    return this.innerhtml;
  }).get();

  var rows = $("tbody tr",$("#meme")).map(function() { 
    return [$("td",this).map(function() { 
      return this.innerhtml;     
    }).get()];
  }).get();

score:1

i'm tinkering with the same thing over here, but i prefer iterating through all tables and writing the header and body arrays into properties of each table, so here's my modification to the original answer:

$(function() {
$("table").each(function(){
  var $table = $(this),
      $headercells = $("thead th", $(this)),
      $rows = $("tbody tr", $(this));
  var headers = [],
      rows = [];


$headercells.each(function(k,v) {
   headers[headers.length] = $(this).text();
  $table.prop("headary", headers);
});

$rows.each(function(row,v) {
  $(this).find("td").each(function(cell,v) {
    if (typeof rows[cell] === 'undefined') rows[cell] = [];
    rows[cell][row] = $(this).text();
    $table.prop("bodary", rows);
  });
});
console.log($(this).prop('headary'));
console.log($(this).prop('bodary'));  
});
});

jsbin

score:3

yet another way of doing it

var headers = jquery('th').map(function(i,e) { return e.innerhtml;}).get();
var datas = []
jquery.each(jquery('tr:gt(0)'), function(i,e ) {
   datas.push(jquery('td', e).map(function(i,e) {
                                     return e.innerhtml; 
                                  }).get()
             );
});

score:17

demo updated http://jsfiddle.net/ish1301/cnsnk/

var header = array();

$("table tr th").each(function(i, v){
        header[i] = $(this).text();
})

alert(header);

var data = array();

$("table tr").each(function(i, v){
    data[i] = array();
    $(this).children('td').each(function(ii, vv){
        data[i][ii] = $(this).text();
    }); 
})

alert(data);

Related Query

More Query from same tag