Grid Column
By default, the column shows the most primitive data in the database. If you need to modify the display of the column data, refer to the following method.
Display callback
The display()
method is used to process the value of the current column via the passed callback function:
$grid->column('title')->display(function ($title) {
Return "<span style='color:blue'>$title</span>";
});
In the incoming anonymous function, the data can be processed in any way. In addition, the anonymous function binds the data of the current column as a parent object, and can call the data of the current row in the function:
$grid->column('first_name');
$grid->column('last_name');
// `full_name` field that does not exist
$grid->column('full_name')->display(function () {
Return $this->first_name . ' ' . $this->last_name;
});
Display different components according to conditions
If this column is to be displayed as a different component based on certain criteria
$grid->column('title')->display(function ($title, $column) {
/ / If the value of the status field of this column is equal to 1, directly display the title field
If ($this->status == 1) {
Return $title;
}
// Otherwise it is displayed as editable
Return $column->editable();
});
Content Mapping
If the value of the field gender
is f
, m
, it needs to be displayed with female
and male
respectively.
$grid->column('gender')->using(['f' => 'female', 'm' => 'male']);
Content replacement
If you need to replace some of the values of this column with other content to display:
$grid->column('cost')->replace([0 => '-']);
Column view
Use the view()
method to make the current column render a view display output, such as having a view resources/views/content.blade.php
<p>
{{ $value }}
{{ $model->id }}
</p>
By default, two variables of the view are passed in, $model
is the model of the current row, and $value
is the value of the current column.
Then use the following call to render this view output
$grid->column('content')->view('content');
This method can be used to render complex column content.
Column expansion
If there are more fields in a row, you can hide too much content by using the column expansion feature. Expand the display by clicking on the column, or click to expand other related data, such as the following example, to expand the 10 latest comments under an article:
Use Ezadev\Admin\Widgets\Table;
$grid->column('title', 'title')->expand(function ($model) {
$comments = $model->comments()->take(10)->map(function ($comment) {
Return $comment->only(['id', 'content', 'created_at']);
});
Return new Table(['ID', 'content', 'release time'], $comments->toArray());
});
Any content that can be rendered can be returned in the closure function.
Popup modal box
Similar to the column expansion
function, you can display more content by popping up the modal box.
$grid->column('title', 'title')->modal('latest comment', function ($model) {
$comments = $model->comments()->take(10)->get()->map(function ($comment) {
Return $comment->only(['id', 'content', 'created_at']);
});
Return new Table(['ID', 'content', 'release time'], $comments->toArray());
});
Gavatar
If this column of data is a email, you want to display it as a Gavatar:
$grid->column('email', 'avatar')->gravatar();
// set the size
$grid->column('email', 'avatar')->gravatar(45);
File size
If the data in this column is the number of bytes representing the file size, you can display the more readable text by calling the filezise
method.
$grid->column('file_size')->filesize();
Such a value 199812019
will be displayed as 190.56 MB
Download link
If the data in this column stores the path to the uploaded file, you can set this column as a downloadable link by calling the downloadable
method.
$grid->column('file_path')->downloadable();
Copy button
With the following call, a copy icon will appear in front of each line of text in this column, click on it to copy its value
$grid->column('title')->copyable();
QR code
Through the following call, a QR code icon will appear in front of each line of text in this column. Click on it to expand a small bullet box, which will display the QR code of this column value.
$grid->column('link')->qrcode();
Display image
If the picture
field holds the full address of the picture, or the path, the column can be rendered as a picture display in the following way.
Multi-graph display is supported, and field output is required as an array.
$grid->column('picture')->image();
/ / Set the server and width
$grid->column('picture')->image('http://xxx.com', 100, 100);
// show multiple images
$grid->column('pictures')->display(function ($pictures) {
Return json_decode($pictures, true);
})->image('http://xxx.com', 100, 100);
Display label
Display the field as a label
tag. If the field is output as an array, it will appear as multiple label
tags.
$grid->column('name')->label();
/ / Set the color, the default `success`, optional `danger`, `warning`, `info`, `primary`, `default`, `success`
$grid->column('name')->label('danger');
// receive array
$grid->column('keywords')->label();
If you need to display the different values of the status
field as labels of different colors
$grid->column('status')->label([
1 => 'default',
2 => 'warning',
3 => 'success',
4 => 'info',
]);
Show icon
Display the field as a font-awesome
icon, more icons refer to http://fontawesome.io/icons/
$grid->column('status')->icon([
0 => 'toggle-off',
1 => 'toggle-on',
], $default = '');
link
Display the field as a link.
// When the link method does not pass parameters, the linked `href` and `text` are the values of the current column.
$grid->column('homepage')->link();
// or pass in a specified href
$grid->column('homepage')->link($href);
Table
Display the field as a table, requiring the value of the current column to be a two-dimensional array
/ / table method does not pass parameters, the title of the table is the key of each column of the two-dimensional array
$grid->column('settings')->table();
// You can specify the key for each column by the following method
$grid->column('settings')->table(['key' => 'key', 'val' => 'value']);
Progress bar
Display the field as a progress bar, the value of the current column needs to be a value, the default maximum value is 100,
$grid->column('progress')->progressBar();
// optional parameters
$grid->column('progress')->progressBar($style = 'primary', $size = 'sm', $max = 100);
$style
is used to set the style, optional values danger
, warning
, info
, primary
, default
, success
$size
is used to set the size. The optional values are sm
, xs
, xxs
, $max
to set the maximum range.
Loading status
$grid->column('status')->loading([1, 2, 3]);
If the value of status is one of [1, 2, 3]
, it will be displayed as a loading icon.
Show other field values displayed
$grid->column('status')->loading([1, 2, 3], [
4 => 'Complete'
]);
Image Carousel
If the field value is an image array, you can use the following call to display the image carousel component.
$grid->column('images')->carousel();
/ / Set the display size and image server
$grid->column('images')->carousel($width = 300, int $height = 200, $server);
Date format
If the field value is a timestamp, you can format the output with the date
method.
$grid->column('created_at')->date('Y-m-d');
For the format parameters, please refer to PHP's date function.
Last updated