Responsive.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Layout\Content;
  6. /**
  7. * @see http://gergeo.se/RWD-Table-Patterns/#demo
  8. */
  9. class Responsive
  10. {
  11. /**
  12. * @var Grid
  13. */
  14. protected $grid;
  15. /**
  16. * @var array
  17. */
  18. protected $options = ['addFocusBtn' => false];
  19. public function __construct(Grid $grid)
  20. {
  21. $this->grid = $grid;
  22. $this->options([
  23. 'i18n' => [
  24. 'focus' => trans('admin.responsive.focus'),
  25. 'display' => trans('admin.responsive.display'),
  26. 'displayAll' => trans('admin.responsive.display_all'),
  27. ],
  28. ]);
  29. }
  30. /**
  31. * Show focus button.
  32. *
  33. * @return $this
  34. */
  35. public function focus()
  36. {
  37. return $this->options(['addFocusBtn' => true]);
  38. }
  39. /**
  40. * @return $this
  41. */
  42. public function all()
  43. {
  44. $this->grid->columns()->each->responsive();
  45. return $this;
  46. }
  47. /**
  48. * @param array $options
  49. *
  50. * @return $this
  51. */
  52. public function options(array $options)
  53. {
  54. $this->options = array_merge($this->options, $options);
  55. return $this;
  56. }
  57. protected function disablePerfectScrollbar()
  58. {
  59. Content::composed(function (Content $content) {
  60. $content->perfectScrollbar(false);
  61. });
  62. }
  63. public function build()
  64. {
  65. Admin::collectAssets('rwd-table');
  66. $this->disablePerfectScrollbar();
  67. $opt = json_encode($this->options);
  68. // 这里需要延迟执行,否则可能会造成页面元素跳跃闪动
  69. Admin::script("setTimeout(function() {
  70. $('#{$this->grid->getTableId()}').parent().responsiveTable($opt);
  71. }, 400);");
  72. }
  73. }