PetUserLazyRenderable.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Module\Pet\AdminControllers\LazyRenderable;
  3. use App\Module\Pet\Models\PetUser;
  4. use App\Module\Pet\Enums\PetStatus;
  5. use App\Module\Pet\Repositorys\PetUserRepository;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Grid\LazyRenderable;
  8. class PetUserLazyRenderable extends LazyRenderable
  9. {
  10. /**
  11. * 获取模型ID字段
  12. *
  13. * @return string
  14. */
  15. public function getModelSelectId()
  16. {
  17. return 'id';
  18. }
  19. /**
  20. * 获取模型显示字段
  21. *
  22. * @return string
  23. */
  24. public function getModelViewName()
  25. {
  26. return 'name';
  27. }
  28. /**
  29. * 获取模型类
  30. *
  31. * @return string
  32. */
  33. public function getModel()
  34. {
  35. return PetUser::class;
  36. }
  37. /**
  38. * 渲染表格
  39. *
  40. * @return Grid
  41. */
  42. public function grid(): Grid
  43. {
  44. return Grid::make(new PetUserRepository(), function (Grid $grid) {
  45. $grid->column('id', 'ID')->sortable();
  46. $grid->column('name', '宠物名称');
  47. $grid->column('level', '等级');
  48. $grid->column('status', '状态')->display(function ($value) {
  49. if ($value instanceof PetStatus) {
  50. $value = $value->value;
  51. }
  52. $labels = [
  53. PetStatus::NONE->value => '未知',
  54. PetStatus::NORMAL->value => '正常',
  55. PetStatus::DEAD->value => '死亡',
  56. PetStatus::FEEDING->value => '喂养中',
  57. PetStatus::TRAINING->value => '训练中',
  58. PetStatus::RESTING->value => '休息中',
  59. PetStatus::TRAVELING->value => '外出中',
  60. PetStatus::STEALING->value => '偷菜中',
  61. ];
  62. return $labels[$value] ?? '未知';
  63. });
  64. $grid->quickSearch(['id', 'name']);
  65. $grid->filter(function (Grid\Filter $filter) {
  66. $filter->equal('id', 'ID');
  67. $filter->like('name', '宠物名称');
  68. $filter->equal('status', '状态')->select([
  69. PetStatus::NONE->value => '未知',
  70. PetStatus::NORMAL->value => '正常',
  71. PetStatus::DEAD->value => '死亡',
  72. PetStatus::FEEDING->value => '喂养中',
  73. PetStatus::TRAINING->value => '训练中',
  74. PetStatus::RESTING->value => '休息中',
  75. PetStatus::TRAVELING->value => '外出中',
  76. ]);
  77. });
  78. $grid->disablePagination(true);
  79. $grid->disableActions();
  80. $grid->disableBatchActions();
  81. $grid->disableCreateButton();
  82. });
  83. }
  84. }