HasComplexHeaders.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Exception\InvalidArgumentException;
  4. use Dcat\Admin\Grid\Column;
  5. use Dcat\Admin\Grid\ComplexHeader;
  6. use Illuminate\Support\Collection;
  7. trait HasComplexHeaders
  8. {
  9. /**
  10. * @var ComplexHeader[]|Collection
  11. */
  12. protected $complexHeaders;
  13. /**
  14. * Merge cells.
  15. *
  16. * @param string $column
  17. * @param array $columnNames
  18. * @param string $label
  19. * @return ComplexHeader
  20. */
  21. public function combine(string $column, array $columnNames, string $label = null)
  22. {
  23. if (count($columnNames) < 2) {
  24. throw new InvalidArgumentException('Invalid column names.');
  25. }
  26. if (! $this->complexHeaders) {
  27. $this->complexHeaders = new Collection();
  28. }
  29. $this->withBorder();
  30. return $this->complexHeaders[$column] = new ComplexHeader($this, $column, $columnNames, $label);
  31. }
  32. /**
  33. * @return ComplexHeader[]
  34. */
  35. public function getComplexHeaderNames()
  36. {
  37. if (! $this->complexHeaders) {
  38. return [];
  39. }
  40. return $this->complexHeaders->map(function ($header) {
  41. return $header->getName();
  42. })->toArray();
  43. }
  44. /**
  45. * @return ComplexHeader[]|Collection|null
  46. */
  47. public function getComplexHeaders()
  48. {
  49. return $this->complexHeaders;
  50. }
  51. /**
  52. * Reorder the headers.
  53. */
  54. protected function sortHeaders()
  55. {
  56. if (! $this->complexHeaders) {
  57. return;
  58. }
  59. $originalHeaders = $this->complexHeaders->toArray();
  60. $originalColumns = $this->columns;
  61. $headersColumns = $this->complexHeaders = $this->columns = [];
  62. foreach ($originalHeaders as $header) {
  63. $headersColumns = array_merge(
  64. $headersColumns,
  65. $tmp = $header->getColumnNames()->toArray()
  66. );
  67. foreach ($tmp as &$name) {
  68. if ($column = $originalColumns->get($name)) {
  69. $this->columns[$name] = $column;
  70. }
  71. }
  72. }
  73. $before = $after = [];
  74. $isBefore = true;
  75. foreach ($originalColumns as $name => $column) {
  76. if ($isBefore && ! isset($this->columns[$name])) {
  77. $before[$name] = $column;
  78. continue;
  79. }
  80. $isBefore = false;
  81. if (! isset($this->columns[$name])) {
  82. $after[$name] = $column;
  83. }
  84. }
  85. $beforeHeaders = $this->createHeaderWithColumns($before);
  86. $afterHeaders = $this->createHeaderWithColumns($after);
  87. $this->columnNames = array_merge(
  88. array_keys($before),
  89. array_keys($this->columns),
  90. array_keys($after)
  91. );
  92. $this->columns = collect($this->columns);
  93. $this->complexHeaders = collect(
  94. array_merge(
  95. $beforeHeaders,
  96. array_values($originalHeaders),
  97. $afterHeaders
  98. )
  99. );
  100. }
  101. protected function createHeaderWithColumns(array $columns)
  102. {
  103. $headers = [];
  104. /* @var Column $column */
  105. foreach ($columns as $name => $column) {
  106. $header = new ComplexHeader($this, $column->getName(), [$name], $column->getLabel());
  107. if ($html = $column->renderHeader()) {
  108. $header->append($html);
  109. }
  110. $headers[] = $header;
  111. }
  112. return $headers;
  113. }
  114. }