Table.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Support\Arr;
  4. class Table extends Widget
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $view = 'admin::widgets.table';
  10. /**
  11. * @var array
  12. */
  13. protected $headers = [];
  14. /**
  15. * @var array
  16. */
  17. protected $rows = [];
  18. /**
  19. * @var array
  20. */
  21. protected $style = [];
  22. /**
  23. * @var int
  24. */
  25. protected $depth = 0;
  26. /**
  27. * Table constructor.
  28. *
  29. * @param array $headers
  30. * @param array $rows
  31. * @param array $style
  32. */
  33. public function __construct($headers = [], $rows = [], $style = [])
  34. {
  35. if ($headers && ! $rows) {
  36. $rows = $headers;
  37. $headers = [];
  38. }
  39. $this->setHeaders($headers);
  40. $this->setRows($rows);
  41. $this->setStyle($style);
  42. $this->class('table '.implode(' ', (array) $this->style), true);
  43. }
  44. /**
  45. * Set table headers.
  46. *
  47. * @param array $headers
  48. *
  49. * @return $this
  50. */
  51. public function setHeaders($headers = [])
  52. {
  53. $this->headers = $headers;
  54. return $this;
  55. }
  56. /**
  57. * @param int $depth
  58. *
  59. * @return $this
  60. */
  61. public function depth(int $depth)
  62. {
  63. $this->depth = $depth;
  64. return $this;
  65. }
  66. /**
  67. * Set table rows.
  68. *
  69. * @param array $rows
  70. *
  71. * @return $this
  72. */
  73. public function setRows($rows = [])
  74. {
  75. if (! Arr::isAssoc($rows)) {
  76. $this->rows = $rows;
  77. return $this;
  78. }
  79. $noTrPadding = false;
  80. foreach ($rows as $key => $item) {
  81. if (is_array($item)) {
  82. if (Arr::isAssoc($item)) {
  83. $borderLeft = $this->level ? 'table-left-border-nofirst' : 'table-left-border';
  84. $item = static::make()
  85. ->depth($this->depth + 1)
  86. ->setRows($item)
  87. ->class('table-no-top-border '.$borderLeft, true)
  88. ->render();
  89. if (! $noTrPadding) {
  90. $this->class('table-no-tr-padding', true);
  91. }
  92. $noTrPadding = true;
  93. } else {
  94. $item = json_encode($item, JSON_UNESCAPED_UNICODE);
  95. }
  96. }
  97. $this->rows[] = [$key, $item];
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Set table style.
  103. *
  104. * @param array $style
  105. *
  106. * @return $this
  107. */
  108. public function setStyle($style = [])
  109. {
  110. $this->style = $style;
  111. return $this;
  112. }
  113. /**
  114. * Render the table.
  115. *
  116. * @return string
  117. */
  118. public function render()
  119. {
  120. $vars = [
  121. 'headers' => $this->headers,
  122. 'rows' => $this->rows,
  123. 'style' => $this->style,
  124. 'attributes' => $this->formatHtmlAttributes(),
  125. ];
  126. return view($this->view, $vars)->render();
  127. }
  128. }