Datatables with ColVis, Tabletools and Custom buttons Codeigniter Example

Datatables with ColVis, Tabletools and Custom buttons Codeigniter Example

matyhatymatyhaty Posts: 11Questions: 1Answers: 0
edited July 2011 in General
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

Replies

  • allanallan Posts: 63,243Questions: 1Answers: 10,419 Site admin
    HI Matt,

    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
  • matyhatymatyhaty Posts: 11Questions: 1Answers: 0
    Hi 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
  • allanallan Posts: 63,243Questions: 1Answers: 10,419 Site admin
    [quote]matyhaty said: I will look to help datatables by putting up further code and example to allow speedy implementation of datatables....[/quote]

    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
  • winonageekwinonageek Posts: 4Questions: 0Answers: 0
    edited February 2012
    Thanks for adding fnGetSelectedData. That is a useful function. Looking over the 2.0.2 code for TableTools, I think this function though is ignoring the mColumns definition for the button. Can you confirm that this function is or is not looking at mColumns when it grabs the selected data?

    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.
This discussion has been closed.