laravel-admin模型表格Grid行展开ExpandRow设计

自定义ExpandRow

app/Exceptions/Column/ExpandRow.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace App\Exceptions\Column;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Displayers\AbstractDisplayer;

class ExpandRow extends AbstractDisplayer
{

public function display(\Closure $callback = null, $btn = '', $col_key = '')
{
$callback = $callback->bindTo($this->row);

$html = call_user_func($callback);

$script = <<<EOT

$('.grid-expand-$col_key').on('click', function () {
if ($(this).data('inserted') == '0') {
var key = $(this).data('key');
var row = $(this).closest('tr');
var html = $('template.grid-expand-'+key).html();

row.after("<tr><td colspan='"+row.find('td').length+"' style='padding:0 !important; border:0px;'>"+html+"</td></tr>");

$(this).data('inserted', 1);
}

$("i", this).toggleClass("fa-caret-right fa-caret-down");
});
EOT;
Admin::script($script);

$btn = $btn ?: $this->column->getName();

$key = $col_key . $this->getKey();

return <<<EOT
<a class="btn btn-xs btn-default grid-expand-$col_key" data-inserted="0" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}">
<i class="fa fa-caret-right"></i> $btn
</a>
<template class="grid-expand-{$key}">
<div id="grid-collapse-{$key}" class="collapse">$html</div>
</template>
EOT;
}

}

bootstrap注册

Admin/bootstrap.php

1
2
3
4
use App\Exceptions\Column\ExpandRow;
use Encore\Admin\Grid\Column;

Column::extend('expand', ExpandRow::class);

Grid中使用

1
2
3
4
5
6
7
8
9
10
11
12
13
$grid->column('version_id', "版本信息")->expand(function () {
//$app_version = AppVersion::where('id', $this->version_id)->get(['version_code', 'version_name', 'update_desc', 'bundle_desc'])->first();
$app_version = $this->version;
$version_array = array('版本号'=>$app_version->version_code, '版本名称'=>$app_version->version_name, '更新内容'=>$app_version->update_desc, '更新时间'=>$app_version->update_time);
$rows = array_only($version_array, ['版本号', '版本名称', '更新内容', '更新时间']);
return new Table([], $rows);
}, '版本信息', 'version_id');

$grid->column('file', "文件信息")->expand(function () {
$file_array = array('文件路径'=>$this->file_path, 'Size'=>$this->size, 'Md5'=>$this->md5, 'Sha1'=>$this->sha1);
$rows = array_only($file_array, ['文件路径', 'Size', 'Md5', 'Sha1']);
return new Table([], $rows);
}, '文件信息', 'file');

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :