HasPaginator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * Paginate the grid.
  24. *
  25. * @param int $perPage
  26. *
  27. * @return void
  28. */
  29. public function paginate(int $perPage = 20)
  30. {
  31. $this->perPage = $perPage;
  32. $this->model()->setPerPage($perPage);
  33. }
  34. /**
  35. * 是否使用 simplePaginate 方法分页.
  36. *
  37. * @param bool $value
  38. *
  39. * @return $this
  40. */
  41. public function simplePaginate(bool $value = true)
  42. {
  43. $this->model()->simple($value);
  44. return $this;
  45. }
  46. /**
  47. * @return int
  48. */
  49. public function getPerPage()
  50. {
  51. return $this->perPage;
  52. }
  53. /**
  54. * @param string $paginator
  55. *
  56. * @return $this
  57. */
  58. public function setPaginatorClass(string $paginator)
  59. {
  60. $this->options['paginator_class'] = $paginator;
  61. return $this;
  62. }
  63. /**
  64. * Get the grid paginator.
  65. *
  66. * @return \Dcat\Admin\Grid\Tools\Paginator
  67. */
  68. public function paginator()
  69. {
  70. if (! $this->paginator) {
  71. $paginatorClass = $this->options['paginator_class'] ?: (config('admin.grid.paginator_class') ?: Tools\Paginator::class);
  72. $this->paginator = new $paginatorClass($this);
  73. }
  74. return $this->paginator;
  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. }