Skip to content

基础使用

简单示例

php

namespace app\admin\controller;
use app\model\Testorder;
use app\model\User;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Show;
use Encore\Admin\Url;
use Illuminate\Support\Collection;
use LinAdmin\controller\CNewController;

class Ctestorder extends CNewController
{
    protected $header = '测试表格';
    protected $model = Testorder::class;

    /**
     * 列表
     * @return Grid
     */
    public function table()
    {
        $table = new Grid(new $this->model());
        //关联表
        $table->model()->with('user', "nickname,avatar");
        $table->column("id", "ID");
        $table->column('user_id', '用户ID');
        $table->column('user.nickname', '用户昵称');
        $table->column('user.avatar', '头像')->image();
        $table->column("create_time", "创建时间")->date();
        $table->actions(function (Grid\Displayers\Actions $action) {
            $action->enableShow(); //开启详情功能,默认为关闭
        });
        //导出csv文件
        $table->export(function (Grid\Exporters\CsvExporter $export) {
            $export->filename("测试表格明细"); //文件名,后面会自动带时间
            $export->except(["avatar"]); //过滤
        });
        $table->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('user.nickname',"用户昵称");
        });


        return $table;
    }
}

基本使用方法

添加列(column)

php
// 添加单列
$table->column('username', '用户名');

// 添加多列
$table->columns('email', 'username' ...);

修改显示输出 (display)

php


$table->column('email')->display(function ($email) {
    return "mailto:$email";
});

// 添加不存在的字段
$table->column('column_not_in_table')->display(function () {
    return 'blablabla....'.$this->id;
});

获取当前行数据 (row)

php
$table->column('first_name');
$table->column('last_name');

// 不存的字段列
$table->column('full_name')->display(function () {
    return $this->first_name.' '.$this->last_name;
});

设置创建按钮 (createButton)

php
// 禁用
$table->disableCreateButton();
// 显示
$table->showCreateButton();

设置查询过滤器 (filter)

php
// 禁用
$table->disableFilter();
// 显示
$table->showFilter();

设置批量操作按钮 (batchActions)

php
// 禁用
$table->disableBatchActions();
// 显示
$table->showBatchActions();

// 禁用批量删除按钮
$table->disableBatchDelete();
// 显示批量删除按钮
$table->showBatchDelete();

设置刷新按钮 (refresh)

php
// 禁用
$table->disableRefreshButton();
// 显示(默认)
$table->showRefreshButton();

设置整个工具栏 (allTools)

php
// 禁用
$table->disableAllTools();
// 显示
$table->showAllTools();

设置表格宽度

默认用100%,不会出现横向滚动条

php
$table->setTableWidth("150%");
$table->setTableWidth("1500px");

显示列字段的总和(sum)

$table->column('money', '金额')->totalRow(function ($total){ return "$".$total; });