Field display

HTML

The html method allows you to insert a piece of HTML code without displaying label on the details page

  1. // pass in string
  2. $show->html('<br/>');
  3. // pass in view
  4. $show->html(view(...));
  5. // incoming closures
  6. $show->html(function () {
  7. // Get field information
  8. $id = $this->id;
  9. $username = $this->username;
  10. return view(..., ['id' => $id]);
  11. });

Dividers

If a separator line is to be added between fields.

  1. $show->divider();

Line feed

If you want to use line feeds between fields.

  1. $show->newline();

Modify display content

Modify the display using the following methods

  1. $show->title()->as(function ($title) {
  2. // Get other fields of the current row
  3. $username = $this->username;
  4. return "<{$title}> {$username}";
  5. });
  6. $show->contents()->as(function ($content) {
  7. return "<pre>{$content}</pre>";
  8. });

Ways to help

The help method is used in the same way as the data form field help method, see [Help Method] (model-grid-column.md#help).

Built-in Display Extension Methods

Here are a few common display styles that are built in as methods:

view

The view method can introduce a view file.

  1. // The following three variables are received in the template:
  2. // name field name
  3. // value field value
  4. // model Current row data
  5. $show->content->view('admin.fields.content');

explode

The explode method splits the string into arrays.

  1. $show->tag->explode()->label();
  2. // You can specify delimiter, default ",".
  3. $show->tag->explode('|')->label();

prepend

The prepend method is used to insert content in front of a value of type string or array.

  1. // When the value of a field is a string
  2. $show->email->prepend('mailto:');
  3. // When the value of a field is an array
  4. $show->arr->prepend('first item');

The prepend method allows passing parameters to closures

  1. $show->email->prepend(function ($value, $original) {
  2. // $value is the current field value
  3. // $original is the original value of the current field as queried from the database.
  4. // Get other field values
  5. $username = $this->username;
  6. return "[{$username}]";
  7. });

append

The append method is used to insert content after a value of type string or array.

  1. // When the value of a field is a string
  2. $show->email->append('@gmail.com');
  3. // When the value of a field is an array
  4. $show->arr->append('last item');

The append method allows passing parameters to closures

  1. $show->email->append(function ($value, $original) {
  2. // $value is the current field value
  3. // $original is the original value of the current field as queried from the database.
  4. // Get other field values
  5. $username = $this->username;
  6. return "[{$username}]";
  7. });

image

The content of the field avatar is the path or url of the image, which can be displayed as the image.

  1. $show->avatar()->image();

The parameters of the image() method refer to Field::image().

file

The content of the field document is the path or url of a file, which can be displayed as a file.

  1. $show->avatar()->file();

The arguments of the file() method refer to Field::file().

link

The content of the field homepage is the url link, which can be displayed as an HTML link.

  1. $show->homepage()->link();

The arguments of the link() method refer to Field::link()

label

Display the contents of the field tag as label.

  1. $show->tag()->label();

The arguments of the label() method refer to Field::label().

badge

Display the contents of the field rate as badge.

  1. $show->rate()->badge();

The arguments of the badge() method are referenced in Field::badge().

using

If the field gender takes the values f and m, it needs to be displayed as female and male respectively.

  1. $show->gender()->using(['f' => '女', 'm' => '男']);

dot

Column text can be preceded by a colored dot using the dot method.

  1. use Dcat\Admin\Admin;
  2. $show->state
  3. ->using([1 => 'Unprocessed', 2 => 'Processed', ...])
  4. ->dot(
  5. [
  6. 1 => 'primary',
  7. 2 => 'danger',
  8. 3 => 'success',
  9. 4 => Admin::color()->info(),
  10. ],
  11. 'primary' // The second parameter is the default value.
  12. );

Display file size

If the field data is a number of bytes representing the size of the file, you can display more readable text by calling the filezise method

  1. $show->field('file_size')->filesize();

This value 199812019 will show 190.56 MB.