ComplexHeader.php 2.7 KB

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