123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace Dcat\Admin\Grid;
- use Dcat\Admin\Grid\Column\Help;
- use Dcat\Admin\Grid\Column\Sorter;
- use Dcat\Admin\Widgets\Widget;
- use Dcat\Admin\Grid;
- class Header extends Widget
- {
- /**
- * @var Grid
- */
- protected $grid;
- /**
- * @var string
- */
- protected $label;
- /**
- * @var array
- */
- protected $columnNames = [];
- /**
- * @var array
- */
- protected $html = [];
- public function __construct(Grid $grid, string $label, array $columnNames)
- {
- $this->grid = $grid;
- $this->label = admin_trans_field($label);
- $this->columnNames = $columnNames;
- $this->setupAttributes();
- }
- /**
- * @return array
- */
- public function getColumnNames()
- {
- return $this->columnNames;
- }
- /**
- * 默认隐藏字段
- * 开启responsive模式有效
- *
- * @return $this
- */
- public function hide()
- {
- return $this->responsive(0);
- }
- public function getLabel()
- {
- return $this->label;
- }
- /**
- * 允许使用responsive
- * 开启responsive模式有效
- *
- * data-priority=”1″ 保持可见,但可以在下拉列表筛选隐藏。
- * data-priority=”2″ 480px 分辨率以下可见
- * data-priority=”3″ 640px 以下可见
- * data-priority=”4″ 800px 以下可见
- * data-priority=”5″ 960px 以下可见
- * data-priority=”6″ 1120px 以下可见
- *
- * @param int $priority
- * @return $this
- */
- public function responsive(int $priority = 1)
- {
- $this->setHtmlAttribute('data-priority', $priority);
- return $this;
- }
- /**
- *
- * @param string $html
- * @return $this
- */
- public function append($html)
- {
- $this->html[] = $html;
- return $this;
- }
- /**
- * 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 = 'bottom')
- {
- return $this->append((new Help($message, $style, $placement))->render());
- }
- protected function setupAttributes()
- {
- $count = count($this->columnNames);
- if ($count == 1) {
- $this->htmlAttributes['rowspan'] = 2;
- } else {
- $this->htmlAttributes['colspan'] = $count;
- }
- }
- public function render()
- {
- $headers = implode(' ', $this->html);
- return "<th {$this->formatHtmlAttributes()}>{$this->label}{$headers}</th>";
- }
- }
|