
Laravel Yajra Datatables Export to Excel CSV Button
Laravel Yajra Datatables is a powerful package that makes it easy to create dynamic data tables with advanced features. To add an export to Excel and CSV button using Yajra Datatables, follow these steps:
- Install and Set Up Yajra Datatables: If you haven’t already, install the Yajra Datatables package in your Laravel project:
composer require yajra/laravel-datatables-oracle
Follow the package documentation to set up your DataTables and integrate them into your application.
- Install DataTables Buttons and JSZip: For the export buttons, you need to install the DataTables Buttons and JSZip libraries. You can do this using npm:
npm install datatables.net-buttons jszip
Import the necessary CSS and JavaScript files in your application, such as in your resources/js/app.js
file:
import 'datatables.net-buttons/js/buttons.html5.min';
import 'jszip/dist/jszip.min';
import 'datatables.net-buttons-dt/css/buttons.dataTables.min.css';
- Add Export Buttons to Your DataTable: In your Blade view where you render your DataTable, add the export buttons configuration to enable the export buttons:
<script>
$(document).ready(function() {
$('#yourDataTableId').DataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'excel', 'csv', 'pdf', 'print'
],
// other DataTable options...
});
});
</script>
Replace 'yourDataTableId'
with the actual ID of your DataTable element.
- Customize Export Options (Optional): You can customize the export options by adding additional configurations to the
buttons
array. For example, you can add theextend
option to customize the export filename:
buttons: [
'copy', 'excel', 'csv', 'pdf', 'print',
{
extend: 'excel',
title: 'YourExportFileName'
}
]
- Testing: Start your development server:
php artisan serve
Visit the page with your DataTable and you should see the export buttons (Copy, Excel, CSV, PDF, Print) above the table. Clicking on these buttons will allow users to export the table data in the corresponding format.
Remember that these steps are a general guideline for adding export buttons using Yajra Datatables and DataTables Buttons. You might need to adjust the code to match your specific DataTable setup and requirements.