DataTables
Advanced interaction
features for your tables.
Editor
Comprehensive editing
library for DataTables.
Manual
Download
Examples
Manual
Reference
Extensions
Plug-ins
Blog
Forums
Discussions
Sign In
Support
FAQs
Download
Purchase
≡
Show site navigation
Custom Columns with Server Side processing
Custom Columns with Server Side processing
skione
Posts: 7
Questions: 0
Answers: 0
May 2011
edited May 2011
in
General
It seems that the examples and documentation when using server side processing require you to specify the table header prior to loading the data.
Can I dynamically load the table columns like I load the data?
Replies
ChrisGedrim
Posts: 10
Questions: 0
Answers: 0
May 2011
Skione,
As Allan says in this thread: http://datatables.net/forums/comments.php?DiscussionID=3519 - you would have to make a separate ajax call to get the aoColumns settings with which to initialize the table:
[code]
var aoColumns;
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": "http://mydomain.com/aoColumns_data",
"success": function(data){
aoColumns = data.aoColumns
}
} );
$('#example').dataTable( {
"aoColumns": aoColumns
} );
[/code]
- Chris
(NOTE: this is untested, and your server-side script would have to return the aoColumns array in the format needed by DataTables)
skione
Posts: 7
Questions: 0
Answers: 0
May 2011
I have having a little difficulty with format for columns, could you confirm whether this is right?
"aoColumns":[{"sTitle":"id"},{"sTitle":"phone"},{"sTitle":"message"},{"sTitle":"status"}]
ChrisGedrim
Posts: 10
Questions: 0
Answers: 0
May 2011
That's correct for the client side (js)
skione
Posts: 7
Questions: 0
Answers: 0
May 2011
edited May 2011
Thanks for you help.
Here is the code:
[code]
$.ajax({
url: 'report_generator.php?action=PrepTable',
dataType: 'json',
type: 'post',
data: $('#ReportForm').serialize(),
error: function() {
return true;
},
success: function(data) {
if( data )
{
if (data.status == 'success') {
aoColumns = data.aoColumns;
$('#ReportGeneratorControls').hide();
$('#ReportSection').show();
$('#ReportSection').html('');
TableTools.DEFAULTS.aButtons = [ "copy", "csv", "pdf", "print" ];
console.log(aoColumns);
$('#ReportTable').dataTable({
'sDom': 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
"copy",
"csv",
"pdf",
"print",
{
"sExtends": "text",
"sButtonText": "Export All",
"fnComplete": function ( nButton, oConfig, oFlash, sFlash ) {
$.ajax({
url: 'report_exporter.php',
dataType: 'json',
type: 'post',
data: $('#form1').serialize(),
error: function() {
return true;
},
success: function(data) {
if( data )
{
if (data.status == 'success') {
$("#ExportedFileSection").show();
$("#ExportedFile").attr("href", data.filename);
}
}
}
});
}
}
]
},
'aoColumns': aoColumns,
'bProcessing': true,
'bServerSide': true,
'sAjaxSource': 'report_generator.php?action=Generate',
'fnServerData': function ( sSource, aoData, fnCallback ) {
$('#ReportForm :input').each(function(){
aoData.push({"name": $(this).attr('name'),"value":$(this).val()});
});
$.ajax({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': fnCallback
});
}
} );
$('#Busy').hide();
} else if (data.status == 'error') {
alert('An error occured retrieve report options');
}
}
}
});
[/code]
This is how I formed the aoColumns array in PHP:
[code]
foreach ($sColumns as $s) {
$output['aoColumns'][]['sTitle'] = $s;
}
[/code]
ChrisGedrim
Posts: 10
Questions: 0
Answers: 0
May 2011
So, assuming you were using php you would need to do something like this as your ajax method:
[code]
<?php
echo json_encode(
array(
array(
'sTitle' => 'id'
),
array(
'sTitle' => 'phone'
),
array(
'sTitle' => 'message'
),
array(
'sTitle' => 'status'
),
)
);
[/code]
skione
Posts: 7
Questions: 0
Answers: 0
May 2011
Chris,
Thanks again, you responded so quickly. I figured it out and edited my post.
Ski
This discussion has been closed.
Sign In
·
Register
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Register
Quick Links
Categories
Recent Discussions
Unanswered
Categories
74.2K
All Categories
56
Priority support
24K
Free community support
996
General
14
Announcements
2.5K
DataTables
75
DataTables 2
1.3K
DataTables 1.10
92
DataTables 1.9
35
DataTables 1.8
9
CloudTables
2.2K
Editor
2.8K
Extensions
20
AutoFill
310
Buttons
50
ColVis
30
DateTime
68
FixedColumns
50
FixedHeader
31
ColReorder
30
KeyTable
103
Responsive
23
RowReorder
41
Scroller
163
SearchBuilder
193
SearchPanes
107
Select
26
StateRestore
22
TableTools
219
Bug reports
67
Feature requests
100
Plug-ins
11
Blog
69
Web-site
Replies
As Allan says in this thread: http://datatables.net/forums/comments.php?DiscussionID=3519 - you would have to make a separate ajax call to get the aoColumns settings with which to initialize the table:
[code]
var aoColumns;
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": "http://mydomain.com/aoColumns_data",
"success": function(data){
aoColumns = data.aoColumns
}
} );
$('#example').dataTable( {
"aoColumns": aoColumns
} );
[/code]
- Chris
(NOTE: this is untested, and your server-side script would have to return the aoColumns array in the format needed by DataTables)
"aoColumns":[{"sTitle":"id"},{"sTitle":"phone"},{"sTitle":"message"},{"sTitle":"status"}]
Here is the code:
[code]
$.ajax({
url: 'report_generator.php?action=PrepTable',
dataType: 'json',
type: 'post',
data: $('#ReportForm').serialize(),
error: function() {
return true;
},
success: function(data) {
if( data )
{
if (data.status == 'success') {
aoColumns = data.aoColumns;
$('#ReportGeneratorControls').hide();
$('#ReportSection').show();
$('#ReportSection').html('');
TableTools.DEFAULTS.aButtons = [ "copy", "csv", "pdf", "print" ];
console.log(aoColumns);
$('#ReportTable').dataTable({
'sDom': 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
"copy",
"csv",
"pdf",
"print",
{
"sExtends": "text",
"sButtonText": "Export All",
"fnComplete": function ( nButton, oConfig, oFlash, sFlash ) {
$.ajax({
url: 'report_exporter.php',
dataType: 'json',
type: 'post',
data: $('#form1').serialize(),
error: function() {
return true;
},
success: function(data) {
if( data )
{
if (data.status == 'success') {
$("#ExportedFileSection").show();
$("#ExportedFile").attr("href", data.filename);
}
}
}
});
}
}
]
},
'aoColumns': aoColumns,
'bProcessing': true,
'bServerSide': true,
'sAjaxSource': 'report_generator.php?action=Generate',
'fnServerData': function ( sSource, aoData, fnCallback ) {
$('#ReportForm :input').each(function(){
aoData.push({"name": $(this).attr('name'),"value":$(this).val()});
});
$.ajax({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': fnCallback
});
}
} );
$('#Busy').hide();
} else if (data.status == 'error') {
alert('An error occured retrieve report options');
}
}
}
});
[/code]
This is how I formed the aoColumns array in PHP:
[code]
foreach ($sColumns as $s) {
$output['aoColumns'][]['sTitle'] = $s;
}
[/code]
[code]
<?php
echo json_encode(
array(
array(
'sTitle' => 'id'
),
array(
'sTitle' => 'phone'
),
array(
'sTitle' => 'message'
),
array(
'sTitle' => 'status'
),
)
);
[/code]
Thanks again, you responded so quickly. I figured it out and edited my post.
Ski