Expand.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Support\Str;
  6. class Expand extends AbstractDisplayer
  7. {
  8. protected $button;
  9. /**
  10. * @var array
  11. */
  12. protected static $counter = 0;
  13. public function button($button)
  14. {
  15. $this->button = $button;
  16. }
  17. public function display($callbackOrButton = null)
  18. {
  19. $html = $this->value;
  20. if ($callbackOrButton && $callbackOrButton instanceof \Closure) {
  21. $callback = $callbackOrButton->bindTo($this->row);
  22. $html = $callback($this);
  23. if ($html instanceof Renderable) {
  24. $html = $html->render();
  25. }
  26. } elseif ($callbackOrButton && is_string($callbackOrButton)) {
  27. $this->button = $callbackOrButton;
  28. }
  29. $this->setupScript();
  30. $key = $this->getDataKey();
  31. $button = is_null($this->button) ? $this->value : $this->button;
  32. return <<<EOT
  33. <span class="grid-expand" data-inserted="0" data-key="{$key}" data-toggle="collapse" data-target="#grid-collapse-{$key}">
  34. <a href="javascript:void(0)"><i class="fa fa-angle-double-right"></i> $button</a>
  35. </span>
  36. <template class="grid-expand-{$key}">
  37. <div id="grid-collapse-{$key}" class="collapse">$html</div>
  38. </template>
  39. EOT;
  40. }
  41. /**
  42. * @return string
  43. */
  44. protected function getDataKey()
  45. {
  46. $key = $this->key() ?: Str::random(8);
  47. static::$counter++;
  48. return $this->grid->getName().$key.'-'.static::$counter;
  49. }
  50. protected function setupScript()
  51. {
  52. $script = <<<'JS'
  53. $('.grid-expand').off('click').click(function () {
  54. if ($(this).data('inserted') == '0') {
  55. var key = $(this).data('key');
  56. var row = $(this).closest('tr');
  57. var html = $('template.grid-expand-'+key).html();
  58. row.after("<tr><td colspan='"+(row.find('td').length)+"' style='padding:0 !important; border:0;height:0;'>"+html+"</td></tr>");
  59. $(this).data('inserted', 1);
  60. }
  61. $("i", this).toggleClass("fa-angle-double-right fa-angle-double-down");
  62. });
  63. JS;
  64. Admin::script($script);
  65. }
  66. }