HasFilter.php 3.0 KB

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