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

自定义ExpandRow

app/Exceptions/Column/ExpandRow.php

<?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

use App\Exceptions\Column\ExpandRow;
use Encore\Admin\Grid\Column;

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

Grid中使用

$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');

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/expandrow-design-for-expanding-grid-rows-in-laravel-admin-model-table/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
laravel-admin模型表格Grid行展开ExpandRow设计
自定义ExpandRow app/Exceptions/Column/ExpandRow.php <?php namespace App\Exceptions\Column; use Encore\Admin\Admin; use Encore\Admin\Grid\Displa……
<<上一篇
下一篇>>
文章目录
关闭
目 录