Table.php 3.1 KB

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