使用服务器端处理从 DataTables 导出全部?
Export All from DataTables with Server Side processing?
我有 tables 使用 DataTables 服务器端处理显示在我的网站上。我希望能够 'Export All' 并导出所有行,而不仅仅是显示的那些行。有60000+行和65+列,所以必须用服务器端处理。
我尝试了一些方法,但到目前为止没有任何效果。
我试过这个:
{ extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}
只导出页面上显示的行。
我试过这个:
{
text: 'Export All to Excel',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('draw', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };
$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
}).draw();
}
}
这会将整个 table 的数据发送到屏幕,而不是使用分页并将整个数据集发送到 excel 文件。
我在 Google 和此处进行了搜索,但没有找到有效的解决方案。
我还应该提到,我想根据 table 上设置的当前过滤器全部导出。这样最终用户将只获得他们正在搜索的那些行的导出。他们通常将其限制在 30k - 40k 行,仍然有 65+ 列。我(还)不允许 remove/hide 列。
EDIT/UPDATE
这是一个次要考虑因素:如果我不能从服务器的响应中导出所有内容,我可以在服务器上构建 Excel 文件吗?我的服务器没有安装 Excel,我仍然希望我的最终用户获取该文件。我确信我必须找到一种方法让 Excel 进入我的服务器,但是我如何将任何创建的文件传输给最终用户,甚至比仅发送响应更快整个数据集并在用户计算机上创建 Excel 文件?
编辑
建议我尝试 jquery 的 $.ajax()
来让它工作。如果有人可以告诉我该怎么做,我会尝试使用第三个按钮。
我已经可以提取所有数据,使用用户添加的相同过滤器和排序,并通过一个按钮完成。上面的第二次尝试是这样做的,但将其发送到屏幕。我有 PHPExcel 和一个可以创建 Excel sheet 的文件。我如何将我在第二个按钮中获得的内容发送到另一个文件以创建 Excel sheet?我认为使用 jquery 的 $.ajax()
可能有用,我只是不知道如何得到它。我知道我必须使用 $_POST
,因为数据可能太大而无法使用 $_GET
将数据发送到 PHPExcel 文件。
我已经可以导出为 CSV,但我需要导出一些 CSV 没有的格式。这就是为什么我要麻烦使用 PHPExcel.
编辑三
我正在尝试这个,虽然它还没有工作:
{
text: 'Export all to Excel II',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('export', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };
$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
})
}
}
编辑 4
希望是最后一次编辑。
我知道我必须做三件事才能完成这项工作:
- 获取当前排序和过滤
- 获取长度设置为-1的数据集
将此发送到 PHPExcel 文件以处理和创建 Excel 文件
我可以像这样创建一个按钮:
{
文本:'Export all Data to Excel',
行动:
}
我只是不知道需要采取什么行动。
我上面的第二次尝试提取了我需要的整个数据集,但将其发送到屏幕而不是我的 PHPExcel 文件(ExportAllToExcel.php).
我一直在努力解决这个问题,但还没有取得太大进展。我被告知我需要使用 $.ajax()
来执行此操作,我被告知我不需要使用它。我试过有和没有,但都无法到达任何地方。
我也试过用这个没有效果:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
"text": "Export All Test",
action: function (e, dt, node, config)
{
var SearchData = dt.search();
var OrderData = dt.order();
alert("Test Data for Searching: " + SearchData);
alert("Test Data for Ordering: " + OrderData);
}
};
我大部分时间都在用这个。现在超时了,但这是一个单独的问题,因为数据大小与此工作无关。对于小型数据集,它工作得很好。
这是我创建按钮的方式(我在这里使用的是 export 按钮):
"buttons": [{
extend: 'collection',
text: 'Selection',
buttons: ['selectAll', 'selectNone']
}, {
extend: 'collection',
text: 'Export',
buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}]
}
]
这是上面创建的按钮的初始化:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
id: 'ExportButton',
text: "Export All Test III",
action: function (e, dt, node, config)
{
var SearchData = dt.rows({ filter: 'applied' }).data();
var SearchData1 = dt.search();
console.log(SearchData);
var OrderData = dt.order();
console.log(SearchData1);
var NumCol = SearchData[0].length;
var NumRow = SearchData.length;
var SearchData2 = [];
for (j = 0; j < NumRow; j++)
{
var NewSearchData = SearchData[j];
for (i = 0; i < NewSearchData.length; i++)
{
NewSearchData[i] = NewSearchData[i].replace("<div class='Scrollable'>", "");
NewSearchData[i] = NewSearchData[i].replace("</div>", "");
}
SearchData2.push([NewSearchData]);
}
for (i = 0; i < SearchData2.length; i++)
{
for (j = 0; j < SearchData2[i].length; j++)
{
SearchData2[i][j] = SearchData2[i][j].join('::');
}
}
SearchData2 = SearchData2.join("%%");
window.location.href = './ServerSide.php?ExportToExcel=Yes';
}
};
这里是ServerSide.php文件中获取数据并将其发送到服务器进行处理的部分:
require('FilterSort.class.php');
if (isset($_GET['ExportToExcel']) && $_GET['ExportToExcel'] == 'Yes')
{
$request = @unserialize($_COOKIE['KeepPost']);
$DataReturn = json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader));
require './ExportAllToExcel.php';
}
else
{
echo json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader));
}
这就是我设置用于保留搜索和排序标准的 cookie 的方式:
if(isset($_POST['draw']))
{
$KeepPost = $_POST;
$KeepPost['length'] = -1;
$PostKept = serialize($KeepPost);
setcookie("KeepPost",$PostKept,time() + (60*60*24*7));
}
所有这些组合将正确的条件发送到 FilterSort.class.php,它应该处理条件和 return 数据集到 ExportAllToExcell.php 然后创建 Excel 文件。现在我正在向它发送大量报告,但它超时了。
更新
我稍微改变了我这样做的方式:
这是一组新按钮:
"buttons": [{
extend: 'collection',
text: 'Export',
buttons: ['export', { extend: 'csv',
text: 'Export All To CSV', //Export all to CSV file
action: function (e, dt, node, config)
{
window.location.href = './ServerSide.php?ExportToCSV=Yes';
}
}, 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page', //Export to Excel only the current page and highlight the first row as headers
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}]
}
]
下面是我如何创建 Export All to Excel 按钮:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert', //Adds the "Export all to Excel" button
id: 'ExportButton',
text: "Export All To Excel",
action: function (e, dt, node, config)
{
window.location.href = './ServerSide.php?ExportToExcel=Yes';
}
};
这些现在将数据发送到我之前使用的同一个 ServerSide.php 文件:
require('FilterSort.class.php');
if (isset($_GET['ExportToExcel']) && $_GET['ExportToExcel'] == 'Yes')
{
include 'Helper/LogReport.php';
$GetSQL = "Select Value from PostKept where UserName = '" .$_COOKIE['UserName']. "'";
$KeepResult = $conn->query($GetSQL);
$KeepResults = $KeepResult->fetchALL(PDO::FETCH_ASSOC);
$request = unserialize($KeepResults[0]['Value']);
$DataReturn = json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader,1));
require './ExportAllToExcel.php';
我还更改了保留查询的方式,我现在还保留了 Table Name 和 UserName 像这样:
include 'DBConn.php';
$KeepPost = $_POST; //POST holds all the data for the search
$KeepPost['length'] = -1; //-1 means pulling the whole table
$PostKept = serialize($KeepPost); //This takes the array of data and turns it into a string for storage in SQL
$SQLCheck = "select distinct UserName from PostKept"; //Gets all the distinct Usernames of users that have used the Report Dashboard.
$sth = $conn->query($SQLCheck);
$CheckedUser = $sth->fetchALL(PDO::FETCH_ASSOC);
foreach($CheckedUser as $User)
{
foreach($User as $Index => $Who)
{
$FoundUsers[] = $Who; //Taking all the found users and placing them into a simpler array for searching later
}
}
if(isset($_COOKIE['UserName']) && in_array($_COOKIE['UserName'],$FoundUsers)) //If the user already has an entry update it with new information
{
$TSQL = "UPDATE PostKept set Value = '" .$PostKept. "', TableName = '" .$TableName. "' where UserName = '" .$_COOKIE['UserName']. "'";
}
else
{
if(isset($_COOKIE['UserName'])) //If this is a new user
{
$TSQL = "INSERT into PostKept(Value, TableName, UserName) select '" .$PostKept. "','" .$TableName. "','" .$_COOKIE['UserName']. "'";
}
else //If this is on the Prod site and the User info is not yet kept
{
$TSQL = "INSERT into PostKept(Value, TableName) select '" .$PostKept. "','" .$TableName. "'";
}
}
$sth = $conn->prepare($TSQL);
$sth->execute();
这就是现在将数据发送到我拥有的 ExportAllToExcel.php 文件然后它又创建文件的所有组合。
在按钮中:
action: function (e, dt, node, config) {
var formData = 'yourfilters';
formData.begin = '0';
formData.length = 'yourTotalSize';
$http({
url: 'yourURL',
method: 'POST',
data: JSON.stringify(formData)
}).then(function (ajaxReturnedData) {
dt.rows.add(ajaxReturnedData.data).draw();
$.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
});}
我刚刚 运行 进入这个并想出了一个替代解决方案。
在数据表选项中,添加:
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
这将允许用户 select 所有行,并在 'length' 查询字符串参数中将 -1 发送到服务器。在服务器端,您需要处理负数并在收到 -1 时允许 return 所有行。
这将显示 table 中的所有行并将导出所有行。
我知道对于 50-60K 行这可能不是 suitable 但对于较小的数据集,这可以在服务器端和客户端都没有实现任何额外代码的情况下工作。
首先在数据表中添加如下代码
"dom": 'Blfrtip',
"buttons": [
{
"extend": 'excel',
"text": '<button class="btn"><i class="fa fa-file-excel-o" style="color: green;"></i> Excel</button>',
"titleAttr": 'Excel',
"action": newexportaction
},
],
然后在 $(document).ready() 函数中添加这个函数
function newexportaction(e, dt, button, config) {
var self = this;
var oldStart = dt.settings()[0]._iDisplayStart;
dt.one('preXhr', function (e, s, data) {
// Just this once, load all data from the server...
data.start = 0;
data.length = 2147483647;
dt.one('preDraw', function (e, settings) {
// Call the original action function
if (button[0].className.indexOf('buttons-copy') >= 0) {
$.fn.dataTable.ext.buttons.copyHtml5.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-excel') >= 0) {
$.fn.dataTable.ext.buttons.excelHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.excelFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-csv') >= 0) {
$.fn.dataTable.ext.buttons.csvHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.csvHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.csvFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-pdf') >= 0) {
$.fn.dataTable.ext.buttons.pdfHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.pdfHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.pdfFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-print') >= 0) {
$.fn.dataTable.ext.buttons.print.action(e, dt, button, config);
}
dt.one('preXhr', function (e, s, data) {
// DataTables thinks the first item displayed is index 0, but we're not drawing that.
// Set the property to what it was before exporting.
settings._iDisplayStart = oldStart;
data.start = oldStart;
});
// Reload the grid with the original page. Otherwise, API functions like table.cell(this) don't work properly.
setTimeout(dt.ajax.reload, 0);
// Prevent rendering of the full data to the DOM
return false;
});
});
// Requery the server with the new one-time export settings
dt.ajax.reload();
}
我有 tables 使用 DataTables 服务器端处理显示在我的网站上。我希望能够 'Export All' 并导出所有行,而不仅仅是显示的那些行。有60000+行和65+列,所以必须用服务器端处理。
我尝试了一些方法,但到目前为止没有任何效果。
我试过这个:
{ extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}
只导出页面上显示的行。
我试过这个:
{
text: 'Export All to Excel',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('draw', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };
$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
}).draw();
}
}
这会将整个 table 的数据发送到屏幕,而不是使用分页并将整个数据集发送到 excel 文件。
我在 Google 和此处进行了搜索,但没有找到有效的解决方案。
我还应该提到,我想根据 table 上设置的当前过滤器全部导出。这样最终用户将只获得他们正在搜索的那些行的导出。他们通常将其限制在 30k - 40k 行,仍然有 65+ 列。我(还)不允许 remove/hide 列。
EDIT/UPDATE
这是一个次要考虑因素:如果我不能从服务器的响应中导出所有内容,我可以在服务器上构建 Excel 文件吗?我的服务器没有安装 Excel,我仍然希望我的最终用户获取该文件。我确信我必须找到一种方法让 Excel 进入我的服务器,但是我如何将任何创建的文件传输给最终用户,甚至比仅发送响应更快整个数据集并在用户计算机上创建 Excel 文件?
编辑
建议我尝试 jquery 的 $.ajax()
来让它工作。如果有人可以告诉我该怎么做,我会尝试使用第三个按钮。
我已经可以提取所有数据,使用用户添加的相同过滤器和排序,并通过一个按钮完成。上面的第二次尝试是这样做的,但将其发送到屏幕。我有 PHPExcel 和一个可以创建 Excel sheet 的文件。我如何将我在第二个按钮中获得的内容发送到另一个文件以创建 Excel sheet?我认为使用 jquery 的 $.ajax()
可能有用,我只是不知道如何得到它。我知道我必须使用 $_POST
,因为数据可能太大而无法使用 $_GET
将数据发送到 PHPExcel 文件。
我已经可以导出为 CSV,但我需要导出一些 CSV 没有的格式。这就是为什么我要麻烦使用 PHPExcel.
编辑三
我正在尝试这个,虽然它还没有工作:
{
text: 'Export all to Excel II',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('export', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };
$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
})
}
}
编辑 4
希望是最后一次编辑。
我知道我必须做三件事才能完成这项工作:
- 获取当前排序和过滤
- 获取长度设置为-1的数据集
将此发送到 PHPExcel 文件以处理和创建 Excel 文件 我可以像这样创建一个按钮:
{ 文本:'Export all Data to Excel', 行动: }
我只是不知道需要采取什么行动。
我上面的第二次尝试提取了我需要的整个数据集,但将其发送到屏幕而不是我的 PHPExcel 文件(ExportAllToExcel.php).
我一直在努力解决这个问题,但还没有取得太大进展。我被告知我需要使用 $.ajax()
来执行此操作,我被告知我不需要使用它。我试过有和没有,但都无法到达任何地方。
我也试过用这个没有效果:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
"text": "Export All Test",
action: function (e, dt, node, config)
{
var SearchData = dt.search();
var OrderData = dt.order();
alert("Test Data for Searching: " + SearchData);
alert("Test Data for Ordering: " + OrderData);
}
};
我大部分时间都在用这个。现在超时了,但这是一个单独的问题,因为数据大小与此工作无关。对于小型数据集,它工作得很好。
这是我创建按钮的方式(我在这里使用的是 export 按钮):
"buttons": [{
extend: 'collection',
text: 'Selection',
buttons: ['selectAll', 'selectNone']
}, {
extend: 'collection',
text: 'Export',
buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}]
}
]
这是上面创建的按钮的初始化:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
id: 'ExportButton',
text: "Export All Test III",
action: function (e, dt, node, config)
{
var SearchData = dt.rows({ filter: 'applied' }).data();
var SearchData1 = dt.search();
console.log(SearchData);
var OrderData = dt.order();
console.log(SearchData1);
var NumCol = SearchData[0].length;
var NumRow = SearchData.length;
var SearchData2 = [];
for (j = 0; j < NumRow; j++)
{
var NewSearchData = SearchData[j];
for (i = 0; i < NewSearchData.length; i++)
{
NewSearchData[i] = NewSearchData[i].replace("<div class='Scrollable'>", "");
NewSearchData[i] = NewSearchData[i].replace("</div>", "");
}
SearchData2.push([NewSearchData]);
}
for (i = 0; i < SearchData2.length; i++)
{
for (j = 0; j < SearchData2[i].length; j++)
{
SearchData2[i][j] = SearchData2[i][j].join('::');
}
}
SearchData2 = SearchData2.join("%%");
window.location.href = './ServerSide.php?ExportToExcel=Yes';
}
};
这里是ServerSide.php文件中获取数据并将其发送到服务器进行处理的部分:
require('FilterSort.class.php');
if (isset($_GET['ExportToExcel']) && $_GET['ExportToExcel'] == 'Yes')
{
$request = @unserialize($_COOKIE['KeepPost']);
$DataReturn = json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader));
require './ExportAllToExcel.php';
}
else
{
echo json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader));
}
这就是我设置用于保留搜索和排序标准的 cookie 的方式:
if(isset($_POST['draw']))
{
$KeepPost = $_POST;
$KeepPost['length'] = -1;
$PostKept = serialize($KeepPost);
setcookie("KeepPost",$PostKept,time() + (60*60*24*7));
}
所有这些组合将正确的条件发送到 FilterSort.class.php,它应该处理条件和 return 数据集到 ExportAllToExcell.php 然后创建 Excel 文件。现在我正在向它发送大量报告,但它超时了。
更新
我稍微改变了我这样做的方式:
这是一组新按钮:
"buttons": [{
extend: 'collection',
text: 'Export',
buttons: ['export', { extend: 'csv',
text: 'Export All To CSV', //Export all to CSV file
action: function (e, dt, node, config)
{
window.location.href = './ServerSide.php?ExportToCSV=Yes';
}
}, 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page', //Export to Excel only the current page and highlight the first row as headers
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
}]
}
]
下面是我如何创建 Export All to Excel 按钮:
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert', //Adds the "Export all to Excel" button
id: 'ExportButton',
text: "Export All To Excel",
action: function (e, dt, node, config)
{
window.location.href = './ServerSide.php?ExportToExcel=Yes';
}
};
这些现在将数据发送到我之前使用的同一个 ServerSide.php 文件:
require('FilterSort.class.php');
if (isset($_GET['ExportToExcel']) && $_GET['ExportToExcel'] == 'Yes')
{
include 'Helper/LogReport.php';
$GetSQL = "Select Value from PostKept where UserName = '" .$_COOKIE['UserName']. "'";
$KeepResult = $conn->query($GetSQL);
$KeepResults = $KeepResult->fetchALL(PDO::FETCH_ASSOC);
$request = unserialize($KeepResults[0]['Value']);
$DataReturn = json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader,1));
require './ExportAllToExcel.php';
我还更改了保留查询的方式,我现在还保留了 Table Name 和 UserName 像这样:
include 'DBConn.php';
$KeepPost = $_POST; //POST holds all the data for the search
$KeepPost['length'] = -1; //-1 means pulling the whole table
$PostKept = serialize($KeepPost); //This takes the array of data and turns it into a string for storage in SQL
$SQLCheck = "select distinct UserName from PostKept"; //Gets all the distinct Usernames of users that have used the Report Dashboard.
$sth = $conn->query($SQLCheck);
$CheckedUser = $sth->fetchALL(PDO::FETCH_ASSOC);
foreach($CheckedUser as $User)
{
foreach($User as $Index => $Who)
{
$FoundUsers[] = $Who; //Taking all the found users and placing them into a simpler array for searching later
}
}
if(isset($_COOKIE['UserName']) && in_array($_COOKIE['UserName'],$FoundUsers)) //If the user already has an entry update it with new information
{
$TSQL = "UPDATE PostKept set Value = '" .$PostKept. "', TableName = '" .$TableName. "' where UserName = '" .$_COOKIE['UserName']. "'";
}
else
{
if(isset($_COOKIE['UserName'])) //If this is a new user
{
$TSQL = "INSERT into PostKept(Value, TableName, UserName) select '" .$PostKept. "','" .$TableName. "','" .$_COOKIE['UserName']. "'";
}
else //If this is on the Prod site and the User info is not yet kept
{
$TSQL = "INSERT into PostKept(Value, TableName) select '" .$PostKept. "','" .$TableName. "'";
}
}
$sth = $conn->prepare($TSQL);
$sth->execute();
这就是现在将数据发送到我拥有的 ExportAllToExcel.php 文件然后它又创建文件的所有组合。
在按钮中:
action: function (e, dt, node, config) {
var formData = 'yourfilters';
formData.begin = '0';
formData.length = 'yourTotalSize';
$http({
url: 'yourURL',
method: 'POST',
data: JSON.stringify(formData)
}).then(function (ajaxReturnedData) {
dt.rows.add(ajaxReturnedData.data).draw();
$.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
});}
我刚刚 运行 进入这个并想出了一个替代解决方案。
在数据表选项中,添加:
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
这将允许用户 select 所有行,并在 'length' 查询字符串参数中将 -1 发送到服务器。在服务器端,您需要处理负数并在收到 -1 时允许 return 所有行。
这将显示 table 中的所有行并将导出所有行。
我知道对于 50-60K 行这可能不是 suitable 但对于较小的数据集,这可以在服务器端和客户端都没有实现任何额外代码的情况下工作。
首先在数据表中添加如下代码
"dom": 'Blfrtip',
"buttons": [
{
"extend": 'excel',
"text": '<button class="btn"><i class="fa fa-file-excel-o" style="color: green;"></i> Excel</button>',
"titleAttr": 'Excel',
"action": newexportaction
},
],
然后在 $(document).ready() 函数中添加这个函数
function newexportaction(e, dt, button, config) {
var self = this;
var oldStart = dt.settings()[0]._iDisplayStart;
dt.one('preXhr', function (e, s, data) {
// Just this once, load all data from the server...
data.start = 0;
data.length = 2147483647;
dt.one('preDraw', function (e, settings) {
// Call the original action function
if (button[0].className.indexOf('buttons-copy') >= 0) {
$.fn.dataTable.ext.buttons.copyHtml5.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-excel') >= 0) {
$.fn.dataTable.ext.buttons.excelHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.excelFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-csv') >= 0) {
$.fn.dataTable.ext.buttons.csvHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.csvHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.csvFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-pdf') >= 0) {
$.fn.dataTable.ext.buttons.pdfHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.pdfHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.pdfFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-print') >= 0) {
$.fn.dataTable.ext.buttons.print.action(e, dt, button, config);
}
dt.one('preXhr', function (e, s, data) {
// DataTables thinks the first item displayed is index 0, but we're not drawing that.
// Set the property to what it was before exporting.
settings._iDisplayStart = oldStart;
data.start = oldStart;
});
// Reload the grid with the original page. Otherwise, API functions like table.cell(this) don't work properly.
setTimeout(dt.ajax.reload, 0);
// Prevent rendering of the full data to the DOM
return false;
});
});
// Requery the server with the new one-time export settings
dt.ajax.reload();
}