| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace Dcat\Admin\Grid\Column;
- use Dcat\Admin\Exception\RuntimeException;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Grid\Model;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Renderable;
- /**
- * @property Grid $grid
- */
- trait HasHeader
- {
- /**
- * @var Filter
- */
- public $filter;
- /**
- * @var array
- */
- protected $headers = [];
- /**
- * Add contents to column header.
- *
- * @param string|Renderable|Htmlable $header
- *
- * @return $this
- */
- public function addHeader($header)
- {
- if ($header instanceof Filter) {
- $header->setParent($this);
- $this->filter = $header;
- }
- $this->headers[] = $header;
- return $this;
- }
- /**
- * Add a column sortable to column header.
- *
- * @param string $cast
- *
- * @return $this
- */
- public function sortable($cast = null)
- {
- $sorter = new Sorter($this->grid, $this->getName(), $cast);
- return $this->addHeader($sorter);
- }
- /**
- * Set column filter.
- *
- * @example
- * $grid->username()->filter();
- *
- * $grid->user()->filter('user.id');
- *
- * $grid->user()->filter(function () {
- * return $this->user['id'];
- * });
- *
- * $grid->username()->filter(
- * Grid\Column\Filter\StartWith::make(__('admin.username'))
- * );
- *
- * $grid->created_at()->filter(
- * Grid\Column\Filter\Equal::make(__('admin.created_at'))->date()
- * );
- *
- * @param Grid\Column\Filter|string $filter
- *
- * @return $this
- */
- public function filter($filter = null)
- {
- $valueKey = is_string($filter) || $filter instanceof \Closure ? $filter : null;
- if (! $filter || $valueKey) {
- $filter = Grid\Column\Filter\Equal::make()->valueFilter($valueKey);
- }
- if (! $filter instanceof Grid\Column\Filter) {
- throw new RuntimeException('The "$filter" must be a type of '.Grid\Column\Filter::class.'.');
- }
- return $this->addHeader($filter);
- }
- /**
- * @param string|\Closure $valueKey
- *
- * @return $this
- */
- public function filterByValue($valueKey = null)
- {
- return $this->filter(
- Grid\Column\Filter\Equal::make()
- ->valueFilter($valueKey)
- ->hide()
- );
- }
- /**
- * Add a help tooltip to column header.
- *
- * @param string|\Closure $message
- * @param null|string $style 'green', 'blue', 'red', 'purple'
- * @param null|string $placement 'bottom', 'left', 'right', 'top'
- *
- * @return $this
- */
- public function help($message, ?string $style = null, ?string $placement = null)
- {
- return $this->addHeader(new Help($message, $style, $placement));
- }
- /**
- * Add a binding based on filter to the model query.
- *
- * @param Model $model
- */
- public function bindFilterQuery(Model $model)
- {
- if ($this->filter) {
- $this->filter->addBinding($this->filter->value(), $model);
- }
- }
- /**
- * Render Column header.
- *
- * @return string
- */
- public function renderHeader()
- {
- if (! $this->headers) {
- return '';
- }
- $headers = implode(
- '',
- array_map(
- [Helper::class, 'render'],
- $this->headers
- )
- );
- return "<span class='grid-column-header'>$headers</span>";
- }
- }
|