工具栏

工具按钮

model-grid的头部默认有批量删除刷新两个操作工具,如果有更多的操作需求,系统提供了自定义工具的功能,下面的示例添加一个性别分类选择的按钮组工具。

设置工具栏按钮样式

{tip} Since v1.4.5

从这个v1.4.5工具栏按钮默认显示outline模式,效果如下

用法

  1. $grid->toolsWithOutline();
  2. // 禁止
  3. $grid->toolsWithOutline(false);

效果 工具栏 - 图1

禁用outline后的效果

工具栏 - 图2

如果你希望某个按钮不使用outline模式,可以在按钮的class属性中加上disable-outline

  1. $grid->tools('<a class="btn btn-primary disable-outline">测试按钮</a>');

自定义工具栏按钮

先定义工具类app/Admin/Extensions/Tools/UserGender.php继承工具类的基类Dcat\Admin\Grid\Tools\AbstractTool

  1. <?php
  2. namespace App\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. class UserGender extends AbstractTool
  6. {
  7. protected function script()
  8. {
  9. $url = request()->fullUrlWithQuery(['gender' => '_gender_']);
  10. return <<<JS
  11. $('input:radio.user-gender').change(function () {
  12. var url = "$url".replace('_gender_', $(this).val());
  13. Dcat.reload(url);
  14. });
  15. JS;
  16. }
  17. public function render()
  18. {
  19. Admin::script($this->script());
  20. $options = [
  21. 'all' => 'All',
  22. 'm' => 'Male',
  23. 'f' => 'Female',
  24. ];
  25. return view('admin.tools.gender', compact('options'));
  26. }
  27. }

视图admin.tools.gender文件为resources/views/admin/tools/gender.blade.php:

  1. <div class="btn-group" data-toggle="buttons">
  2. @foreach($options as $option => $label)
  3. <label class="btn btn-default {{ request()->get('gender', 'all') == $option ? 'active' : '' }}">
  4. <input type="radio" class="user-gender" value="{{ $option }}">{{$label}}
  5. </label>
  6. @endforeach
  7. </div>

Grid引入这个工具:

  1. $grid->tools(new UserGender());

model-grid定义中接收到gender参数后,做好数据查询就可以了:

  1. if (in_array($gender = request()->get('gender'), ['m', 'f'])) {
  2. $grid->model()->where('gender', $gender);
  3. }

可以参考上面的方式来添加自己的工具。

进阶用法

如果你的工具按钮需要与后端API进行交互,则可以参考以下方式定义:

{tip} AbstractTool类是属于Dcat\Admin\Actions\Action的子类,本质也是动作类的一种,更详细用法请参考动作类基本使用

  1. <?php
  2. namespace App\Admin\Grid\Tools;
  3. use Dcat\Admin\Grid\Tools\AbstractTool;
  4. use Illuminate\Http\Request;
  5. class SendMessage extends AbstractTool
  6. {
  7. /**
  8. * 按钮样式定义,默认 btn btn-white waves-effect
  9. *
  10. * @var string
  11. */
  12. protected $style = 'btn btn-white waves-effect';
  13. /**
  14. * 按钮文本
  15. *
  16. * @return string|void
  17. */
  18. public function title()
  19. {
  20. return '发送提醒';
  21. }
  22. /**
  23. * 确认弹窗,如果不需要则返回空即可
  24. *
  25. * @return array|string|void
  26. */
  27. public function confirm()
  28. {
  29. // 只显示标题
  30. // return '您确定要发送新的提醒消息吗?';
  31. // 显示标题和内容
  32. return ['您确定要发送新的提醒消息吗?', '确认信息内容,如没有可以留空'];
  33. }
  34. /**
  35. * 处理请求
  36. * 如果你的类中包含了此方法,则点击按钮后会自动向后端发起ajax请求,并且会通过此方法处理请求逻辑
  37. *
  38. * @param Request $request
  39. */
  40. public function handle(Request $request)
  41. {
  42. // 你的代码逻辑
  43. return $this->response()->success('发送成功')->refresh();
  44. }
  45. /**
  46. * 设置请求参数
  47. *
  48. * @return array|void
  49. */
  50. public function parameters()
  51. {
  52. return [
  53. ];
  54. }
  55. }

使用

  1. use App\Admin\Grid\Tools\SendMessage;
  2. $grid->tools(new SendMessage());

添加工具类

Grid::tools 方法允许传入 stringarray, AbstractTool闭包等类型参数,下面是演示。

  1. // 传入字符串
  2. $grid->tools('<a class="btn btn-sm btn-default">工具按钮测试</a>');
  3. // 传入数组
  4. $grid->tools([
  5. '<a class="btn btn-sm btn-default">工具按钮测试</a>',
  6. new UserGender(),
  7. ]);
  8. // 传入闭包
  9. $grid->tools(function (Grid\Tools $tools) {
  10. $tools->append(new UserGender());
  11. });

批量操作

禁用批量删除

系统默认开启了批量删除操作的功能,如果要禁用批量删除操作:

  1. $grid->tools(function ($tools) {
  2. $tools->batch(function ($batch) {
  3. $batch->disableDelete();
  4. });
  5. });
  6. // 也可以这样
  7. $grid->disableBatchDelete();
  8. // 或
  9. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  10. $batch->disableDelete();
  11. });

自定义批量操作

下面通过扩展一个对文章批量发布的功能来演示自定义批量操作的功能:

先定义操作类app/Admin/Extensions/Tools/ReleasePost.php,继承Dcat\Admin\Grid\BatchAction

{tip} BatchAction类是属于Dcat\Admin\Actions\Action的子类,本质也是动作类的一种,更详细用法请参考动作类基本使用

  1. <?php
  2. namespace App\Admin\Extensions\Tools;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Http\Request;
  5. class ReleasePost extends BatchAction
  6. {
  7. protected $action;
  8. // 注意action的构造方法参数一定要给默认值
  9. public function __construct($title = null, $action = 1)
  10. {
  11. $this->title = $title;
  12. $this->action = $action;
  13. }
  14. // 确认弹窗信息
  15. public function confirm()
  16. {
  17. return '您确定要发布已选中的文章吗?';
  18. }
  19. // 处理请求
  20. public function handle(Request $request)
  21. {
  22. // 获取选中的文章ID数组
  23. $keys = $this->getKey();
  24. // 获取请求参数
  25. $action = $request->get('action');
  26. foreach (Post::find($keys) as $post) {
  27. $post->released = $action;
  28. $post->save();
  29. }
  30. $message = $action ? '文章发布成功' : '文章下线成功';
  31. return $this->response()->success($message)->refresh();
  32. }
  33. // 设置请求参数
  34. public function parameters()
  35. {
  36. return [
  37. 'action' => $this->action,
  38. ];
  39. }
  40. }

看代码的实现,通过click操作发送一个post请求,把选中的行数据id通过数组的形式传给后端接口,后端接口拿到id列表和要修改的状态来更新数据,然后前端刷新页面(pjax reload),并弹出toastr提示操作成功。

model-grid中加入这个批量操作功能:

  1. $grid->batchActions([
  2. new ReleasePost('发布文章', 1),
  3. new ReleasePost('文章下线', 0)
  4. ]);
  5. // 也可以这么写
  6. $grid->batchActions(function ($batch) {
  7. $batch->add(new ReleasePost('发布文章', 1));
  8. $batch->add(new ReleasePost('文章下线', 0));
  9. });
  10. // 或
  11. $grid->tools(function ($tools) {
  12. $tools->batch(function ($batch) {
  13. $batch->add(new ReleasePost('发布文章', 1));
  14. $batch->add(new ReleasePost('文章下线', 0));
  15. });
  16. });

获取复选框选中的ID数组

通过getSelectedKeysScript方法可以获取到复选框选中的ID数组,用法如下

{tip} getSelectedKeysScript方法返回的是js代码,只能在js代码中使用。

  1. <?php
  2. namespace App\Admin\Extensions\Tools;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Http\Request;
  5. class ReleasePost extends BatchAction
  6. {
  7. protected $action;
  8. public function __construct($title, $action = 1)
  9. {
  10. $this->title = $title;
  11. $this->action = $action;
  12. }
  13. // 确认弹窗信息
  14. public function confirm()
  15. {
  16. return '您确定要发布已选中的文章吗?';
  17. }
  18. // 处理请求
  19. public function handle(Request $request)
  20. {
  21. ...
  22. }
  23. /**
  24. * 设置动作发起请求前的回调函数,返回false可以中断请求.
  25. *
  26. * @return string
  27. */
  28. public function actionScript(){
  29. $warning = __('No data selected!');
  30. return <<<JS
  31. function (data, target, action) {
  32. var key = {$this->getSelectedKeysScript()}
  33. if (key.length === 0) {
  34. Dcat.warning('{$warning}');
  35. return false;
  36. }
  37. // 设置主键为复选框选中的行ID数组
  38. action.options.key = key;
  39. }
  40. JS;
  41. }
  42. }

表单弹窗

请参考文档工具表单 - 弹窗