123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace Dcat\Admin\Grid\Concerns;
- use Closure;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Grid;
- use Illuminate\Support\Collection;
- trait HasFilter
- {
- /**
- * The grid Filter.
- *
- * @var Grid\Filter
- */
- protected $filter;
- /**
- * Setup grid filter.
- *
- * @return void
- */
- protected function setUpFilter()
- {
- $this->filter = new Grid\Filter($this->model());
- }
- /**
- * Process the grid filter.
- *
- * @param bool $toArray
- *
- * @return Collection
- */
- public function processFilter()
- {
- $this->callBuilder();
- $this->handleExportRequest();
- $this->applyQuickSearch();
- $this->applyColumnFilter();
- $this->applySelectorQuery();
- return $this->filter->execute();
- }
- /**
- * Get or set the grid filter.
- *
- * @param Closure $callback
- *
- * @return $this|Grid\Filter
- */
- public function filter(Closure $callback = null)
- {
- if ($callback === null) {
- return $this->filter;
- }
- call_user_func($callback, $this->filter);
- return $this;
- }
- /**
- * Render the grid filter.
- *
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
- */
- public function renderFilter()
- {
- if (! $this->options['filter']) {
- return '';
- }
- return $this->filter->render();
- }
- /**
- * Expand filter.
- *
- * @return $this
- */
- public function expandFilter()
- {
- $this->filter->expand();
- return $this;
- }
- /**
- * Disable grid filter.
- *
- * @return $this
- */
- public function disableFilter(bool $disable = true)
- {
- $this->filter->disableCollapse($disable);
- return $this->option('filter', ! $disable);
- }
- /**
- * Show grid filter.
- *
- * @param bool $val
- *
- * @return $this
- */
- public function showFilter(bool $val = true)
- {
- return $this->disableFilter(! $val);
- }
- /**
- * Disable filter button.
- *
- * @param bool $disable
- *
- * @return $this
- */
- public function disableFilterButton(bool $disable = true)
- {
- $this->tools->disableFilterButton($disable);
- return $this;
- }
- /**
- * Show filter button.
- *
- * @param bool $val
- *
- * @return $this
- */
- public function showFilterButton(bool $val = true)
- {
- return $this->disableFilterButton(! $val);
- }
- protected function addFilterScript()
- {
- if (! $this->isAsyncRequest()) {
- return;
- }
- Admin::script(
- <<<JS
- var count = {$this->filter()->countConditions()};
- if (count > 0) {
- $('.async-{$this->getTableId()}').find('.filter-count').text('('+count+')');
- }
- JS
- );
- }
- }
|