joydeep-bhowmik / livewire-datatable
一个用于Laravel应用的laravel livewire datatable包
v1.0.4
2024-08-17 07:53 UTC
Requires
- php: ^8.1
- laravel/framework: ^11.0
- livewire/livewire: ^3.0
README
Laravel Livewire Datatable是一个使用Livewire库的动态datatable组件,适用于Laravel应用。它允许你创建具有排序、过滤、分页和数据导出等功能的交互式数据表。
要求
- Livewire版本3.0或更高
- Laravel版本10或更高
- Alpine js v3
安装
要开始使用Laravel Livewire Datatable,请按照以下步骤操作
- 使用Composer安装此包
composer require joydeep-bhowmik/livewire-datatable
- 在您的/bootstrap/providers.php中添加以下服务提供者(Laravel 11 +)
// datatable service provider JoydeepBhowmik\LivewireDatatable\Providers\DataTableServiceProvider::class,
- 发布样式表
php artisan vendor:publish --tag=joydeep-bhowmik-livewire-datatable-css
- 使用样式表
<link rel="stylesheet" href="{{ asset('joydeep-bhowmik/livewire-datatable/css/data-table.css') }}">
用法
<?php namespace App\Livewire; use App\Models\User; use Illuminate\Support\Carbon; use JoydeepBhowmik\LivewireDatatable\Datatable; class Table extends Datatable { public $model = User::class; public function table() { //table method must return an array return [ // id field $this->field('id') ->label('Id') ->sortable(), /* you can also run custom query for sort ->sortable(function($query,$direction){ $query->orderBy('id', $direction); }) */ // email field $this->field('email') ->label('Email') ->searchable(), /* you can also run custom query for search like ->searchable(function($query,$keyword){ $query->where('email', 'like', '%' . $keyword . '%') }) */ // created at $this->field('created_at') ->label('Created At') ->value(function ($row) { return Carbon::createFromTimeStamp(strtotime($row->created_at))->diffForHumans(); }) /* You can also give it a text value like ->value('ok') */ ->sortable(), ]; } }
<livewire:table />
自定义查询
有时我们需要在使用如join等操作时在数据表中使用自定义查询。
class Table extends Datatable { //dont use model when using builder //public $model = Product::class; public function builder() { return Product::leftJoin('stocks', 'stocks.product_id', 'products.id'); } }
在使用join时,您需要像这样修改您的字段
public function table() { return [ $this->field('id') ->label('Id') ->table('products') ->as('product_id')// this is optional add according to your query ->sortable() ->searchable() , $this->field('id') ->label('Stock Id') ->table('stocks') ->as('stock_id')// this is optional add according to your query ->sortable() ->searchable() //add more here ]; }
过滤器
// Example: Define filters public function filters() { return [ //input type select $this->filter('visibility') ->label('Visibility') //this options are required for input type select ->options([ '' => 'Select', 'public' => 'Public', 'private' => 'private', ]) ->type('select') ->query(function ($query, $value) { $query->where('products.id',$value); }), $this->filter('stock_id') ->label('Stock id') ->type('text') ->placeholder('Enter text id') ->query(function ($query, $value) { $query->where('products.id', $value); }) /* ->value('some text') //optional */, $this->filter('stock') ->label('In stock') ->type('checkbox') ->query(function ($query, $value) { $query->where('products.id', $value); }), //add other filters ]; }
批量操作
有时我们需要删除某些选定的数据行或对它们执行批量操作。
public $checkbox = true; public $primaryKey = "id"; public function delete(){ foreach($this->ids as $id){ $product=Product::find($id); $product->delete(); } } public function bulkActions() { return [ $this->button('delete') ->text('Delete') ->action('delete')//this is a public method of your component ->confirm('Are u sure?')// optional, ]; }
隐藏头部
如果您需要隐藏所有的搜索、过滤器等按钮,只需在组件类中添加一个公共属性 $headers=false 即可。
class Table extends Datatable { public $headers=false; }