Datatables with ColVis, Tabletools and Custom buttons Codeigniter Example
Datatables with ColVis, Tabletools and Custom buttons Codeigniter Example
Below is a nice Codeigniter function (in a lib) for a fully featured datatable (well for my means anyway)
This uses the William concepts template library.
This will produce a nice datatable which shows column visible options, tables tools with additional email and zip buttons (note these buttons will pass to relevant controller/function a set of information from column 0 of the table - which I would suggest are ids/identifiers)
Hope its useful for people. Edit, adjust, update this post etc. Share the effort...
[code]
function datatableFull($hides='', $scrolly=500, $showButtons='', $datatable_number='', $return=false)
{
$CI = & get_instance();
$letters = 'CT';
$additionals = '';
$additionals .= "
\"oColVis\": {
\"activate\": \"mouseover\"
},";
$additionals .= "
\"oTableTools\": {
\"sSwfPath\": \"" . base_url() . "swf/copy_cvs_xls_pdf.swf\",
\"sRowSelect\": \"multi\",
" . $buttons . "
},";
$additionals .= "
'sScrollY': '" . $scrolly . "',
'bPaginate': false,
";
$buttons = "
'aButtons': [ 'csv',
'pdf',
'print',
{
'sExtends': 'select_all',
'sButtonText': 'All'
},
{
'sExtends': 'select_none',
'sButtonText': 'None'
},
{
'sExtends': 'text',
'sButtonText': 'Email',
'mColumns': [ 0 ],
'fnClick': function ( nButton, oConfig, oFlash )
{
var ids = '';
//window.location = '" . site_url() . "/communicates/view';
$('#as_datatable .DTTT_selected td:first-child').each(function() {
var text = $(this).text();
//alert(text);
ids += text+'_';
});
//alert(ids);
window.location = '" . site_url() . "/communicates/view/'+ids
}
},
{
'sExtends': 'text',
'sButtonText': 'Zip',
'mColumns': [ 0 ],
'fnClick': function ( nButton, oConfig, oFlash )
{
var ids = '';
$('#as_datatable .DTTT_selected td:first-child').each(function() {
var text = $(this).text();
//alert(text);
ids += text+'_';
});
//alert(ids);
window.location = '" . site_url() . "/persons/zips/'+ids
}
},
],
";
$js = "
$(document).ready(function() {
oTable = $('#as_datatable" . $datatable_number . "').dataTable({
'bJQueryUI': true,
'sPaginationType': 'full_numbers',
\"sDom\": '" . $letters . "lfrtip',
" . $additionals . "
\"aoColumnDefs\": [
{ \"bVisible\": false, \"aTargets\": [ " . $hides . " ] }
]
});
} );
";
if (!$return)
{
$CI->template->add_js($js, 'embed');
}
else
{
return $js;
}
}
[/code]
Regards
Matt
www.s3d.co.uk
This uses the William concepts template library.
This will produce a nice datatable which shows column visible options, tables tools with additional email and zip buttons (note these buttons will pass to relevant controller/function a set of information from column 0 of the table - which I would suggest are ids/identifiers)
Hope its useful for people. Edit, adjust, update this post etc. Share the effort...
[code]
function datatableFull($hides='', $scrolly=500, $showButtons='', $datatable_number='', $return=false)
{
$CI = & get_instance();
$letters = 'CT';
$additionals = '';
$additionals .= "
\"oColVis\": {
\"activate\": \"mouseover\"
},";
$additionals .= "
\"oTableTools\": {
\"sSwfPath\": \"" . base_url() . "swf/copy_cvs_xls_pdf.swf\",
\"sRowSelect\": \"multi\",
" . $buttons . "
},";
$additionals .= "
'sScrollY': '" . $scrolly . "',
'bPaginate': false,
";
$buttons = "
'aButtons': [ 'csv',
'pdf',
'print',
{
'sExtends': 'select_all',
'sButtonText': 'All'
},
{
'sExtends': 'select_none',
'sButtonText': 'None'
},
{
'sExtends': 'text',
'sButtonText': 'Email',
'mColumns': [ 0 ],
'fnClick': function ( nButton, oConfig, oFlash )
{
var ids = '';
//window.location = '" . site_url() . "/communicates/view';
$('#as_datatable .DTTT_selected td:first-child').each(function() {
var text = $(this).text();
//alert(text);
ids += text+'_';
});
//alert(ids);
window.location = '" . site_url() . "/communicates/view/'+ids
}
},
{
'sExtends': 'text',
'sButtonText': 'Zip',
'mColumns': [ 0 ],
'fnClick': function ( nButton, oConfig, oFlash )
{
var ids = '';
$('#as_datatable .DTTT_selected td:first-child').each(function() {
var text = $(this).text();
//alert(text);
ids += text+'_';
});
//alert(ids);
window.location = '" . site_url() . "/persons/zips/'+ids
}
},
],
";
$js = "
$(document).ready(function() {
oTable = $('#as_datatable" . $datatable_number . "').dataTable({
'bJQueryUI': true,
'sPaginationType': 'full_numbers',
\"sDom\": '" . $letters . "lfrtip',
" . $additionals . "
\"aoColumnDefs\": [
{ \"bVisible\": false, \"aTargets\": [ " . $hides . " ] }
]
});
} );
";
if (!$return)
{
$CI->template->add_js($js, 'embed');
}
else
{
return $js;
}
}
[/code]
Regards
Matt
www.s3d.co.uk
This discussion has been closed.
Replies
Thanks very much for posting your code! I'm sure that it will prove very useful to others. Nice use of TableTools as well - love that :-). Off topic: the s3d site looks fantastic as well - very nicely done.
Regards,
Allan
Thank you.
The s3d site is awful! Its getting a face lift as we speak!
I will look to help datatables by putting up further code and example to allow speedy implementation of datatables....
Sometimes with too much power the basics can get hidden (although I have to say the documentation is good)
I still feel there should be a function which can 'retrieve the contents of selected rows' Seems weird that this is missing.
Regards
Matt
www.s3d.co.uk
That would be very welcome indeed! New tutorials are always very useful.
[quote]matyhaty said: I still feel there should be a function which can 'retrieve the contents of selected rows' Seems weird that this is missing.[/quote]
You might like this commit I made to TableTools last week: https://github.com/DataTables/TableTools/commit/d719f0c6e1c783df9139503430bf7a24093a83f8#diff-stat :-). It adds a method called "fnGetSelectedData", which is basically the same as "fnGetSelected", but rather than an array of nodes, it returns an array of data.
Regards,
Allan
Of course I could just be using the tools incorrectly too. What I'm seeing is that when I do an alert on fnGetSelectedData from within fnClick, I get the entire table row of data and not just the first column. I'm setting mColumns: [0] earlier.
Thanks.