AsyncTable.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Grid\LazyRenderable;
  4. use Illuminate\Support\Str;
  5. class AsyncTable extends Widget
  6. {
  7. public static $js = [
  8. '@grid-extension',
  9. ];
  10. /**
  11. * @var LazyRenderable
  12. */
  13. protected $renderable;
  14. /**
  15. * 设置是否自动加载.
  16. *
  17. * @var bool
  18. */
  19. protected $load = true;
  20. /**
  21. * 设置是否启用表格简化模式.
  22. *
  23. * @var bool
  24. */
  25. protected $simple;
  26. /**
  27. * AsyncTable constructor.
  28. *
  29. * @param LazyRenderable $renderable
  30. * @param bool $load
  31. */
  32. public function __construct(LazyRenderable $renderable, bool $load = true)
  33. {
  34. $this->from($renderable);
  35. $this->load($load);
  36. $this->class('async-table');
  37. $this->id('async-table-'.Str::random(8));
  38. }
  39. /**
  40. * 设置异步表格实例.
  41. *
  42. * @param LazyRenderable|null $renderable
  43. *
  44. * @return $this
  45. */
  46. public function from(?LazyRenderable $renderable)
  47. {
  48. if (! $renderable) {
  49. return $this;
  50. }
  51. $this->renderable = $renderable;
  52. return $this;
  53. }
  54. /**
  55. * 设置是否自动加载.
  56. *
  57. * @param bool $value
  58. *
  59. * @return $this
  60. */
  61. public function load(bool $value)
  62. {
  63. $this->load = $value;
  64. return $this;
  65. }
  66. /**
  67. * 设置是否启用表格简化模式.
  68. *
  69. * @param bool $value
  70. *
  71. * @return $this
  72. */
  73. public function simple(bool $value = true)
  74. {
  75. $this->simple = $value;
  76. if ($value) {
  77. $this->class('simple-grid', true);
  78. }
  79. return $this;
  80. }
  81. /**
  82. * 监听异步渲染完成事件.
  83. *
  84. * @param string $script
  85. *
  86. * @return $this
  87. */
  88. public function onLoad(string $script)
  89. {
  90. $this->script .= "$('{$this->getElementSelector()}').on('table:loaded', function (event) { {$script} });";
  91. return $this;
  92. }
  93. protected function addScript()
  94. {
  95. $this->script = <<<JS
  96. Dcat.grid.AsyncTable({container: '{$this->getElementSelector()}'});
  97. JS;
  98. if ($this->load) {
  99. $this->script .= $this->getLoadScript();
  100. }
  101. }
  102. /**
  103. * @return string
  104. */
  105. public function getElementSelector()
  106. {
  107. return '#'.$this->getHtmlAttribute('id');
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getLoadScript()
  113. {
  114. return <<<JS
  115. $('{$this->getElementSelector()}').trigger('table:load');
  116. JS;
  117. }
  118. public function render()
  119. {
  120. if ($this->simple !== null) {
  121. $this->renderable->simple();
  122. }
  123. $this->addScript();
  124. return parent::render();
  125. }
  126. public function html()
  127. {
  128. $this->setHtmlAttribute([
  129. 'data-url' => $this->renderable->getUrl(),
  130. ]);
  131. return <<<HTML
  132. <div {$this->formatHtmlAttributes()} style="min-height: 200px"></div>
  133. HTML;
  134. }
  135. }