BatchActions.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\BatchAction;
  5. use Illuminate\Support\Collection;
  6. class BatchActions extends AbstractTool
  7. {
  8. /**
  9. * @var Collection
  10. */
  11. protected $actions;
  12. /**
  13. * @var bool
  14. */
  15. protected $enableDelete = true;
  16. /**
  17. * @var bool
  18. */
  19. protected $isHoldSelectAllCheckbox = false;
  20. /**
  21. * BatchActions constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->actions = new Collection();
  26. $this->appendDefaultAction();
  27. }
  28. /**
  29. * Append default action(batch delete action).
  30. *
  31. * return void
  32. */
  33. protected function appendDefaultAction()
  34. {
  35. $this->add(new BatchDelete(trans('admin.delete')));
  36. }
  37. /**
  38. * Disable delete.
  39. *
  40. * @return $this
  41. */
  42. public function disableDelete(bool $disable = true)
  43. {
  44. $this->enableDelete = ! $disable;
  45. return $this;
  46. }
  47. /**
  48. * Disable delete And Hide SelectAll Checkbox.
  49. *
  50. * @return $this
  51. */
  52. public function disableDeleteAndHideSelectAll()
  53. {
  54. $this->enableDelete = false;
  55. $this->isHoldSelectAllCheckbox = true;
  56. return $this;
  57. }
  58. /**
  59. * Add a batch action.
  60. *
  61. * @param BatchAction $action
  62. *
  63. * @return $this
  64. */
  65. public function add(BatchAction $action)
  66. {
  67. $action->selectorPrefix = '.grid-batch-action-'.$this->actions->count();
  68. $this->actions->push($action);
  69. return $this;
  70. }
  71. /**
  72. * Prepare batch actions.
  73. *
  74. * @return void
  75. */
  76. protected function prepareActions()
  77. {
  78. foreach ($this->actions as $action) {
  79. $action->setGrid($this->parent);
  80. }
  81. }
  82. /**
  83. * Render BatchActions button groups.
  84. *
  85. * @return string
  86. */
  87. public function render()
  88. {
  89. if (! $this->enableDelete) {
  90. $this->actions->shift();
  91. }
  92. if ($this->actions->isEmpty()) {
  93. return '';
  94. }
  95. $this->prepareActions();
  96. $data = [
  97. 'actions' => $this->actions,
  98. 'selectAllName' => $this->parent->getSelectAllName(),
  99. 'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
  100. 'parent' => $this->parent,
  101. ];
  102. return Admin::view('admin::grid.batch-actions', $data);
  103. }
  104. }