HasPaginator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Grid\Tools;
  4. use Dcat\Admin\Support\Helper;
  5. trait HasPaginator
  6. {
  7. /**
  8. * @var Tools\Paginator
  9. */
  10. protected $paginator;
  11. /**
  12. * Per-page options.
  13. *
  14. * @var array
  15. */
  16. protected $perPages = [10, 20, 30, 50, 100, 200];
  17. /**
  18. * Default items count per-page.
  19. *
  20. * @var int
  21. */
  22. protected $perPage = 20;
  23. /**
  24. * @var string
  25. */
  26. protected $paginatorClass = Tools\Paginator::class;
  27. /**
  28. * Paginate the grid.
  29. *
  30. * @param int $perPage
  31. *
  32. * @return void
  33. */
  34. public function paginate(int $perPage = 20)
  35. {
  36. $this->perPage = $perPage;
  37. $this->model()->setPerPage($perPage);
  38. }
  39. /**
  40. * @return int
  41. */
  42. public function getPerPage()
  43. {
  44. return $this->perPage;
  45. }
  46. /**
  47. * @param string $paginator
  48. *
  49. * @return $this
  50. */
  51. public function setPaginatorClass(string $paginator)
  52. {
  53. $this->paginatorClass = $paginator;
  54. return $this;
  55. }
  56. /**
  57. * Get the grid paginator.
  58. *
  59. * @return mixed
  60. */
  61. public function paginator()
  62. {
  63. return $this->paginator ?: ($this->paginator = new $this->paginatorClass($this));
  64. }
  65. /**
  66. * If this grid use pagination.
  67. *
  68. * @return bool
  69. */
  70. public function allowPagination()
  71. {
  72. return $this->options['pagination'];
  73. }
  74. /**
  75. * Set per-page options.
  76. *
  77. * @param array $perPages
  78. */
  79. public function perPages(array $perPages)
  80. {
  81. $this->perPages = $perPages;
  82. return $this;
  83. }
  84. /**
  85. * @return $this
  86. */
  87. public function disablePerPages()
  88. {
  89. return $this->perPages([]);
  90. }
  91. /**
  92. * Get per-page options.
  93. *
  94. * @return array
  95. */
  96. public function getPerPages()
  97. {
  98. return $this->perPages;
  99. }
  100. /**
  101. * Disable grid pagination.
  102. *
  103. * @return $this
  104. */
  105. public function disablePagination(bool $disable = true)
  106. {
  107. $this->model->usePaginate(! $disable);
  108. return $this->option('pagination', ! $disable);
  109. }
  110. /**
  111. * Show grid pagination.
  112. *
  113. * @param bool $val
  114. *
  115. * @return $this
  116. */
  117. public function showPagination(bool $val = true)
  118. {
  119. return $this->disablePagination(! $val);
  120. }
  121. /**
  122. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View|string
  123. */
  124. public function renderPagination()
  125. {
  126. return view('admin::grid.table-pagination', ['grid' => $this]);
  127. }
  128. }