Responsive.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. /**
  6. * @see http://gergeo.se/RWD-Table-Patterns/#demo
  7. */
  8. class Responsive
  9. {
  10. /**
  11. * @var Grid
  12. */
  13. protected $grid;
  14. /**
  15. * @var array
  16. */
  17. protected $options = ['addFocusBtn' => false];
  18. public function __construct(Grid $grid)
  19. {
  20. $this->grid = $grid;
  21. $this->options([
  22. 'i18n' => [
  23. 'focus' => trans('admin.responsive.focus'),
  24. 'display' => trans('admin.responsive.display'),
  25. 'displayAll' => trans('admin.responsive.display_all'),
  26. ],
  27. ]);
  28. }
  29. /**
  30. * Show focus button.
  31. *
  32. * @return $this
  33. */
  34. public function focus()
  35. {
  36. return $this->options(['addFocusBtn' => true]);
  37. }
  38. /**
  39. * @return $this
  40. */
  41. public function all()
  42. {
  43. $this->grid->columns()->each->responsive();
  44. return $this;
  45. }
  46. /**
  47. * @param array $options
  48. *
  49. * @return $this
  50. */
  51. public function options(array $options)
  52. {
  53. $this->options = array_merge($this->options, $options);
  54. return $this;
  55. }
  56. public function build()
  57. {
  58. Admin::collectAssets('rwd-table');
  59. $opt = json_encode($this->options);
  60. if (request()->pjax()) {
  61. Admin::script("$('#{$this->grid->getTableId()}').parent().responsiveTable($opt);");
  62. } else {
  63. Admin::script("setTimeout(function(){ $('#{$this->grid->getTableId()}').parent().responsiveTable($opt); },10);");
  64. }
  65. }
  66. }