Header.php 2.7 KB

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