-
-
Notifications
You must be signed in to change notification settings - Fork 58
ServerSide export with ColVis integration. #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add buttons to export only visible columns
AmirTallap
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much, that did the trick.
|
@FabienLucini @AmirTallap thanks! Released on v4.4.0 🚀 |
|
|
|
It really took me some time to figure out this is a JavaScript only implementation at the moment. Because I couldn't really work out what above code does or where it would need to go, I implemented something myself in a custom base class for all my DataTables: |
This my code how i can add the function >> @Namoshek Thanks bro |
|
@raar4ever You can implement the export of visible columns only by copying over the methods of my example into your table. I would recommend creating a common (abstract) base class for all of your DataTables though. In that case you would place the methods in that class. Important is that you do not enable the button |
|
Hello @FabienLucini @yajra public function html()
{
$dataTable = $this->setBuilder('documents-table', 2)
->parameters([
'initComplete' => 'function () {
window.LaravelDataTables["documents-table"].buttons().container()
.appendTo( "#table-actions")
}',
'fnDrawCallback' => 'function( oSettings ) {
$(".select-picker").selectpicker();
}',
]);
if (canDataTableExport()) {
$dataTable->buttons(
Button::make([
'extend' => 'excel',
'text' => '<i class="fa fa-file-export"></i> ' . trans('app.exportExcel'),
'exportOptions' => [
'columns' => ':visible',
],
]),
Button::make([
'extend' => 'pdf',
'text' => '<i class="fa fa-file-pdf"></i> ' . trans('app.exportPdf'),
'exportOptions' => [
'columns' => ':visible',
],
]),
Button::make([
'extend' => 'print',
'text' => '<i class="fa fa-print"></i>' . trans('app.print'),
])->exportOptions([
'columns' => ':visible',
]),
Button::make([
'extend' => 'colvis',
'text' => '<i class="fa fa-columns"></i> ' . trans('app.columnSelector'),
]),
);
}
return $dataTable;
}
protected function getColumns()
{
$data = [
'check' => [
'title' => '<input type="checkbox" name="select_all_table" id="select-all-table" onclick="selectAllTable(this)">',
'exportable' => false,
'orderable' => false,
'searchable' => false
],
'#' => ['data' => 'DT_RowIndex', 'orderable' => false, 'searchable' => false, 'visible' => false, 'title' => '#'],
__('app.ref') => ['data' => 'id', 'name' => 'id', 'title' => __('app.ref')],
__('modules.employees.employeeId') => Column::computed('user.name')
->title(__('modules.employees.employeeId'))
->data('employee')
->orderable(true)
->exportable(false),
__('app.nationality') => ['data' => 'nationality', 'name' => 'nationality', 'title' => __('app.nationality'), 'exportable' => false],
__('modules.project.documentId') => ['data' => 'document_id', 'name' => 'document_id', 'exportable' => false, 'title' => __('modules.projects.documentId')],
__('app.name') => ['data' => 'document_name', 'name' => 'document_name', 'exportable' => false, 'title' => __('app.name')],
__('modules.projects.documentType') => Column::computed('document_type')
->title(__('modules.projects.documentType'))
->data('document_type')
->orderable(true)
->exportable(false),
__('modules.employees.issueDate') => Column::computed('document_issue_date')
->title(__('modules.employees.issueDate'))
->data('document_issue_date')
->orderable(true)
->exportable(false),
__('modules.employees.expiryDate') => Column::computed('document_expiry_date')
->title(__('modules.employees.expiryDate'))
->data('document_expiry_date')
->orderable(true)
->exportable(false),
__('modules.employees.attachments') => Column::computed('attachments')
->title(__('modules.employees.attachments'))
->data('attachments')
->exportable(false),
];
$action = [
Column::computed('action', __('app.action'))
->exportable(false)
->printable(false)
->orderable(false)
->searchable(false)
->addClass('text-right pr-20')
];
return array_merge($data, CustomFieldGroup::customFieldsDataMerge(new EmployeeDocument()), $action);
} |
|
And these are my versions => |
|
Hey everyone after been a quite a research i found my issue and applying the method mentioned above protected function excludeNotVisibleExportColumns(): void
{
$request = $this->request();
$columns = $this->getColumnsFromBuilder();
if ($request->has('action') && in_array($request->get('action'), ['excel', 'csv'])) {
if ($request->has('visible_columns')) {
$visibleColumns = (array) $request->get('visible_columns');
/** @var Column $column */
foreach ($columns as $column) {
if (!in_array($column->name, $visibleColumns)) {
$this->excludeFromExport[] = $column->name;
}
}
}
}
}but i got another issue
public function html()
{
$dataTable = $this->setBuilder('documents-table', 2)
->parameters([
'initComplete' => 'function () {
window.LaravelDataTables["documents-table"].buttons().container()
.appendTo( "#table-actions");
}',
'fnDrawCallback' => 'function( oSettings ) {
$(".select-picker").selectpicker();
}',
'columnDefs' => '[
{ visible: false, targets: 1 }
]',
]);
if (canDataTableExport()) {
$exportOptions = ['columns' => ':visible'];
$dataTable->buttons(
Button::make([
'extend' => 'excel',
'text' => '<i class="fa fa-file-export"></i> ' . trans('app.exportExcel'),
])->action("function (e, dt, button, config) {
var visibleColumns = dt.columns(':visible').indexes().toArray();
// Map the visible columns to column data
var columnData = visibleColumns.map(function (index) {
return dt.settings()[0].aoColumns[index].data;
});
var url = dt.ajax.url() || '';
var params = dt.ajax.params();
params.action = 'excel';
params._token = $('meta[name=\"csrf-token\"]').attr('content');
params.visible_columns = columnData;
if (url.indexOf('?') > -1) {
return url + '&' + $.param(params);
}
url = url + '?' + $.param(params);
window.location = url;
}")->exportOptions($exportOptions),
Button::make([
'extend' => 'pdf',
'text' => '<i class="fa fa-file-pdf"></i> ' . trans('app.exportPdf'),
])->exportOptions($exportOptions),
Button::make([
'extend' => 'print',
'text' => '<i class="fa fa-print"></i>' . trans('app.print'),
])->exportOptions($exportOptions),
Button::make([
'extend' => 'colvis',
'text' => '<i class="fa fa-columns"></i> ' . trans('app.columnSelector'),
'columnText' => 'function ( dt, idx, title ) {
return (idx+1)+": "+title;
}'
]),
);
}
return $dataTable;
}
$(document).ready(function () {
var table = $('#documents-table').DataTable();
// Define the Excel button with customization for selected columns export
var excelButton = {
extend: 'excel',
text: 'Export Selected Columns',
action: function (e, dt, button, config) {
// Get the list of visible columns
var visibleColumns = dt.columns(':visible').indexes().toArray();
// Map the visible columns to column data
var columnData = visibleColumns.map(function (index) {
return dt.settings()[0].aoColumns[index].data;
});
// @method _buildUrl function(dt, action){}
var url = dt.ajax.url() || '';
var params = dt.ajax.params();
params.action = 'excel';
params._token = $('meta[name="csrf-token"]').attr('content');
params.visible_columns = columnData;
if (url.indexOf('?') > -1) {
return url + '&' + $.param(params);
}
url = url + '?' + $.param(params);
window.location = url;
}
};
// Create DataTable buttons
var buttons = new $.fn.dataTable.Buttons(table, {
buttons: [
{
extend: 'colvis',
text: 'Column Visibility'
},
excelButton
]
});
// Add the buttons to an existing container
buttons.container().appendTo($('.buttons-container'));
}); |
Add buttons to export only visible columns.
It adds the parameter 'visible_columns' in the request, so you can catch it and make your columns 'exportable' => false, in your 'getColumns' method on server side)
Example :