GridView.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-03-21 18:45
  7. */
  8. namespace backend\grid;
  9. use Yii;
  10. use yii\helpers\ArrayHelper;
  11. use yii\widgets\LinkPager;
  12. use yii\grid\GridViewAsset;
  13. use yii\helpers\Json;
  14. use yii\widgets\BaseListView;
  15. /**
  16. * @inheritdoc
  17. */
  18. class GridView extends \yii\grid\GridView
  19. {
  20. /* @var $dataColumnClass \backend\grid\DataColumn */
  21. public $dataColumnClass = 'backend\grid\DataColumn';
  22. public $options = ['class' => 'fixed-table-header', 'style' => 'margin-right: 0px;'];
  23. public $tableOptions = ['class' => 'table table-hover'];
  24. public $layout = "{items}\n<div class='row'><div class='col-sm-3' style='line-height: 567%'>{summary}</div><div class='col-sm-9'><div class='dataTables_paginate paging_simple_numbers'>{pager}</div></div></div>";
  25. public $pagerOptions = [
  26. 'firstPageLabel' => '首页',
  27. 'lastPageLabel' => '尾页',
  28. 'prevPageLabel' => '上一页',
  29. 'nextPageLabel' => '下一页',
  30. 'options' => [
  31. 'class' => 'pagination',
  32. ],
  33. ];
  34. public $filterRow;
  35. /**
  36. * @inheritdoc
  37. */
  38. public function init()
  39. {
  40. parent::init();
  41. if( !$this->rowOptions && $this->rowOptions !== false ) {
  42. $this->rowOptions = function ($model, $key, $index, $grid) {
  43. if ($index % 2 === 0) {
  44. return ['class' => 'odd'];
  45. } else {
  46. return ['class' => 'even'];
  47. }
  48. };
  49. }
  50. if( !$this->pagerOptions && $this->pagerOptions !== false ) {
  51. $this->pagerOptions = [
  52. 'firstPageLabel' => Yii::t('app', 'first'),
  53. 'lastPageLabel' => Yii::t('app', 'last'),
  54. 'prevPageLabel' => Yii::t('app', 'previous'),
  55. 'nextPageLabel' => Yii::t('app', 'next'),
  56. 'options' => [
  57. 'class' => 'pagination',
  58. ]
  59. ];
  60. }
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function renderTableRow($model, $key, $index)
  66. {
  67. if ($this->filterRow !== null && call_user_func($this->filterRow, $model, $key, $index, $this) === false) {
  68. return '';
  69. }
  70. return parent::renderTableRow($model, $key, $index);
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function renderPager()
  76. {
  77. $pagination = $this->dataProvider->getPagination();
  78. if ($pagination === false || $this->dataProvider->getCount() <= 0) {
  79. return '';
  80. }
  81. /* @var $class LinkPager */
  82. $pager = $this->pager;
  83. $class = ArrayHelper::remove($pager, 'class', LinkPager::className());
  84. $pager['pagination'] = $pagination;
  85. $pager['view'] = $this->getView();
  86. $pager = array_merge($pager, $this->pagerOptions);
  87. return $class::widget($pager);
  88. }
  89. /**
  90. * @inheritdoc
  91. */
  92. public function run()
  93. {
  94. $id = $this->options['id'];
  95. $options = Json::htmlEncode($this->getClientOptions());
  96. $view = $this->getView();
  97. GridViewAsset::register($view);
  98. $view->registerJs("jQuery('#$id').yiiGridView($options);");
  99. BaseListView::run();
  100. }
  101. }