milenmk / laravel-simple-datatables
Simple Table component to create datatables for Livewire components
Requires
- php: ^8.2|^8.3|^8.4
- blade-ui-kit/blade-heroicons: ^2.6
- blade-ui-kit/blade-icons: ^1.8
- illuminate/contracts: ^10.0|^11.0|^12.0
- illuminate/pagination: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- illuminate/view: ^12.2
- livewire/livewire: ^3.0|dev-main
Requires (Dev)
- laravel/pint: ^1.10
- nunomaduro/collision: ^6.0|^7.0|^8.0|^9.0
- squizlabs/php_codesniffer: ^3.11
- tightenco/duster: ^3.1
Conflicts
- laravel/framework: <10.0
README
This package provides simple Table component to create Datatables for Livewire components
Requirements
- PHP 8.2 or higher
- Laravel 10.x or higher
- Livewire 3.x or higher
Install
Run composer require milenmk/laravel-simple-datatables
to install the package
Tailwind CSS
Publish the package css files by running php artisan vendor:publish --tag=laravel-simple-datatables-css
and add @SimpleDatatablesStyle
in the head
tag of your layout to include the package css
As an alternative, you can copy the content from /vendor/milenmk/laravel-simple-datatables/resources/css/package.css
to your app.css
file
For Tailwind 4.x
Add @source '../../vendor/milenmk/laravel-simple-datatables/resources/views/';
in your app.css
For Tailwind 3.x
Add '../../vendor/milenmk/laravel-simple-datatables/resources/views/'
inside content: []
of tailwind.config.js
Editing assets
If you want to edit the view files, run php artisan vendor:publish --tag="laravel-simple-datatables-views"
The view files are now available in /resources/views/vendor/laravel-simple-datatables
To make the classes in the package view files discovered when running npm run dev
оr npm run build
, add:
Do not forget to update the @source
directives in your app.css
Usage
Using a command
-
Run
php artisan make:milenmk-datatable PostList Post
wherePostList
is the name of the Livewire component that will be created andPost
is the name of the model. -
If you want to generate the columns for the model properties you add
--generate
at the end of the command -
The command will create a component in
App\Livewire
and a view file inresources/views/livewire
Manual
- In you Livewire component add the package trait
use HasTable;
- Define your table fields like:
public function table(Table $table): Table
{
return $table
->query(Menu::query())
->schema([
TextColumn::make('name')
->label('Name')
->searchable()
->sortable(),
TextColumn::make('route_name')
->label('Route')
->sortable(),
TextColumn::make('parent_menu_id')->label('Parent ID'),
TextColumn::make('type')->label('Type'),
TextColumn::make('icon')->label('Icon'),
TextColumn::make('position')
->label('Position')
->align('center')
->headerAlign('center'),
ToggleColumn::make('is_admin_menu')->label('Is Admin Menu'),
IconColumn::make('is_admin_menu')->boolean(),
])
->striped();
}
- In the view file of the Livewire component add
{{ $this->table }}
to render the table
Additional Information
- Available columns: TextColumn, ToggleColumn, CheckBoxColumn, ProgressColumn and IconColumn
- Available column options/settings:
- label (string|array|callable)
- value
- color (Full value e.g. text-gray-500, text-primary, text-danger etc.)
- background (Full value e.g. bg-gray-500, bg-danger)
- visible (true|false, default true)
- weight (font weight of the text (e.g., bold, thin, medium))
- wrap (true|false, default true)
- description (string|callable)
- align (left|center|right, default left)
- model (Specify custom value for the wire:model of the field. Default is the column key inside make())
DISCLAIMER
This package is provided ”as is”, without warranty of any kind, either express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, or noninfringement.
The author(s) make no representations or warranties regarding the accuracy, reliability or completeness of the code or its suitability for any specific use case. It is recommended that you thoroughly test this package in your environment before deploying it to production.
By using this package, you acknowledge and agree that the author(s) shall not be held liable for any damages, losses or other issues arising from the use of this software.
Contributing
You can review the source code, report bugs, or contribute to the project by visiting the GitHub repository:
Feel free to open issues or submit pull requests. Contributions are welcome!
License
This package is licensed under the MIT License. See the LICENSE file for more details.