FastAdmin 开关状态字段显示

项目中会存在很多的开关,比如广告表中的开启/关闭状态。FastAdmin 中也有相关设计,前提是字段符合符合 --switchsuffix 和字段类型为 tinyint(1) char(1) 且有默认值。我们项目更多是用 Laravel 的 migration 来维护业务表结构。对于 status 一般都是用 unsignedTinyInteger 来实现,意味我们不满足 FastAdmin 原本的设计,需要我们自行进行处理。

application\admin\command\Crud.php

1
2
3
4
5
6
7
8
9
10
protected function getFieldType(& $v)
{
...
// 状态开关
// COLUMN_NAME 是字段的名字,COLUMN_TYPE 是字段类型,如果不清楚具体的可以输出到日志
if($v['COLUMN_NAME'] === 'status' && $v['COLUMN_TYPE'] === 'tinyint(3) unsigned') {
$inputType = 'switch';
}
return $inputType;
}
1
2
3
4
5
6
7
protected function execute(Input $input, Output $output)
{
- if ($this->headingFilterField && $this->headingFilterField == $field && $itemArr) {
+ if ($this->headingFilterField && $this->headingFilterField == $field && $itemArr && $inputType !== 'switch') {
$headingHtml = $this->getReplacedStub('html/heading-html', ['field' => $field, 'fieldName' => Loader::parseName($field, 1, false)]);
}
}

创建 CURD

1
$ php think crud -t banner

效果

往上