ComplexHeader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Grid\Column\Help;
  5. use Dcat\Admin\Widgets\Widget;
  6. use Illuminate\Support\Collection;
  7. class ComplexHeader extends Widget
  8. {
  9. /**
  10. * @var Grid
  11. */
  12. protected $grid;
  13. /**
  14. * @var string
  15. */
  16. protected $label;
  17. /**
  18. * @var array
  19. */
  20. protected $columnNames = [];
  21. /**
  22. * @var array
  23. */
  24. protected $html = [];
  25. public function __construct(Grid $grid, string $label, array $columnNames)
  26. {
  27. $this->grid = $grid;
  28. $this->label = admin_trans_field($label);
  29. $this->columnNames = collect($columnNames);
  30. $this->addDefaultAttributes();
  31. }
  32. /**
  33. * @return Collection
  34. */
  35. public function getColumnNames()
  36. {
  37. return $this->columnNames;
  38. }
  39. /**
  40. * @return Collection
  41. */
  42. public function columns()
  43. {
  44. return $this->columnNames->map(function ($name) {
  45. return $this->grid->allColumns()->get($name);
  46. })->filter();
  47. }
  48. /**
  49. * 默认隐藏字段
  50. * 开启responsive模式有效.
  51. *
  52. * @return $this
  53. */
  54. public function hide()
  55. {
  56. return $this->responsive(0);
  57. }
  58. public function getLabel()
  59. {
  60. return $this->label;
  61. }
  62. /**
  63. * 允许使用responsive
  64. * 开启responsive模式有效.
  65. *
  66. * data-priority=”1″ 保持可见,但可以在下拉列表筛选隐藏。
  67. * data-priority=”2″ 480px 分辨率以下可见
  68. * data-priority=”3″ 640px 以下可见
  69. * data-priority=”4″ 800px 以下可见
  70. * data-priority=”5″ 960px 以下可见
  71. * data-priority=”6″ 1120px 以下可见
  72. *
  73. * @param int $priority
  74. *
  75. * @return $this
  76. */
  77. public function responsive(int $priority = 1)
  78. {
  79. $this->setHtmlAttribute('data-priority', $priority);
  80. return $this;
  81. }
  82. /**
  83. * @param string $html
  84. *
  85. * @return $this
  86. */
  87. public function append($html)
  88. {
  89. $this->html[] = $html;
  90. return $this;
  91. }
  92. /**
  93. * @param string|\Closure $message
  94. * @param null|string $style 'green', 'blue', 'red', 'purple'
  95. * @param null|string $placement 'bottom', 'left', 'right', 'top'
  96. *
  97. * @return $this
  98. */
  99. public function help($message, ?string $style = null, ?string $placement = null)
  100. {
  101. return $this->append((new Help($message, $style, $placement))->render());
  102. }
  103. protected function addDefaultAttributes()
  104. {
  105. $count = $this->columnNames->count();
  106. if ($count == 1) {
  107. $this->htmlAttributes['rowspan'] = 2;
  108. } else {
  109. $this->htmlAttributes['colspan'] = $count;
  110. }
  111. }
  112. public function render()
  113. {
  114. $headers = implode(' ', $this->html);
  115. return "<th {$this->formatHtmlAttributes()}>{$this->label}<span class='grid-column-header'>{$headers}</span></th>";
  116. }
  117. }