Table of Contents |
This template will aid you in exporting the raw data from a table. It will create a file or it will open a new window where you can copy the delimited text from. I would advise that you upload the javascript attached to this page into your own wiki and change the reference in the template.
NOTE: Attached is a new version of table2CSV.js ensure you download it even if you had it once before. Also you now need the attached PHP file.
| Version | Date | Author | Description |
| 2.0 | 10/29/2009 | rberinger | Added the ability to actually download a file |
| 1.0 | 09/22/2009 | rberinger | Initial Release |
For all the examples on this page, the code is shown before the working example. The code is shown with the syntax extension, and looks like this:
table2CSV{id: str?, text: str?, separator: str?, header: [ list? ], button: bool? }
This means that the actual code on the page should be enclosed in a DekiScript block. If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above.
| Name | Type | Default | Description |
| id | str | n/a | ID or Class of table to export |
| text | str | Export Table | Text to Name Button or Link |
| separator | str | , (comma) | The column separator |
| header | list | n/a | List of alternate column headers |
| button | true/false | false | True will present button false will present Link |
| tofile | true/false | true | True will create a file for download, False will open a window and show results |
Here is the table(s) that we will be using for this example: NOTICE that each has its own ID but both have the same class.
<table cellspacing="1" cellpadding="1" border="1" width="100%" class="exporttable" id="exportme" style="table-layout: _cke_saved_style="table-layout: fixed;">
<thead>
<tr>
<th scope="col">col1</th>
<th scope="col">col2</th>
<th scope="col">col3</th>
</tr>
</thead>
<tbody>
<tr>
<td>value 1-1</td>
<td>value 1-2</td>
<td>value 1-3</td>
</tr>
<tr>
<td>value 2-1</td>
<td>value 2-2</td>
<td>value 2-3</td>
</tr>
</tbody>
</table>
<table cellspacing="1" cellpadding="1" border="1" width="100%" class="exporttable" id="exportthis" style="table-layout: fixed;">
<thead>
<tr>
<th scope="col">col1-1</th>
<th scope="col">col2-2</th>
<th scope="col">col3-3</th>
</tr>
</thead>
<tbody>
<tr>
<td>value 1-1-1</td>
<td>value 1-2-2</td>
<td>value 1-3-3</td>
</tr>
<tr>
<td>value 2-1-1</td>
<td>value 2-2-2</td>
<td>value 2-3-3</td>
</tr>
</tbody>
</table>
This example will simply present a button that when click will show the text of the first table since it was passed the ID of the first table.
In a DekiScript Block:
table2CSV{ id: 'exportme',
text: 'Show Values',
button: true,
tofile: false
}
Here is the result
This example will simply present a button that when click will show the text of the first table since it was passed the ID of the first table, and will change the separator and column header values.
In a DekiScript Block:
table2CSV{ id: 'exportme',
text: 'Show Values',
button: true,
separator: '|',
header: ['Column 1', 'Column 2', 'Column 3'],
tofile: false
}
Here is the result
This example will present a link that when clicked will show the text of both tables since it was passed a class instead of an ID.
In a DekiScript Block:
table2CSV{ id: '.exporttable',
text: 'Show Values',
button: false,
tofile: false
}
Here is the result
Show Values
The original web site where I found the .js file is here. Sorry I don't know his/her name. It ha
var classorid = ($id ?? $0 ?? '#exportthis');
var text = ($text ?? $1 ?? 'Export Table');
var separator = ($separator ?? $2 ?? ',');
var header = ($header ?? $3 ?? []);
var button = ($button ?? $4 ?? false);
var tofile = ($tofile ?? $5 ?? true);
var options = {separator: (separator)};
if(#header != 0) {
let options ..= {header: (header)};
}
if(tofile) {
let options ..= {delivery:'value'};
}
// var ctortext = 'when($this.click) $('..json.emit(options)..').table2CSV('..json.emit(options)..');';
if(button) {
<input value=(text) type="button" ctor="when($this.click) GetTable()" />
} else {
<a href="#" ctor="when($this.click) GetTable(); return false;">;text;</a>
}
<html><head>
<script type="text/javascript" src="http://developer.mindtouch.com/@api/deki/files/4807/=table2CSV.js" ></script>
<script type="text/javascript">"
var GetTable = function() {
var myOpts = "..json.emit(options)..";
var tbl = "..json.emit(classorid)..";
var siteURI = "..json.emit(site.uri).."
var fulluri = siteURI + 'deki/dl/dlstring.php?csv_output=';
if(myOpts.delivery == 'value') {
var csv = $(tbl).table2CSV(myOpts);
location.href = fulluri + encodeURIComponent(csv);
} else {
$(tbl).table2CSV(myOpts);
}
}
"</script>
</head></html>
<?php
/*
This file will generate our CSV table. There is nothing to display on this page,
it is simply used to generate our CSV file and then exit. That way we won't be
re-directed after pressing the export to CSV button on the previous page.
*/
/*
First we'll generate an output variable called out. It'll have all of our text
for the CSV file.
*/
$out = '';
/*Next we'll check to see if our variables posted and if they did we'll simply
append them to out.
*/
if (isset($_GET['csv_output'])) {
$out .= $_GET['csv_output'];
}
/*Now we're ready to create a file. This method generates a filename based on
the current date & time.
*/
$filename = $file."_".date("Y-m-d_H-i",time());
//Generate the CSV file header
header("Content-type: application/vnd.ms-excel;");
//header("Content-type: text/plain; charset=utf-8");
header("Content-disposition: attachment" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
//Print the contents of out to the generated file.
print stripslashes($out);
//Exit the script
exit;
?>
None.
| File | Version | Size | Modified | |
|---|---|---|---|---|
| ||||
| ||||
| Images 0 | ||
|---|---|---|
| No images to display in the gallery. |
Copyright © 2011 MindTouch, Inc. Powered by
Do you think it is a very problem to program such a thing?
Thanks