HasHeader.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Dcat\Admin\Grid\Column;
  3. use Dcat\Admin\Exception\RuntimeException;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Model;
  6. use Dcat\Admin\Support\Helper;
  7. use Illuminate\Contracts\Support\Htmlable;
  8. use Illuminate\Contracts\Support\Renderable;
  9. /**
  10. * @property Grid $grid
  11. */
  12. trait HasHeader
  13. {
  14. /**
  15. * @var Filter
  16. */
  17. public $filter;
  18. /**
  19. * @var array
  20. */
  21. protected $headers = [];
  22. /**
  23. * Add contents to column header.
  24. *
  25. * @param string|Renderable|Htmlable $header
  26. *
  27. * @return $this
  28. */
  29. public function addHeader($header)
  30. {
  31. if ($header instanceof Filter) {
  32. $header->setParent($this);
  33. $this->filter = $header;
  34. }
  35. $this->headers[] = $header;
  36. return $this;
  37. }
  38. /**
  39. * Add a column sortable to column header.
  40. *
  41. * @param string $cast
  42. *
  43. * @return $this
  44. */
  45. public function sortable($cast = null)
  46. {
  47. $sorter = new Sorter($this->grid, $this->getName(), $cast);
  48. return $this->addHeader($sorter);
  49. }
  50. /**
  51. * Set column filter.
  52. *
  53. * @example
  54. * $grid->username()->filter();
  55. *
  56. * $grid->user()->filter('user.id');
  57. *
  58. * $grid->user()->filter(function () {
  59. * return $this->user['id'];
  60. * });
  61. *
  62. * $grid->username()->filter(
  63. * Grid\Column\Filter\StartWith::make(__('admin.username'))
  64. * );
  65. *
  66. * $grid->created_at()->filter(
  67. * Grid\Column\Filter\Equal::make(__('admin.created_at'))->date()
  68. * );
  69. *
  70. * @param Grid\Column\Filter|string $filter
  71. *
  72. * @return $this
  73. */
  74. public function filter($filter = null)
  75. {
  76. $valueKey = is_string($filter) || $filter instanceof \Closure ? $filter : null;
  77. if (! $filter || $valueKey) {
  78. $filter = Grid\Column\Filter\Equal::make()->valueFilter($valueKey);
  79. }
  80. if (! $filter instanceof Grid\Column\Filter) {
  81. throw new RuntimeException('The "$filter" must be a type of '.Grid\Column\Filter::class.'.');
  82. }
  83. return $this->addHeader($filter);
  84. }
  85. /**
  86. * @param string|\Closure $valueKey
  87. *
  88. * @return $this
  89. */
  90. public function filterByValue($valueKey = null)
  91. {
  92. return $this->filter(
  93. Grid\Column\Filter\Equal::make()
  94. ->valueFilter($valueKey)
  95. ->hide()
  96. );
  97. }
  98. /**
  99. * Add a help tooltip to column header.
  100. *
  101. * @param string|\Closure $message
  102. * @param null|string $style 'green', 'blue', 'red', 'purple'
  103. * @param null|string $placement 'bottom', 'left', 'right', 'top'
  104. *
  105. * @return $this
  106. */
  107. public function help($message, ?string $style = null, ?string $placement = null)
  108. {
  109. return $this->addHeader(new Help($message, $style, $placement));
  110. }
  111. /**
  112. * Add a binding based on filter to the model query.
  113. *
  114. * @param Model $model
  115. */
  116. public function bindFilterQuery(Model $model)
  117. {
  118. if ($this->filter) {
  119. $this->filter->addBinding($this->filter->value(), $model);
  120. }
  121. }
  122. /**
  123. * Render Column header.
  124. *
  125. * @return string
  126. */
  127. public function renderHeader()
  128. {
  129. if (! $this->headers) {
  130. return '';
  131. }
  132. $headers = implode(
  133. '',
  134. array_map(
  135. [Helper::class, 'render'],
  136. $this->headers
  137. )
  138. );
  139. return "<span class='grid-column-header'>$headers</span>";
  140. }
  141. }