$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'));
$grid->after('id')->add('column', 'Description');
$grid->remove('id');
$grid->remove('paid_at', 'created_at');
$grid->remove(['paid_at', 'created_at']);
$grid->add('column', 'Description')->default('default value');
$grid->addLeftTopButton('new', route('...'));
$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;
});
-
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
lego_register(
GridCellPipe::class,
function ($name, Model $model, Cell $cell) {
return $model->getAttribute($cell->name() . '_en')
},
'trans2en'
)
$grid->add('name|trans2en', 'Name');
$grid->export('filename');
Notice: Lego 导出功能依赖 Laravel-Excel,所以需要将下面类注册到
config/app.php
的providers
中:Maatwebsite\Excel\ExcelServiceProvider::class
$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();
});