微信小程序开发之小程序架构篇的图解与分析
1029
2022-11-05
一个动态,响应式的Laravel Livewire表格组件,拥有搜索、排序、复选框和分页功能
Laravel Livewire Tables
A dynamic, responsive Laravel Livewire table component with searching, sorting, checkboxes, and pagination.
SupportContributionsBuy me a coffee
Installation
Make sure you've installed Laravel Livewire.
Installing this package via composer:
composer require kdion4891/laravel-livewire-tables
This package was designed to work well with Laravel frontend scaffolding.
If you're just doing scaffolding now, you'll need to add the Livewire @livewireScripts and @livewireStyles blade directives to your resources/views/layouts/app.blade.php file:
@livewireStyles...@livewireScripts
This package also uses Font Awesome for icons. If you don't already have it installed, it's as simple as:
npm install @fortawesome/fontawesome-free
Then add the following line to resources/sass/app.scss:
@import '~@fortawesome/fontawesome-free/css/all.min.css';
Now all that's left is to compile the assets:
npm install && npm run dev
Making Table Components
Using the make command:
php artisan make:table UserTable --model=User
This creates your new table component in the app/Http/Livewire folder.
After making a component, you may want to edit the query and column methods:
class UserTable extends TableComponent{ public function query() { return User::query(); } public function columns() { return [ Column::make('ID')->searchable()->sortable(), Column::make('Created At')->searchable()->sortable(), Column::make('Updated At')->searchable()->sortable(), ]; }}
You don't have to use the render() method in your table component or worry about a component view, because the package handles that automatically.
Using Table Components
You use table components in views just like any other Livewire component:
@livewire('user-table')
Now all you have to do is update your table component class!
Table Component Properties
$table_class
Sets the CSS class names to use on the
CSS class for the table header. $attribute The column attribute. Example: public function thClass($attribute){ if ($attribute == 'name') return 'font-italic'; if ($attribute == 'accidents_count') return 'text-right'; if ($attribute == 'brand.name') return 'font-weight-bold'; return null;} trClass($model) This method is used to compute the |
---|
CSS class for the table data. $attribute The column attribute. $value The column value. Example: public function tdClass($attribute, $value){ if ($attribute == 'name' && $value == 'Silverado') return 'table-secondary'; if ($attribute == 'accidents_count' && $value < 2) return 'table-success'; if ($attribute == 'brand.name' && $value == 'Ford') return 'table-primary'; return null;} mount() This method sets the initial table properties. If you have to override it, be sure to call $this->setTableProperties(). Example: public function mount(){ $this->setTableProperties(); // my custom code} render() This method renders the table component view. If you have to override it, be sure to return $this->tableView(). Example: public function render(){ // my custom code return $this->tableView();} Table Column Declaration The Column class is used to declare your table columns. public function columns(){ return [ Column::make('ID')->searchable()->sortable(), Column::make('Created At')->searchable()->sortable(), Column::make('Updated At')->searchable()->sortable(), ];} make($heading = null, $attribute = null) $heading The heading to use for the table column, e.g. Created At. Can be null for view-only columns. $attribute The attribute to use for the table column value. If null, it will use a snake cased $heading. You can also specify _counts and relationship attributes with a dot notation. For counts, let's say I added withCount() to my query(): public function query(){ return Car::withCount('accidents');} Now I can create a column using this count like so: Column::make('Accidents', 'accidents_count')->sortable(), For relationships, let's say I added with() to my query(): public function query(){ return Car::with('brand');} Now I can create a column using any of the relationship attributes like so: Column::make('Brand ID', 'brand.id')->searchable()->sortable(),Column::make('Brand Name', 'brand.name')->searchable()->sortable(), searchable() Sets the column to be searchable. sortable() Sets the column to be sortable. sortUsing($callback) Allows custom logic to be used for sorting. Your supplied callable will receive the following parameters: $models: The current Eloquent query (\Illuminate\Database\Eloquent\Builder). You should apply your sort logic to this query, and return it.$sort_attribute: The name of the column currently being sorted. If you used a nested relationship for sorting, it will be properly transformed to relationship_table.column_name format so the query will be scoped correctly.$sort_direction: The direction sort direction requested, either asc, or desc. Additionally, your callback will be passed through Laravel's Container so you may inject any dependencies you need in your callback. Make sure your dependencies are listed before the parameters above. Example: Column::make('Paint Color')->searchable()->sortable()->sortUsing(function ($models, $sort_attribute, $sort_direction) { return $models->orderByRaw('?->\'$.color_code\' ?', [$sort_attribute, $sort_direction]);}); This will sort the paint_color column using the JSON value color_code. SQL Injection warning: Make sure if you are using any of Eloquent's *Raw methods, you always use the bindings feature. view($view) Sets a custom view to use for the column. Example: Column::make('Paint Color')->searchable()->sortable()->view('cars.table-paint-color'), Notice how the column is still searchable() and sortable(), because the Car model contains a paint_color attribute! If you're making a view-only column (for action buttons, etc), just don't make it searchable or sortable: Column::make()->view('cars.table-actions'), Custom column views are passed $model and $column objects, as well as variables passed from the table component. For the Paint Color example, we can use the paint_color attribute from the model like so: {{-- resources/views/cars/table-paint-color.blade.php --}} For the action buttons example, we can use the id attribute from the model like so: {{-- resources/views/cars/table-actions.blade.php --}} Using a custom view for a relationship column? No problem: {{-- resources/views/cars/table-brand-name.blade.php --}}{{ $model->brand->name }} Publishing Files Publishing files is optional. Publishing the table view files: php artisan vendor:publish --tag=table-views Publishing the config file: php artisan vendor:publish --tag=table-config 版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。 发表评论 |
暂时没有评论,来抢沙发吧~