详情字段显示扩展

这个功能用来扩展详情字段显示, 在内置的显示方法不满足需求的情况下,可以使用这个功能来实现

首先定义扩展类:

  1. <?php
  2. namespace App\Admin\Extensions\Show;
  3. use Dcat\Admin\Show\AbstractField;
  4. class UnSerialize extends AbstractField
  5. {
  6. // 这个属性设置为false则不会转义HTML代码
  7. public $escape = false;
  8. public function render($arg = '')
  9. {
  10. // 返回任意可被渲染的内容
  11. return unserialize($this->value);
  12. }
  13. }

然后在app/Admin/bootstrap.php中注册扩展类

  1. use Dcat\Admin\Show\Field;
  2. use App\Admin\Extensions\Show\UnSerialize;
  3. Field::extend('unserialize', UnSerialize::class);

然后在控制器中使用这个扩展

  1. $show->column()->unserialize('xxx');

传入unserialize()方法的参数会按顺序传入UnSerialize::render()方法中。

在父类Dcat\Admin\Show\AbstractField中可以看到几个常用的属性

  1. /**
  2. * Field value.
  3. *
  4. * @var mixed
  5. */
  6. protected $value;
  7. /**
  8. * Current field model.
  9. *
  10. * @var Fluent
  11. */
  12. protected $model;
  13. /**
  14. * If this field show with a border.
  15. *
  16. * @var bool
  17. */
  18. public $border = true;
  19. /**
  20. * If this field show escaped contents.
  21. *
  22. * @var bool
  23. */
  24. public $escape = true;

其中$value$model分别是当前字段值和当前详情内容的数据,在render()方法中可以用来获取你想要的数据。

$border用来控制当前显示内容是否需要外边框,$escape分别用来设置当前显示内容要不要HTML转义。