dongrim/datatable-inertia
Composer 安装命令:
composer require dongrim/datatable-inertia
包简介
Easy build datatable for InertiaJS on Laravel
README 文档
README
This package provides a DataTables-like experience for Inertia.js with support for searching, filtering, sorting and pagination.
Laravel compatibility
| Laravel | datatable-inertia |
|---|---|
| 6.0-9.x | 0.0.x |
Installation
Install the package via composer.
composer require dongrim/datatable-inertia
Config Files
In order to edit the default configuration you may execute:
php artisan vendor:publish --provider="Dongrim\DatatableInertia\DatatableInertiaServiceProvider"
Usage
php artisan datatable:make SomeDatatable
By default, the command generates the SomeDatatable class in the \App\Datatables directory
If you want to change the destination path of a class, you can use one of these methods:
- Specify a new path in the file /config/datatables.php
'basePath' => '\App\Datatables'
- Run command
php artisan datatable:makewithout specifying the name of the generated class and answer questions.
To generate data, a table macro has been created for Inertia\Response.
You can take advantage of dependency injection
public function index(PostDatatable $datatable) { return Inertia::render('Post/Index')->table($datatable); }
Or just give the classpath
public function index() { return Inertia::render('Post/Index')->table(PostDatatable::class); // '\App\Datatables\PostDatatable' }
If you need to pass additional data, use the usual method of passing data in InertiaJs
public function index(PostDatatable $datatable) { return Inertia::render('Post/Index', ['data' => 'some data'])->table($datatable); }
By default datatable class is generated in minimal configuration
For example
namespace \App\Datatables; use Illuminate\Database\Eloquent\Builder; use Dongrim\DatatableInertia\DatatableInertia; class PostDatatable extends DatatableInertia { /** * Eloquent datatable query builder * * @return Builder */ public function query(): Builder { // code } }
Public properties available in the class:
| Properties | Type | Default | Description |
|---|---|---|---|
datatableName |
string | 'datatable' | The name of the object containing all returned data from datatable |
perPageKey |
string | 'per_page' | The key in the request responsible for changing the number of displayed elements on the page (only when rendering on the client side) |
itemsPerPage |
int | 15 | Parameter responsible for the number of displayed elements on the page by default |
serverSide |
bool | false | Parameter responsible for the server or client side rendering |
Note that these options are set globally in the config/datatables.php configuration file.
You can override them directly in you the class
Public methods available in the class:
| Method | Response type | Required | Description |
|---|---|---|---|
query |
\Illuminate\Database\Eloquent\Builder |
required |
Creates prepared Eloquent query builder |
modify |
\Illuminate\Database\Eloquent\Model |
optional |
Changing the value of returned fields |
columns |
array |
optional |
If method columns not implemented in the derived class, will be returned all fillable fields |
guard |
array |
optional |
Adding access rights to a specific entry to change, delete, etc. |
filters |
array |
optional |
Adding filters and their values (server-side rendering only) |
For ExampleDatatable:
namespace App\Datatables; use App\Models\Post; use Illuminate\Support\Facades\Auth; use Illuminate\Database\Eloquent\Builder; use Dongrim\DatatableInertia\DatatableInertia; class PostDatatable extends DatatableInertia { public $datatableName = 'post_datatable'; public $perPageKey = 'post_per_page'; public $itemsPerPage = 25; public $serverSide = true; public function query(): Builder { return Post::select('posts.*', 'users.username as author_name') ->join('users', function ($join) { $join->on('users.id', '=', 'posts.author_id'); }) ->when(request()->search, function ($query, $search) { return $query->where('title', 'like', "{$search}%") ->orWhere('text', 'like', "{$search}%") ->orWhere('users.username', 'like', "{$search}%"); }) ->when(request()->sort, function ($query, $sort) { $query->withoutGlobalScopes(); $table = ($sort == 'id') ? "posts." : ""; $query->orderBy($table . $sort, request()->order ?? 'asc'); }) ->when(request()->active, function ($query, $active) { $active = $active === 'true' ? true : false; $query->where('posts.active', $active); }); } public function columns(): array { return ['id', 'position', 'active', 'title', 'text', 'author_name']; } public function filters(): array { return ['sort', 'order', 'search', 'active']; } public function guard($data): array { return [ 'edit' => Auth::user()->can('post.edit') ? route('post.edit', $data->id) : null, 'destroy' => Auth::user()->can('post.destroy') ? route('post.destroy', $data->id) : null, 'restore' => Auth::user()->can('post.restore') ? route('post.restore', $data->id) : null, ]; } public function modify($data) { $data->text = str($data->text)->substr(0, 100); return $data; } }
dongrim/datatable-inertia 适用场景与选型建议
dongrim/datatable-inertia 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「datatable」 「inertia」 「inertiajs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dongrim/datatable-inertia 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dongrim/datatable-inertia 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dongrim/datatable-inertia 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.
Plug-ins for DataTables
Data Table will allow you to easily create Listing, Searching, Sorting and Download CSV.
crud widgets for laravel, to make an admin in few lines of code
Laravel filter elequent
Laravel filter elequent
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-28