Table.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Encore\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. use Illuminate\Support\Arr;
  5. class Table extends Widget implements Renderable
  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 array
  21. */
  22. protected $style = [];
  23. /**
  24. * Table constructor.
  25. *
  26. * @param array $headers
  27. * @param array $rows
  28. * @param array $style
  29. */
  30. public function __construct($headers = [], $rows = [], $style = [])
  31. {
  32. $this->setHeaders($headers);
  33. $this->setRows($rows);
  34. $this->setStyle($style);
  35. $this->class('table '.implode(' ', $this->style));
  36. }
  37. /**
  38. * Set table headers.
  39. *
  40. * @param array $headers
  41. *
  42. * @return $this
  43. */
  44. public function setHeaders($headers = [])
  45. {
  46. $this->headers = $headers;
  47. return $this;
  48. }
  49. /**
  50. * Set table rows.
  51. *
  52. * @param array $rows
  53. *
  54. * @return $this
  55. */
  56. public function setRows($rows = [])
  57. {
  58. if (Arr::isAssoc($rows)) {
  59. foreach ($rows as $key => $item) {
  60. $this->rows[] = [$key, $item];
  61. }
  62. return $this;
  63. }
  64. $this->rows = $rows;
  65. return $this;
  66. }
  67. /**
  68. * Set table style.
  69. *
  70. * @param array $style
  71. *
  72. * @return $this
  73. */
  74. public function setStyle($style = [])
  75. {
  76. $this->style = $style;
  77. return $this;
  78. }
  79. /**
  80. * Render the table.
  81. *
  82. * @return string
  83. */
  84. public function render()
  85. {
  86. $vars = [
  87. 'headers' => $this->headers,
  88. 'rows' => $this->rows,
  89. 'style' => $this->style,
  90. 'attributes' => $this->formatAttributes(),
  91. ];
  92. return view($this->view, $vars)->render();
  93. }
  94. }