-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrid-batch.php
63 lines (54 loc) · 1.69 KB
/
grid-batch.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
use App\Suite;
use Illuminate\Support\Collection;
use Lego\Lego;
use Lego\Widget\Form;
$grid = Lego::grid(Suite::class);
$grid->add('id', 'ID');
$grid->add('street.city.name', '城市');
$grid->add('street.name', '街道');
$grid->add('address', '地址');
$grid->add('type', '类型');
$grid->add('status', '状态');
$grid->add('created_at|date', '创建日期');
$grid->addLeftTopButton('新建公寓', route('demo', 'suite'));
// 1、一键批处理
$grid->addBatch('一键删除')
->each(function (Suite $suite) {
$suite->delete();
});
$grid->addBatch('房型汇总')
->handle(function (Collection $suites) {
$message = $suites->groupBy('type')
->map(function ($suites, $type) {
return $type . ':' . count($suites);
})
->implode(',');
return Lego::message($message);
});
// 2、带提示信息的批处理
$grid->addBatch('批量删除-提示信息')
->message('确认删除 ?')
->each(function (Suite $suite) {
$suite->delete();
});
$grid->addBatch('批量删除-动态提示信息')
->message(function (Collection $suites) {
return "确认删除公寓 <共 {$suites->count()} 条>?";
})
->each(function (Suite $suite) {
$suite->delete();
});
// 3、带表单的批处理
$grid->addBatch('修改状态')
->openInNewTab()
->form(function (Form $form) use ($grid) {
$form->addSelect('status', '公寓状态')
->values(Suite::listStatus())
->required();
})
->each(function (Suite $suite, Form $form) {
$suite->status = $form->field('status')->getNewValue();
$suite->save();
});
return $grid;