<?php
namespace app\model;
use think\db\Query;
use tpScriptVueCurd\base\model\BaseModel;
use tpScriptVueCurd\field\RadioField;
use tpScriptVueCurd\field\StringField;
use tpScriptVueCurd\FieldCollection;
use tpScriptVueCurd\ModelField;
class User extends BaseModel
{
/**
* 表字段配置
* @return FieldCollection
*/
public function fields(): FieldCollection
{
$nameField=StringField::init('name','姓名');
$nameField->pushFieldDo()->setEditShowDo(function (BaseModel &$info,?BaseModel $base,ModelField $field,bool $isStepNext){
if(empty($info->id)){//如果是添加数据,设置姓名默认为 张三
$info[$field->name()]='张三';
}
});
$sexField=RadioField::init('sex', '性别')->items(['男','女']);
$sexField->pushFieldDo()->setIndexFilterBeforeDo(function (ModelField $field,Query $query,array &$filterData){
if(empty($info->id)){//如果是添加数据,设置性别默认为 男
$info[$field->name()]='男';
}
});
return new FieldCollection([
$nameField,
$sexField,
]);
}
}