HasPaginator.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Grid\Tools;
  4. trait HasPaginator
  5. {
  6. /**
  7. * Per-page options.
  8. *
  9. * @var array
  10. */
  11. protected $perPages = [10, 20, 30, 50, 100, 200];
  12. /**
  13. * Default items count per-page.
  14. *
  15. * @var int
  16. */
  17. protected $perPage = 20;
  18. /**
  19. * Paginate the grid.
  20. *
  21. * @param int $perPage
  22. *
  23. * @return void
  24. */
  25. public function paginate(int $perPage = 20)
  26. {
  27. $this->perPage = $perPage;
  28. $this->model()->setPerPage($perPage);
  29. }
  30. /**
  31. * @return int
  32. */
  33. public function getPerPage()
  34. {
  35. return $this->perPage;
  36. }
  37. /**
  38. * Get the grid paginator.
  39. *
  40. * @return mixed
  41. */
  42. public function paginator()
  43. {
  44. if (!$this->options['show_pagination']) {
  45. return;
  46. }
  47. return new Tools\Paginator($this);
  48. }
  49. /**
  50. * If this grid use pagination.
  51. *
  52. * @return bool
  53. */
  54. public function allowPagination()
  55. {
  56. return $this->options['show_pagination'];
  57. }
  58. /**
  59. * Set per-page options.
  60. *
  61. * @param array $perPages
  62. */
  63. public function perPages(array $perPages)
  64. {
  65. $this->perPages = $perPages;
  66. return $this;
  67. }
  68. /**
  69. * Get per-page options.
  70. *
  71. * @return array
  72. */
  73. public function getPerPages()
  74. {
  75. return $this->perPages;
  76. }
  77. /**
  78. * Disable grid pagination.
  79. *
  80. * @return $this
  81. */
  82. public function disablePagination(bool $disable = true)
  83. {
  84. $this->model->usePaginate(!$disable);
  85. return $this->option('show_pagination', !$disable);
  86. }
  87. /**
  88. * Show grid pagination.
  89. *
  90. * @param bool $val
  91. * @return $this
  92. */
  93. public function showPagination(bool $val = true)
  94. {
  95. return $this->disablePagination(!$val);
  96. }
  97. }