Getting all table data, not just what is being displayed.
Getting all table data, not just what is being displayed.
Hi Guys,
I'm working on a test page where the goal is to obtain both the header names and everything in the tbody of the table and convert to JSON strings. My JQuery code goes like so:
[code]
$(document).ready(function() {
$('#example').dataTable();
var header = Array();
$('#example thead th').each(function(i) {
header[i] = $(this).text();
});
var data = Array();
$('#example tbody tr').each(function(i) {
data[i] = Array();
$(this).children('td').each(function(j){
data[i][j] = $(this).text();
});
})
$("#showhjson").click(function(){
var aa = JSON.stringify({ header: header });
alert(aa);
});
$("#showdjson").click(function(){
var aa = JSON.stringify({ data: data });
alert(aa);
});
} );
[/code]
On my HTML page I have two anchor tags: (showhjson) one to show the header as a JSON string, (showdjson) the second to show the table data in a JSON string. On an HTML page that doesn't involve DataTables and contains just a normal static table, the above code works great. When I apply the code to an HTML page that contains DataTables, however, the data that gets captured is only the data that DataTables is currently displaying. I'd like to know what it is I need to do to this function:
[code]
var data = Array();
$('#example tbody tr').each(function(i) {
data[i] = Array();
$(this).children('td').each(function(j){
data[i][j] = $(this).text();
});
})
[/code]
in order to display all the records? Please advise.
Alan
I'm working on a test page where the goal is to obtain both the header names and everything in the tbody of the table and convert to JSON strings. My JQuery code goes like so:
[code]
$(document).ready(function() {
$('#example').dataTable();
var header = Array();
$('#example thead th').each(function(i) {
header[i] = $(this).text();
});
var data = Array();
$('#example tbody tr').each(function(i) {
data[i] = Array();
$(this).children('td').each(function(j){
data[i][j] = $(this).text();
});
})
$("#showhjson").click(function(){
var aa = JSON.stringify({ header: header });
alert(aa);
});
$("#showdjson").click(function(){
var aa = JSON.stringify({ data: data });
alert(aa);
});
} );
[/code]
On my HTML page I have two anchor tags: (showhjson) one to show the header as a JSON string, (showdjson) the second to show the table data in a JSON string. On an HTML page that doesn't involve DataTables and contains just a normal static table, the above code works great. When I apply the code to an HTML page that contains DataTables, however, the data that gets captured is only the data that DataTables is currently displaying. I'd like to know what it is I need to do to this function:
[code]
var data = Array();
$('#example tbody tr').each(function(i) {
data[i] = Array();
$(this).children('td').each(function(j){
data[i][j] = $(this).text();
});
})
[/code]
in order to display all the records? Please advise.
Alan
This discussion has been closed.
Replies
var data = oTable.fnGetData();
That's all I needed.