| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Module\Farm\AdminControllers\LazyRenderable;
- use App\Module\Farm\Enums\SEED_TYPE;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Repositories\FarmSeedRepository;
- use Dcat\Admin\Grid;
- use UCore\DcatAdmin\Grid\SelectTable;
- /**
- * 种子选择表格渲染类
- */
- class FarmSeedLazyRenderable extends \UCore\DcatAdmin\Grid\LazyRenderable implements SelectTable
- {
- /**
- * 仓库类
- *
- * @var string
- */
- static $repository = FarmSeedRepository::class;
- /**
- * 渲染表格
- *
- * @return Grid
- */
- public function grid(): Grid
- {
- // 获取外部传递的参数
- $type = $this->type;
- return Grid::make(new self::$repository, function (Grid $grid) use ($type) {
- if ($type) {
- $grid->model()->where('type', $type);
- }
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '种子名称');
- $grid->column('type', '种子类型')->display(function ($value) {
- return "<span class='badge badge-info'>" . SEED_TYPE::getName($value) . "</span>";
- });
- $grid->column('item_id', '产出物品')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- try {
- $item = \App\Module\GameItems\Models\Item::find($value);
- if ($item) {
- return "<span class='badge badge-success'>{$item->name}</span> <small>ID: {$item->id}</small>";
- }
- } catch (\Exception $e) {
- // 忽略异常
- }
- return $value;
- });
- $grid->column('min_output', '产出范围')->display(function () {
- return "{$this->min_output} - {$this->max_output}";
- });
- $grid->column('created_at', '创建时间')->sortable();
- $grid->quickSearch(['id', 'name']);
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->like('name', '种子名称');
- $filter->equal('type', '种子类型')->select(SEED_TYPE::getAll());
- $filter->equal('item_id', '产出物品ID');
- $filter->between('min_output', '最小产出');
- $filter->between('max_output', '最大产出');
- });
- $grid->paginate(10);
- $grid->disableActions();
- });
- }
- /**
- * 获取模型类名
- *
- * @return string
- */
- public function getModel(): string
- {
- return FarmSeed::class;
- }
- /**
- * 获取模型选择ID字段
- *
- * @return string
- */
- public function getModelSelectId(): string
- {
- return 'id';
- }
- /**
- * 获取模型显示名称字段
- *
- * @return string
- */
- public function getModelViewName(): string
- {
- return 'name';
- }
- }
|