HasComplexHeaders.php 3.2 KB

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