Man i had the worst time of my life going through this...
What i needed
The user was to be able to use filter, data range and so on, and then being able to get a csv file with what was displayed as a result of all the filtering.
What i had to do
Bunch of code modification, but it works now. Also a modification of jDom regarding the display of the toggle fields. Let's dig into this :
Find the default.php file of the table you want
Should be in (administrator)/the_name_of_your_component/views/the_name_of_the_view/tmpl/default.php
Add the bunch of code between headerDeclarations and form
// no direct access
defined('_JEXEC') or die('Restricted access');
you_component_Helper::headerDeclarations();
?>
<form action="<?php echo(JRoute::_("index.php")); ?>" method="post" name="adminForm" id="adminForm">
becomes
// no direct access
defined('_JEXEC') or die('Restricted access');
you_component_Helper::headerDeclarations();
?>
<script>
jQuery(document).ready(function() {
jQuery('#convert').click(function() {
var href = 'url_of_your_site/libraries/your_name/export.php?csv=';
var data = jQuery('#grid').tableToCSV();
data = data.replace(/\s+/g, '');
data = data.replace(/\\r/g, "\r\n");
href += encodeURIComponent(data);
jQuery(this).attr('href', href);
});
});
jQuery.fn.tableToCSV = function() {
var csv = '';
var headers = [];
jQuery("#grid").find('th').each(function() {
var header = '';
var $th = jQuery(this);
var text = $th.text();
var header = '"' + text + '"';
headers.push(header);
});
csv += headers.join(',');
csv += '\\r\n';
jQuery("#grid").find('tr').not('tr:first-child').each(function() {
jQuery(this).find('td').each(function() {
var row = jQuery(this).text();
if(!jQuery(this).is('td:last-child')) {
row += ',';
} else {
row += '\\r\n';
}
csv += row;
});
});
return csv;
};
</script>
<form action="<?php echo(JRoute::_("index.php")); ?>" method="post" name="adminForm" id="adminForm">
Add a link that triggers the export
Place this line of code where you want your link to export to appear.
<div><a id="convert">Sauvez en CSV</a></div>
Export.php
Create a new folder in url_of_your_site/libraries/ and call it by your name
Then create a new file (export.php) in url_of_your_site/libraries/your_name/
The content of this file should be
<?
if(empty($_REQUEST['csv'])){
exit;
}
header('Content-Type: application/text; charset=utf-8');
header('Content-Disposition: attachment; filename="table.txt"');
$csv = $_GET['csv'];
$csv = utf8_decode($csv);
echo $csv;
?>
You can see there that you decide the name of the file (table.txt). I saw that some people append the time() so that each downloaded file is unique, if you need it.
If needed, modify jDom for the Toggle Fields
in administrator/your_component_name/dom/html/grid/bool.php,
comment the part that displays the image and the alt attribute, and use the alt attribute to give the link a name
$html .= "<img " /*src='<%IMAGE_SOURCE%>' border='0' "alt='<%ALT%>'"*/
. " title='" . htmlspecialchars($title, ENT_COMPAT, 'UTF-8') . "' /><%ALT%>" .LN;