Skip to content

Instantly share code, notes, and snippets.

@sangar82
Last active December 29, 2015 14:51
Show Gist options
  • Save sangar82/adea0a4669d034cdca6c to your computer and use it in GitHub Desktop.
Save sangar82/adea0a4669d034cdca6c to your computer and use it in GitHub Desktop.
Transforme table to graph
// Guardamos en un array los valores
var values = [];
var headers = [];
// get our values (get rows)
$('.datatables5 tbody tr').each(function(i, v){
values[i] = [];
//miramos cuantas columnas tiene para quitar la última de total
length = $(this).children('td').length;
c = 1;
// select either th or td, doesn't matter
$(this).children('th,td').each(function(ii, vv){
if (c < length)
{
if (c == 1)
values[i][ii] = $(this).html().trim();
else
values[i][ii] = parseInt($(this).html().trim());
}
c++;
});
});
// get headers
$('.datatables5 thead tr').each(function(i, v){
headers[i] = [];
//miramos cuantas columnas tiene para quitar la última de total
length = $(this).children('th').length;
c = 1;
// select either th or td, doesn't matter
$(this).children('th').each(function(ii, vv){
if (c < length)
{
if (c != 1)
headers[i][ii] = $(this).html().trim();
}
c++;
});
});
// get by column
// get by column headers
td = $(' .datatables5 tr > td:nth-child(1)');
td.each(function () {
headers.push($(this).html().trim().replace( /<.*?>/g, '' ))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment