AsyncTable.php 2.6 KB

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