Download the PHP package wdev-rs/laravel-datagrid without Composer
On this page you can find all versions of the php package wdev-rs/laravel-datagrid. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wdev-rs/laravel-datagrid
More information about wdev-rs/laravel-datagrid
Files in wdev-rs/laravel-datagrid
Package laravel-datagrid
Short Description Laravel integration for Grid.js server side processing
License MIT
Homepage https://github.com/wdev-rs/laravel-datagrid
Informations about the package laravel-datagrid
LaravelDatagrid
This packages makes it easy to create data-grid for your Laravel application, for example admin panels, or any other searchable and sortable list. This package comes with 2 frontend options:
- Grid.js
- DataGrid's own Vue 3 frontend.
Both frontends offer server side functionalities like searching, sorting and pagination.
Demo
Please find a demo application here and the source code of the demo application here;
Installation
You can install the package via composer:
Optional - if you use Grid.js install the package with npm
Publish the vendor files by running the following command:
Using Grid.js frontend
Register the DataGrid fronted Vue.js component by adding the following lines to your app.js
:
Use the component in your view file (or in another component):
Using Datagrid Vue 3 frontend
Register the DataGrid fronted Vue.js component by adding the following lines to your app.js
:
Use the component in your view file (or in another component):
Usage
The base of this package is the \WdevRs\LaravelDatagrid\DataGrid\DataGrid
class. This class is used to define the
columns and the behavior of the datagrid. While you can use this class directly from the controller, I
suggest extending it and create separate classes for each datagrid.
Using the fromQuery
method you can define what should be the base query for the DataGrid. It accepts a Laravel Query Builder object.
The column
method is used to define the columns of the DataGrid, the argument are as follows:
id
- the name of the field in the databasename
- the label which should appear in the DataGrid column headerformatter
- optional, callable allows you to format the display of the column. As you can see from the above example probably the most elegant way to do this is to include a blade view and render it.width
- optional, the with of the columnsortable
- optional, boolean if the column should be sortable, default truesearchable
- optional, boolean if the column should be searchable, default true
The key
method defines the unique identifier for the rows, usually id. Specifying the
key is necessary for the mass actions to work when using datagrid frontend.
Data sources
You can create data grid from different data sources:
- Eloquent queries - use the fromQuery() method
- Collections - use the fromCollection() method
- Arrays - use the fromArray() method
When the DataGrid definition is ready, you can add it to the controller:
If the render
method is called without arguments it will use the default view resources/views/vendor/laravel-datagrid/datagrid.blade.php
,
or you can pass your own view and use the DataGrid there:
If you use Inertia for frontend, you can configire laravel-datagrid to use inertia for rendering instead of blade. First publish the config file:
Change the rendering method in the published config/laravel-datagrid.php
:
Now you can pass the name of the vue component to the render
method, it
is going to be rendered with Inertia.
Available commands
Code related
php artisan make:datagrid <-M|--model> <-F|--fields>
Generates datagrid class, the generated class is placed in \App\DataGrids directory
- -M|--model name of the model to use
- -F|--fields the name of the fields of the comma separated: 'field1,field2,field3' or 'field1:label1,field2:label2,field3:label3'
Generated class
Usage example
Frontend customisations
Using Grid.js
The frontend component of the DataGrid can be found in the resources/js/vendor/laravel-datagrid/gridjs/Components/DataGrid.vue
By default DataGrid comes with one row action, which is the delete action. This action can be found in the following file:
resources/js/vendor/laravel-datagrid/gridjs/actions/delete.js
You can extend it with more custom actions by creating them based on the existing one. To add the to the datagrid,
extend the cols
definition in the DataGrid.vue
:
Using Datagrid Vue3
Datagrid's own vue 3 frontend offers extended functionality compared to grid.js, for example mass actions, filters and row action customisations.
Mass actions
Mass actions is a method to run specific action on multiple records. For example delete multiple records at once.
When using mass actions I suggest using datagrid in a wrapper component.
Mass actions can be defined using the mass-actions
prop in an array
[{'action' : 'massDelete', 'label': 'Delete'}]
Datagrid will fire an event when the user selects rows and runs an action on them.
The name of the event is what you defined in the action
property, in this case massDelete
.
Handle the event on the usual way, the handler gets the array of selectedIds
as an argument:
Please find the code of the complete component below.
Customising the row actions
DataGrid by default adds 2 row action to all rows: edit and delete.
You can easily customise these actions using the actions
slot:
Customising the search
If you'd like to make a more detailed search function instead of the default
search input field, use the filters
slot.
Add your custom logic on the frontend for collecting the data and submitting to the
search endpoint, using the ServerConfig
class, for example:
On the backend override the search
method of the base DataGrid
class,
to implement the custom filtering.
Please check out the demo application's source code for a more details about how to implement these customisations here: Laravel DataGrid demo
The demo app can be found here.
Upgrade from Laravel DataGrid 0.x
Update the vendor assets using --force option:
Using Grid.js
Using datagrid frontend
Update the import paths in the app.js to use correct DataGrid component (grid.js or datagrid), see the example above.
Update the usage of the data-grid component to pass the rows property:
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Daniel Werner
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.