HasPaginator.php 2.8 KB

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