QQ扫一扫联系
在一些基础的场合,Grid、Form、Detail 具有高度的相似,可以合并使用一个快速的 CRUD 工具。
先来个例子,数据库中有 news
表
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`cover` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`summary` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`content` text,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
下面的代码可以生成表 news
的数据表格
<?php
namespace App\Admin\Controller;
use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminQuickCRUD;
use ModStart\Admin\Layout\AdminCRUDBuilder;
class NewsController extends Controller
{
use HasAdminQuickCRUD;
protected function crud(AdminCRUDBuilder $builder)
{
$builder
->init('news')
->field(function ($builder) {
$builder->id('id','ID');
$builder->text('title', '名称');
$builder->image('cover', '封面');
$builder->textarea('summary', '摘要');
$builder->richHtml('content', '内容');
$builder->display('created_at', '创建时间');
$builder->display('updated_at', '更新时间');
})
->title('新闻管理');
}
}
使用数据表名称创建数据表格
$builder
->init('news')
->field(function ($builder) {
// ...
})
->title('新闻管理');
使用数据模型类来创建数据表格
$builder
->init(News::class)
->field(function ($builder) {
// ...
})
->title('新闻管理');
其中 News
定义如下
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
protected $table = 'news';
}
$builder->areaChina('field', '类型');
$builder->audio('field', '类型');
$builder->captcha('field', '类型');
$builder->checkbox('field', '名称');
$builder->code('field', '名称');
$builder->color('field', '类型');
$builder->currency('field', '类型');
$builder->date('field', '类型');
$builder->datetime('field', '类型');
$builder->decimal('field', '类型');
$builder->display('field','名称');
$builder->file('field', '类型');
$builder->fileTemp('field', '类型');
$builder->hidden('field', '类型');
$builder->html('field', '类型');
$builder->icon('field', '类型');
$builder->id('field', '类型');
$builder->image('field', '类型');
$builder->type('field', '类型');
$builder->type('field', '类型');
字段存储的是 JSON 格式的 ID 列表,通过 ID 列表获取对应的条目名称
$grid->jsonIdItems('field', '字段名');
// 条目选择接口
->selectUrl(modstart_admin_url('path/to/select'))
// 指定预览接口
->previewUrl(modstart_admin_url('path/to/preview'))
// 指定条目样式,ITEM_STYLE_TITLE、ITEM_STYLE_COVER_TITLE
->itemStyle(JsonIdItems::ITEM_STYLE_TITLE);
条目选择接口需要返回
{
"code": 0,
"data": {
"records": [
{"id": 1, "title": "选项1", "cover": "http://xxx.com/xxx.jpg"},
{"id": 2, "title": "选项2", "cover": "http://xxx.com/xxx.jpg"}
]
}
}
条目预览接口接收 ids
参数,该参数使用 ,
分隔的多个ID,返回的数据格式如下
{
"code": 0,
"data": {
"records": [
{"id": 1, "title": "选项1", "cover": "http://xxx.com/xxx.jpg"},
{"id": 2, "title": "选项2", "cover": "http://xxx.com/xxx.jpg"}
]
}
}
$builder->keyValueList('field', '类型');
$builder->link('field', '类型');
$builder->markdown('field', '类型');
$builder->multiSelect('field', '字段名')->options([
'a' => '选项1',
'b' => '选项2',
'c' => '选项3',
]);
$builder->number('field', '类型');
$builder->password('field', '类型');
$builder->percent('field', '类型');
$builder->radio('field', '类型');
$builder->richHtml('field', '类型');
$field = $builder->select('field', '类型');
// 使用Type作为备选项,XxxType是继承BaseType的类
$field->optionType(XxxType::class);
// 使用数组作为备选项
$field->options(['1' => '选项1', '2' => '选项2']);
// 使用数组作为备选项
$field->optionArray([['id' => 1, 'name' => '选项1'], ['id' => 2, 'name' => '选项2']],'id','name');
// 使用模型作为备选项
$field->optionModel('forum_category','id','title');
// 使用模型作为备选项(简单条件筛选)
$field->optionModel('forum_category','id','title',['xxx'=>'xxx']);
// 使用模型作为备选项,并渲染为树状结构
$field->optionModelTree('forum_category','id','title');
$builder->selectRemote('seriesId', '剧集')
// 指定远程数据源
->server(modstart_admin_url('path/to/select_remote'))
// 显示时查找的数据字段
->viewWithModel(XxxModel::class,'id','title');
远程数据源接口需要返回
{
"code": 0,
"data": {
"options": [
{"value": 1, "label": "选项1"},
{"value": 2, "label": "选项2"}
]
}
}
系统封装好了参数可以直接试用,远程数据源可以直接调用以下方法返回
class XxxController extends Controller
{
public function selectRemote()
{
return SelectRemote::handleModel(XxxModel::class,'id','title');
}
}
$builder->switch('field', '类型');
$builder->tags('field', '名称');
$builder->text('field', '名称');
$builder->textarea('field', '类型');
$builder->time('field', '类型');
$builder->tree('field', '名称');
$builder->type('field', '类型');
$builder->values('field', '类型');
$builder->video('field', '类型');
更多内置组件请参照 ModStart\Support\Manager\FieldManager
中的定义