Cover Image for Laravel Yajra Datatables Export to Excel CSV Button
247 views

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:

  1. Install and Set Up Yajra Datatables: If you haven’t already, install the Yajra Datatables package in your Laravel project:
Bash
 composer require yajra/laravel-datatables-oracle

Follow the package documentation to set up your DataTables and integrate them into your application.

  1. 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:
Bash
 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:

JavaScript
 import 'datatables.net-buttons/js/buttons.html5.min';
 import 'jszip/dist/jszip.min';
 import 'datatables.net-buttons-dt/css/buttons.dataTables.min.css';
  1. 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:
PHP
 <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.

  1. Customize Export Options (Optional): You can customize the export options by adding additional configurations to the buttons array. For example, you can add the extend option to customize the export filename:
PHP
 buttons: [
     'copy', 'excel', 'csv', 'pdf', 'print',
     {
         extend: 'excel',
         title: 'YourExportFileName'
     }
 ]
  1. Testing: Start your development server:
Bash
 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.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS