Download file associated with row from server
Download file associated with row from server
I have a database that contains a link to a file in one of the columns. I would like to download the file when I have the row selected. Is there any way to do this? I was thinking about having a download button at the top (just like TableTools) so that when the user clicks it (and a row is selected) the associated file is downloaded.
Thanks for the help!
Thanks for the help!
This discussion has been closed.
Replies
Is there a way to link to a file from the database this with? Here is my initialization code
[code]
$('#documents').dataTable({
"bProcessing": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "scripts/documents.php",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "col1", "sClass": "center", "sWidth":"300px" },
{ "mData": "col2", "sClass": "center" },
{ "mData": "col3", "sClass": "center", "sWidth":"100px" },
{ "mData": "col4", "sClass": "center" }
],
"sDom": 'T<"clear">Rlfrtip',
"oTableTools": {
"sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
"sRowSelect": "single",
"aButtons": ["select_all", "select_none",
{
"sExtends": "collection",
"sButtonText": "Export",
"aButtons": [
{ "sExtends": "copy", "bSelectedOnly": true },
{ "sExtends": "csv", "bSelectedOnly": true },
{ "sExtends": "xls", "bSelectedOnly": true },
{ "sExtends": "pdf", "bSelectedOnly": true },
{ "sExtends": "print", "sButtonText": "Print View" }
]
}
]
}
});
[/code]
And here is the function that I took from the example page I link to
[code]
function fnFormatDetails ( oTable, nTr ) {
var aData = oTable.fnGetData( nTr );
var sOut = '';
sOut += 'File Name:' + mData[3] +'';
sOut += 'Download PDF:Hello World!';
sOut += '';
return sOut;
}
$(document).ready(function() {
/*
* Insert a 'details' column to the table
*/
var nCloneTh = document.createElement( 'th' );
var nCloneTd = document.createElement( 'td' );
nCloneTd.innerHTML = '';
nCloneTd.className = "center";
$('#documents thead tr').each( function () {
this.insertBefore( nCloneTh, this.childNodes[0] );
} );
$('#documents tbody tr').each( function () {
this.insertBefore( nCloneTd.cloneNode( true ), this.childNodes[0] );
} );
/*
* Initialse DataTables, with no sorting on the 'details' column
*/
var oTable = $('documents').dataTable( {
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0 ] }
],
"aaSorting": [[1, 'asc']]
});
/* Add event listener for opening and closing details
* Note that the indicator for showing which row is open is not controlled by DataTables,
* rather it is done here
*/
$('#documents tbody td img').live('click', function () {
var nTr = this.parentNode.parentNode;
if ( this.src.match('details_close') )
{
/* This row is already open - close it */
this.src = "~/Images/details_open.png";
oTable.fnClose( nTr );
}
else
{
/* Open this row */
this.src = "~/Images/details_close.png";
oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
}
} );
} );
[/code]
All this does is create a new column on the left, and it shifted all the data one column over (So the col1 data is where the image should be, col2 is where col1 should be, etc.). There are no columns under the col4 thead, and none of the images are showing up
However, I looked at mRender, and it seems like it would be the easiest. Here is what I have now
[code]
$('#documents').dataTable({
"bProcessing": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "scripts/documents.php",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "col1", "sClass": "center", "sWidth": "300px" },
{ "mData": "col2", "sClass": "center" },
{ "mData": "col3", "sClass": "center", "sWidth": "100px" },
{ "mData": "col4", "sClass": "center" },
{
"mData": "DirectoryPath",
"mRender": function (data, type, full) {
return 'Download';
}
}
],
"sDom": 'T<"clear">Rlfrtip',
"oTableTools": {
"sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
"sRowSelect": "single",
"aButtons": ["select_all", "select_none",
{
"sExtends": "collection",
"sButtonText": "Export",
"aButtons": [
{"sExtends": "copy", "bSelectedOnly": true },
{ "sExtends": "csv", "bSelectedOnly": true },
{ "sExtends": "xls", "bSelectedOnly": true },
{ "sExtends": "pdf", "bSelectedOnly": true },
{ "sExtends": "print", "sButtonText": "Print View" }
]
}
]
}
});
[/code]
It works! Thank you very much