HasFilter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Grid;
  6. use Illuminate\Support\Collection;
  7. trait HasFilter
  8. {
  9. /**
  10. * The grid Filter.
  11. *
  12. * @var Grid\Filter
  13. */
  14. protected $filter;
  15. /**
  16. * Setup grid filter.
  17. *
  18. * @return void
  19. */
  20. protected function setUpFilter()
  21. {
  22. $this->filter = new Grid\Filter($this->model());
  23. }
  24. /**
  25. * Process the grid filter.
  26. *
  27. * @param bool $toArray
  28. *
  29. * @return Collection
  30. */
  31. public function processFilter()
  32. {
  33. $this->callBuilder();
  34. $this->handleExportRequest();
  35. $this->applyQuickSearch();
  36. $this->applyColumnFilter();
  37. $this->applySelectorQuery();
  38. return $this->filter->execute();
  39. }
  40. /**
  41. * Get or set the grid filter.
  42. *
  43. * @param Closure $callback
  44. *
  45. * @return $this|Grid\Filter
  46. */
  47. public function filter(Closure $callback = null)
  48. {
  49. if ($callback === null) {
  50. return $this->filter;
  51. }
  52. call_user_func($callback, $this->filter);
  53. return $this;
  54. }
  55. /**
  56. * Render the grid filter.
  57. *
  58. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  59. */
  60. public function renderFilter()
  61. {
  62. if (! $this->options['filter']) {
  63. return '';
  64. }
  65. return $this->filter->render();
  66. }
  67. /**
  68. * Expand filter.
  69. *
  70. * @return $this
  71. */
  72. public function expandFilter()
  73. {
  74. $this->filter->expand();
  75. return $this;
  76. }
  77. /**
  78. * Disable grid filter.
  79. *
  80. * @return $this
  81. */
  82. public function disableFilter(bool $disable = true)
  83. {
  84. $this->filter->disableCollapse($disable);
  85. return $this->option('filter', ! $disable);
  86. }
  87. /**
  88. * Show grid filter.
  89. *
  90. * @param bool $val
  91. *
  92. * @return $this
  93. */
  94. public function showFilter(bool $val = true)
  95. {
  96. return $this->disableFilter(! $val);
  97. }
  98. /**
  99. * Disable filter button.
  100. *
  101. * @param bool $disable
  102. *
  103. * @return $this
  104. */
  105. public function disableFilterButton(bool $disable = true)
  106. {
  107. $this->tools->disableFilterButton($disable);
  108. return $this;
  109. }
  110. /**
  111. * Show filter button.
  112. *
  113. * @param bool $val
  114. *
  115. * @return $this
  116. */
  117. public function showFilterButton(bool $val = true)
  118. {
  119. return $this->disableFilterButton(! $val);
  120. }
  121. protected function addFilterScript()
  122. {
  123. if (! $this->isAsyncRequest()) {
  124. return;
  125. }
  126. Admin::script(
  127. <<<JS
  128. var count = {$this->filter()->countConditions()};
  129. if (count > 0) {
  130. $('.async-{$this->getTableId()}').find('.filter-count').text('('+count+')');
  131. }
  132. JS
  133. );
  134. }
  135. }