Skip to content

Latest commit

 

History

History
203 lines (152 loc) · 3.81 KB

grid.md

File metadata and controls

203 lines (152 loc) · 3.81 KB

Grid

Basic

$grid = \Lego::grid($query);

$grid->add('id', 'ID'));
$grid->add('summary', '摘要');
$grid->add('billing_date|date', '账单日');
$grid->add('fen|fen2yuan', '金额(元)');
$grid->add('receiver.name', '接收人');
$grid->add('paid_at', '付款时间');
$grid->add('created_at', '创建时间');
$grid->paginate(100)->orderBy('id', true); // order by id desc

return $grid->view('view-file', compact('filter', 'grid'));

insert to special location

$grid->after('id')->add('column', 'Description');

remove

$grid->remove('id');
$grid->remove('paid_at', 'created_at');
$grid->remove(['paid_at', 'created_at']);

set default value

$grid->add('column', 'Description')->default('default value');

add button

$grid->addLeftTopButton('new', route('...'));

Pipe

Basic

$grid->add('name|trim|strip', 'Name');
$grid = Lego::grid(City::class);

$grid->add('name|trim', 'Name')

    ->pipe('strip')

    ->pipe(function ($name) {
        return do_some_thing($name);
    })

    ->pipe(function ($name, City $city) {
        /**
        * $city is the model of current row
        */
        return $name . '(' . $city->name .')';
    })

    ->pipe(function ($name, City $city, Cell $cell) {
        /**
        * $cell is instanceof \Lego\Widget\Grid\Cell
        * 
        * $cell->name() === 'name'; // Important: NO pipe name
        * $cell->description() === 'Name';
        * $cell->getOriginalValue();
        */
        
        return $name;
    });

Available pipes

  • trim

    $grid->add('name|trim', 'Name');
  • strip, remove html tags

     $grid->add('name|strip', 'Name');
  • date, convert to date string

    $grid->add('published_at|date', 'Published Date');
    // eg: 2017-01-01 12:00:01 => 2017-01-01
  • time, convert to time string

    $grid->add('updated_at|time', 'Last Moidify Time');
    // eg: 2017-01-01 12:00:01 => 12:00:01

Self-Defined Pipe

lego_register(
    GridCellPipe::class,
    function ($name, Model $model, Cell $cell) {
        return $model->getAttribute($cell->name() . '_en')
    }, 
    'trans2en'
)
$grid->add('name|trans2en', 'Name');

Export as Excel (.xls)

Simple

$grid->export('filename');

Notice: Lego 导出功能依赖 Laravel-Excel,所以需要将下面类注册到 config/app.phpproviders 中:

Maatwebsite\Excel\ExcelServiceProvider::class

Exporting callback

$grid->export('filename', function (Grid $grid) {
    $grid->paginate(1000); // export more
})

批处理操作

一键批处理

$grid->addBatch('批量删除')
    ->each(function (Advance $advance) {
        $advance->delete();
    });
$grid->addBatch('汇总')
    ->handle(function (Collection $advances) {
        return Lego::message('' . $advances->sum('amount') . '');
    });

带确认信息的批处理

$grid->addBatch('批量删除')
    ->message('确认删除?')
    ->each(function (Advance $advance) {
        $advance->delete();
    });

带动态确认信息的批处理

$grid->addBatch('批量删除')
    ->message(function (Collection $advances) {
        return "确认删除 {$advances->count()} 条记录?"
    })
    ->each(function (Advance $advance) {
        $advance->delete();
    });

带表单的批处理

$grid->addBatch('变更状态')
    ->form(function (Form $form) {
        $form->addSelect('status')->values('已付款', '作废')->required();
    })
    ->action(function (Advance $advance, Form $form) {
        $advance->status = $form->field('status')->getNewValue();
        $advance->save();
    });