详情字段显示扩展

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

首先定义扩展类:

  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中注册扩展类

<?php

use Dcat\Admin\Show\Field;
use App\Admin\Extensions\Show\UnSerialize;

Field::extend('unserialize', UnSerialize::class);

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

<?php

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

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

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

<?php

/**
 * Field value.
 *
 * @var mixed
 */
protected $value;

/**
 * Current field model.
 *
 * @var Fluent
 */
protected $model;

/**
 * If this field show with a border.
 *
 * @var bool
 */
public $border = true;

/**
 * If this field show escaped contents.
 *
 * @var bool
 */
public $escape = true;

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

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