You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

293 lines
9.4 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: admin
  5. * Date: 2017/1/17
  6. * Time: 19:08
  7. */
  8. namespace common\components;
  9. use kartik\grid\DataColumn;
  10. use kartik\grid\GridView;
  11. use yii\data\ActiveDataProvider;
  12. use yii\db\ActiveQuery;
  13. use yii\helpers\ArrayHelper;
  14. use Yii;
  15. use yii\helpers\Html;
  16. use yii\widgets\LinkPager;
  17. /**
  18. * Class zGridView
  19. *
  20. * @property bool|array $pagerButtons
  21. * @package common\components
  22. */
  23. class zGridView extends GridView
  24. {
  25. public $pagerFixed = false; //分页是否保持在底部不动
  26. public $pagerButtons = []; //分页同一行的按钮
  27. public $pageSummaryRowOptions = ['class' => 'kv-page-summary'];
  28. public $summaryOptions = ['class' => 'zPager'];
  29. public $showEmpty = false; //为空时,是否显示信息
  30. public $emptyTextOptions = ['class' => 'empty text-center'];
  31. public $zSummary = []; //汇总数据,
  32. public $layout = "{zSummary}\n{items}\n{pager}";
  33. public $dataCount; //当前页记录数量
  34. public $dataTotalCount; //总记录数
  35. public $resizableColumns = false;
  36. public function renderTableBody()
  37. {
  38. $tmp = $this->showPageSummary;
  39. $this->showPageSummary = false;
  40. $content = $this::zTableBody();
  41. $this->showPageSummary = $tmp;
  42. if ($this->showPageSummary) {
  43. return $this->renderPageSummary() . $content;
  44. }
  45. return $content;
  46. }
  47. //为空时,不显示任何数据
  48. public function zTableBody()
  49. {
  50. $models = array_values($this->dataProvider->getModels());
  51. $keys = $this->dataProvider->getKeys();
  52. $rows = [];
  53. foreach ($models as $index => $model) {
  54. $key = $keys[$index];
  55. if ($this->beforeRow !== null) {
  56. $row = call_user_func($this->beforeRow, $model, $key, $index, $this);
  57. if (!empty($row)) {
  58. $rows[] = $row;
  59. }
  60. }
  61. $rows[] = $this->renderTableRow($model, $key, $index);
  62. if ($this->afterRow !== null) {
  63. $row = call_user_func($this->afterRow, $model, $key, $index, $this);
  64. if (!empty($row)) {
  65. $rows[] = $row;
  66. }
  67. }
  68. }
  69. if (empty($rows) && $this->emptyText != '') {
  70. $colspan = count($this->columns);
  71. return "<tbody>\n<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>\n</tbody>";
  72. } else {
  73. return "<tbody>\n" . implode("\n", $rows) . "\n</tbody>";
  74. }
  75. }
  76. /**
  77. * User: wangxj
  78. *
  79. * 禁用kartik加载bootstrap的class
  80. *
  81. */
  82. protected function initBootstrapStyle()
  83. {
  84. if ($this->striped) {
  85. Html::addCssClass($this->tableOptions, 'table table-striped table-bordered table-zFixed table-responsive');
  86. }
  87. }
  88. /**
  89. * 重写分页. 左侧有按钮,固定在页面底部,需要引用ui.css
  90. * @return string the rendering result
  91. */
  92. public function renderPager()
  93. {
  94. $pagination = $this->dataProvider->getPagination();
  95. $pagerHtml = '';
  96. $this->dataCount = $this->dataProvider->getCount();
  97. $this->dataTotalCount = $this->dataProvider->getTotalCount();
  98. if ($pagination === false || $this->dataCount <= 0) {
  99. //如果有功能按钮,没有分页也要显示
  100. if (!empty($this->pagerButtons) || $this->summary) {
  101. $pagerHtml = $this->renderPagerButton() . $pagerHtml;
  102. }
  103. return '<div class="bottom_nav">' . $pagerHtml . '</div>';
  104. }
  105. /* @var $class LinkPager */
  106. $pager = $this->pager;
  107. $class = ArrayHelper::remove($pager, 'class', LinkPager::className());
  108. $pager['pagination'] = $pagination;
  109. $pager['view'] = $this->getView();
  110. $pager['nextPageLabel'] = '';
  111. $pager['prevPageLabel'] = '';
  112. // $pager['linkOptions'] = ['class'=> 'icon-right'];
  113. $pagerHtml = $class::widget($pager);
  114. //如果有按钮
  115. if (!empty($this->pagerButtons)) {
  116. $pagerHtml = $this->renderPagerButton() . $pagerHtml;
  117. } else {
  118. $pagerHtml = $this->renderPagerGo() . $this->renderSummary() . $pagerHtml;
  119. }
  120. //如果需要固定
  121. if ($this->pagerFixed) {
  122. $pagerHtml = '<div class="bottom_nav">' . $pagerHtml . '</div>';
  123. }
  124. return $pagerHtml;
  125. }
  126. protected function renderPagerButton()
  127. {
  128. $buttonHtml = '<div style="display: inline-block; margin:14px 0;" >';
  129. foreach ($this->pagerButtons as $pagerButton) {
  130. $buttonHtml .= ' ' . $pagerButton;
  131. }
  132. $buttonHtml .= '</div>';
  133. $buttonHtml .= $this->renderPagerGo() . $this->renderSummary();
  134. return $buttonHtml;
  135. }
  136. public function renderSection($name)
  137. {
  138. switch ($name) {
  139. case '{zSummary}':
  140. return $this->renderZSummary();
  141. case '{summary}':
  142. return $this->renderSummary();
  143. case '{items}':
  144. return $this->renderItems();
  145. case '{pager}':
  146. return $this->renderPager();
  147. case '{sorter}':
  148. return $this->renderSorter();
  149. default:
  150. return false;
  151. }
  152. }
  153. /**
  154. * User: wangxj
  155. *
  156. * 汇总,$this->zSummary数组中的元素
  157. *
  158. * @return string
  159. */
  160. public function renderZSummary()
  161. {
  162. $str = '';
  163. if (!empty($this->zSummary)) {
  164. $str .= '<div class="zSummary-div">';
  165. foreach ($this->zSummary as $item) {
  166. $dataProvider = $this->dataProvider;
  167. /* @var $dataProvider ActiveDataProvider */
  168. /* @var $query ActiveQuery */
  169. /* @var $model yii\db\ActiveRecord */
  170. $query = $dataProvider->query;
  171. $modelClass = $query->modelClass;
  172. $model = new $modelClass();
  173. if (isset($item['label'])) {
  174. $title = $item['label'];
  175. } else {
  176. $title = $model->getAttributeLabel($item['attribute']);
  177. }
  178. $str .= '<span class="zSummary-label">' . $title . ':</span>' .
  179. '<span class="zSummary-number ' . $item['class'] . '">' . number_format($query->sum($item['attribute']), 2) . '</span>';
  180. }
  181. $str .= '</div>';
  182. }
  183. return $str;
  184. }
  185. /**
  186. * Renders the summary text.
  187. */
  188. public
  189. function renderSummary()
  190. {
  191. $count = $this->dataProvider->getCount();
  192. if ($count <= 0) {
  193. return '';
  194. }
  195. $summaryOptions = $this->summaryOptions;
  196. $tag = ArrayHelper::remove($summaryOptions, 'tag', 'div');
  197. if (($pagination = $this->dataProvider->getPagination()) !== false) {
  198. $totalCount = $this->dataProvider->getTotalCount();
  199. $begin = $pagination->getPage() * $pagination->pageSize + 1;
  200. $end = $begin + $count - 1;
  201. if ($begin > $end) {
  202. $begin = $end;
  203. }
  204. $page = $pagination->getPage() + 1;
  205. $pageCount = $pagination->pageCount;
  206. if (($summaryContent = $this->summary) === null) {
  207. return Html::tag($tag, "共 <b>" . number_format($pageCount) . "</b>页 <b>" . number_format($totalCount) . "</b> 条记录", $summaryOptions);
  208. }
  209. } else {
  210. $begin = $page = $pageCount = 1;
  211. $end = $totalCount = $count;
  212. if (($summaryContent = $this->summary) === null) {
  213. return Html::tag($tag, Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [
  214. 'begin' => $begin,
  215. 'end' => $end,
  216. 'count' => $count,
  217. 'totalCount' => $totalCount,
  218. 'page' => $page,
  219. 'pageCount' => $pageCount,
  220. ]), $summaryOptions);
  221. }
  222. }
  223. return Yii::$app->getI18n()->format($summaryContent, [
  224. 'begin' => $begin,
  225. 'end' => $end,
  226. 'count' => $count,
  227. 'totalCount' => $totalCount,
  228. 'page' => $page,
  229. 'pageCount' => $pageCount,
  230. ], Yii::$app->language);
  231. }
  232. //分页显示跳转页数
  233. protected
  234. function renderPagerGo()
  235. {
  236. $pagination = $this->dataProvider->getPagination();
  237. $str = '';
  238. if ($pagination !== false && $pagination->pageCount > 1) {
  239. $str = '<div class="zPager zPagerGo"> <input type="text" id="pager-go" class="form-control pager-go" /> ';
  240. $str .= '<button type="button" id="pager-btn" class="form-control btn pager-btn" onclick="z.pager(\'pagination\',' . $pagination->pageCount . ', this)" >GO</button></div>';
  241. }
  242. return $str;
  243. }
  244. /**
  245. * 每一列的总和
  246. *
  247. * @return null|string
  248. */
  249. public
  250. function renderPageSummary()
  251. {
  252. if (!$this->showPageSummary) {
  253. return null;
  254. }
  255. $cells = [];
  256. /** @var zDataColumn $column */
  257. /* @var $dataProvider ActiveDataProvider */
  258. /* @var $query ActiveQuery */
  259. /* @var $model yii\db\ActiveRecord */
  260. $dataProvider = $this->dataProvider;
  261. $query = $dataProvider->query;
  262. foreach ($this->columns as $column) {
  263. if (get_class($column) == 'common\components\zDataColumn') {
  264. $column->query = $query;
  265. }
  266. $cells[] = $column->renderPageSummaryCell();
  267. }
  268. $tag = ArrayHelper::remove($this->pageSummaryContainer, 'tag', 'tbody');
  269. $content = Html::tag('tr', implode('', $cells), $this->pageSummaryRowOptions);
  270. return Html::tag($tag, $content, $this->pageSummaryContainer);
  271. }
  272. }