bagisto-datagrid-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDataGrid Development
DataGrid 开发
An admin listing page is a subclass plus three lines of wiring. The
engine () owns paging, search, filtering, sorting,
saved filters and export; the subclass supplies a query and describes its
columns. There are 51 of them in the codebase — copy the closest one rather than
inventing a shape.
DataGridpackages/Webkul/DataGrid后台列表页由一个子类加上三行配置代码构成。核心引擎()负责分页、搜索、筛选、排序、保存筛选条件和导出功能;子类则提供查询语句并定义列信息。代码库中已有51个此类实现——建议参考最相似的实现,而非自行设计结构。
DataGridpackages/Webkul/DataGridThe four methods
四个核心方法
Webkul\DataGrid\DataGrid| Method | Required | Purpose |
|---|---|---|
| yes | Return a query builder, not a collection |
| yes | |
| no | Per-row actions, each ACL-gated |
| no | Checkbox actions, each ACL-gated |
Tunable properties, overridden only when the default is wrong:
(default ), , (), (10),
.
$primaryColumn'id'$sortColumn$sortOrder'desc'$itemsPerPage$perPageOptionsWebkul\DataGrid\DataGrid| 方法名 | 是否必填 | 作用 |
|---|---|---|
| 是 | 返回查询构建器,而非集合 |
| 是 | 为每个列调用 |
| 否 | 定义每行操作,每个操作需做ACL权限校验 |
| 否 | 定义复选框批量操作,每个操作需做ACL权限校验 |
可调整属性,仅在默认值不符合需求时覆盖:(默认值)、、(默认值)、(默认值10)、。
$primaryColumn'id'$sortColumn$sortOrder'desc'$itemsPerPage$perPageOptionsThe shape
代码结构示例
php
class CurrencyDataGrid extends DataGrid
{
/**
* Prepare query builder.
*
* @return Builder
*/
public function prepareQueryBuilder()
{
return DB::table('currencies')
->select('id', 'name', 'code');
}
/**
* Add Columns.
*
* @return void
*/
public function prepareColumns()
{
$this->addColumn([
'index' => 'name',
'label' => trans('admin::app.settings.currencies.index.datagrid.name'),
'type' => 'string',
'searchable' => true,
'filterable' => true,
'sortable' => true,
]);
}
/**
* Prepare actions.
*
* @return void
*/
public function prepareActions()
{
if (bouncer()->hasPermission('settings.currencies.edit')) {
$this->addAction([
'index' => 'edit',
'icon' => 'icon-edit',
'title' => trans('admin::app.settings.currencies.index.datagrid.edit'),
'method' => 'GET',
'url' => fn ($row) => route('admin.settings.currencies.edit', $row->id),
]);
}
}
}Align the inside an / array only if the file you are
editing already does; Pint does not enforce alignment either way, and the
codebase has both.
=>addColumnaddActionphp
class CurrencyDataGrid extends DataGrid
{
/**
* Prepare query builder.
*
* @return Builder
*/
public function prepareQueryBuilder()
{
return DB::table('currencies')
->select('id', 'name', 'code');
}
/**
* Add Columns.
*
* @return void
*/
public function prepareColumns()
{
$this->addColumn([
'index' => 'name',
'label' => trans('admin::app.settings.currencies.index.datagrid.name'),
'type' => 'string',
'searchable' => true,
'filterable' => true,
'sortable' => true,
]);
}
/**
* Prepare actions.
*
* @return void
*/
public function prepareActions()
{
if (bouncer()->hasPermission('settings.currencies.edit')) {
$this->addAction([
'index' => 'edit',
'icon' => 'icon-edit',
'title' => trans('admin::app.settings.currencies.index.datagrid.edit'),
'method' => 'GET',
'url' => fn ($row) => route('admin.settings.currencies.edit', $row->id),
]);
}
}
}仅当你正在编辑的文件已对齐/数组中的时,才保持对齐;Pint(Laravel代码规范工具)不强制要求对齐方式,代码库中两种格式都存在。
addColumnaddAction=>Wiring
关联配置
The controller serves JSON on an AJAX hit and the view otherwise — one route,
two responses:
php
public function index()
{
if (request()->ajax()) {
return datagrid(CurrencyDataGrid::class)->process();
}
return view('admin::settings.currencies.index');
}The Blade side is one tag pointing at that same route:
blade
<x-admin::datagrid :src="route('admin.settings.currencies.index')" />datagrid()InvalidDataGridExceptionDataGrid控制器在AJAX请求时返回JSON数据,否则返回视图——一个路由,两种响应:
php
public function index()
{
if (request()->ajax()) {
return datagrid(CurrencyDataGrid::class)->process();
}
return view('admin::settings.currencies.index');
}Blade视图侧只需一个标签指向同一路由:
blade
<x-admin::datagrid :src="route('admin.settings.currencies.index')" />除非类继承自,否则会抛出,因此类名是唯一的约定。
DataGriddatagrid()InvalidDataGridExceptionReference files
参考文档
| File | Load when |
|---|---|
| columns.md | Column types, search/filter/sort flags, dropdown options, closures, joins and |
| actions.md | Row actions, mass actions, ACL gating, export |
| 文件 | 适用场景 |
|---|---|
| columns.md | 列类型、搜索/筛选/排序标记、下拉选项、闭包、关联查询和 |
| actions.md | 行操作、批量操作、ACL权限校验、导出功能 |
Non-negotiables
必须遵守的规则
- returns a builder. Calling
prepareQueryBuilder(),->get()or mapping to a collection breaks paging, filtering and export, because the engine appends to the query you return.->paginate() - The query builder is the one place is expected. Everywhere else in Bagisto goes through a repository; a DataGrid is built on the query builder by design.
DB:: - Every action and mass action is wrapped in . An ungated action renders for admins who cannot perform it, and the grid is the most common place this is forgotten.
bouncer()->hasPermission(...) - Every goes through
label, with the key added to all 22 locales.trans() - A joined query needs for every aliased column — see columns.md. Without it, filtering and sorting on that column produce an ambiguous-column SQL error.
addFilter() - Escape whatever a closure interpolates. Cells render through . The engine strips tags from raw values first, but not quotes — so a value placed inside an attribute can break out. See columns.md.
v-html
REQUIRED SUB-SKILL: Use bagisto-change-verification before calling any change done.
- 必须返回查询构建器。 调用
prepareQueryBuilder()、->get()或转换为集合会破坏分页、筛选和导出功能,因为核心引擎会在你返回的查询基础上追加条件。->paginate() - 查询构建器是唯一允许使用的地方。 Bagisto的其他所有地方都通过仓库层操作;DataGrid在设计上就是基于查询构建器实现的。
DB:: - 每个行操作和批量操作都必须用包裹。 未做权限校验的操作会对无权限的管理员显示,而列表页是最容易遗漏权限校验的地方。
bouncer()->hasPermission(...) - 所有都必须通过
label进行翻译,且翻译键需添加到全部22种语言包中。trans() - 关联查询中每个别名列都需要调用——详见columns.md。如果不这么做,对该列进行筛选或排序会导致SQL歧义列错误。
addFilter() - 闭包中插入的任何内容都必须转义。 单元格通过渲染。核心引擎会先从原始值中剥离标签,但不会处理引号——因此插入到属性中的值可能会导致注入问题。详见columns.md。
v-html
必备子技能: 在完成任何修改前,使用bagisto-change-verification工具进行验证。