savannabits / laravel-pagetables
Laravel Pagetables 是一个使用默认 Laravel 分页的 Datatables 服务器端适配器。
v1.1.0
2022-06-13 06:16 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-13 11:29:21 UTC
README
生成支持搜索、排序、每页数量和页面查询的 Laravel 分页。Datatables 支持通过子/父关系进行搜索和排序,例如 user.role.name。
如果您使用 Vue.js,我们建议使用 Pagetables 组件,该组件渲染样式优美的 datatables,并支持本包的 payload。
安装
您可以通过 composer 安装此包。
composer require savannabits/laravel-pagetables
用法
在底层,此包使用 Laravel 分页并返回一个 LengthAwarePaginator 对象以及指定的列。该包有一个 Column 类,它有助于构建带有名称和其他属性(为了前端渲染和格式化)的列。使用此包的三个简单步骤
- 指定列作为 Savannabits\Pagetables\Column[] 数组。
- 从查询中创建 datatables,传入步骤 1 中的列数组。
- 从前端控制器方法中查询,使用参数如
search、sort、per_page、sort_direction和page
示例 在控制器中
use Savannabits\Pagetables\Column; use Savannabits\Pagetables\Pagetables; use App\Models\Article; $columns = [ Column::name("id")->title("ID")->sortDesc()->searchable(), Column::name("slug")->title("Slug")->sort()->searchable(), Column::name("name")->title("Name")->sort()->searchable(), Column::name("weighted_cost")->title("Weighted Cost")->sort()->searchable(), Column::name("itemGroup.name")->title("Group")->sort()->searchable(), Column::name("actions")->title("")->raw(true), ]; $dt = Pagetables::of(Article::where("enabled", "=",true)) ->columns($columns) ->make(true); return response()->json($dt);
客户端的 API 请求
GET https://myserver.test/api/articles?search=&per_page=10&sort=weighted_cost&sort_direction=asc&page=1
响应 JSON
{
"current_page": 1,
"data": [
{
"id": 1949,
"slug": "mocha-frappe-500-ml-pa",
"name": "MOCHA FRAPPE 500 ML (PA)",
"description": "Created while importing Recipe .MOCHA FRAPPE 500 ML from Micros by smaosa at 2019-12-19 16:52:15",
"article_type_id": 1,
"item_group_id": 1,
"default_depot_id": 44,
"derived_unit_id": 22,
"last_purchase_price": 0,
"last_ordered_quantity": 0,
"last_order_time": null,
"lifespan_days": 2,
"is_product": true,
"last_received_quantity": 0,
"last_receiving_price": 0,
"last_received_at": null,
"weighted_cost": 0,
"enabled": true,
"created_at": "2019-12-19 16:52:15",
"updated_at": "2020-08-31 20:41:19",
"is_consumable": 0,
"api_route": "https://myserver.test/api/articles",
"item_group": {
"id": 1,
"slug": "bakery-products",
"name": "Bakery Products",
"description": "Assigned to Profit Contribution. Imported at 2019-12-19 13:59:43 by smaosa",
"enabled": true,
"major_group_id": 6,
"created_at": "2019-12-19 13:59:43",
"updated_at": "2020-08-31 20:41:18",
"api_route": "https://myserver.test/api/item-groups"
}
},
{"id": 1950 },
{"id": 1951 }
],
"first_page_url": "https://myserver.test/api/articles?page=1",
"from": 1,
"last_page": 189,
"last_page_url": "https://myserver.test/api/articles?page=189",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "https://myserver.test/api/articles?page=1",
"label": 1,
"active": true
},
{
"url": "https://myserver.test/api/articles?page=2",
"label": 2,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=3",
"label": 3,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=4",
"label": 4,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=5",
"label": 5,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=6",
"label": 6,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=7",
"label": 7,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=8",
"label": 8,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=9",
"label": 9,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=10",
"label": 10,
"active": false
},
{
"url": null,
"label": "...",
"active": false
},
{
"url": "https://myserver.test/api/articles?page=188",
"label": 188,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=189",
"label": 189,
"active": false
},
{
"url": "https://myserver.test/api/articles?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "https://myserver.test/api/articles?page=2",
"path": "https://myserver.test/api/articles",
"per_page": "10",
"prev_page_url": null,
"to": 10,
"total": 1884,
"columns": [
{
"title": "ID",
"name": "id",
"raw": false,
"sortable": true,
"searchable": true,
"sort_key": "id",
"search_key": "id",
"sort_direction": "desc"
},
{
"title": "Slug",
"name": "slug",
"raw": false,
"sortable": true,
"searchable": true,
"sort_key": "slug",
"search_key": "slug",
"sort_direction": "asc"
},
{
"title": "Name",
"name": "name",
"raw": false,
"sortable": true,
"searchable": true,
"sort_key": "name",
"search_key": "name",
"sort_direction": "asc"
},
{
"title": "Weighted Cost",
"name": "weighted_cost",
"raw": false,
"sortable": true,
"searchable": true,
"sort_key": "weighted_cost",
"search_key": "weighted_cost",
"sort_direction": "asc"
},
{
"title": "Group",
"name": "itemGroup.name",
"raw": false,
"sortable": true,
"searchable": true,
"sort_key": "itemGroup.name",
"search_key": "itemGroup.name",
"sort_direction": "asc"
},
{
"title": "",
"name": "actions",
"raw": true,
"sortable": false,
"searchable": false,
"sort_key": "actions",
"search_key": "actions",
"sort_direction": "asc"
}
]
}
从这里,您如何使用 payload 来渲染 datatables 完全取决于您自己。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞,请参阅 我们的安全策略。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。