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.

202 lines
6.1 KiB

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2019 Blobt.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace blobt\grid;
  26. use Closure;
  27. use yii\base\Model;
  28. use yii\data\ActiveDataProvider;
  29. use yii\data\ArrayDataProvider;
  30. use yii\db\ActiveQueryInterface;
  31. use yii\helpers\ArrayHelper;
  32. use yii\helpers\Html;
  33. use yii\helpers\Inflector;
  34. /**
  35. *
  36. *
  37. * @author Blobt
  38. * @email 380255922@qq.com
  39. * @created Aug 13, 2019
  40. */
  41. class DataColumn extends Column {
  42. /**
  43. * @var string 本列需要显示的模型属性值,
  44. * 如果设置了value,则会显示value。
  45. */
  46. public $attribute;
  47. /**
  48. * @var string 列首的label。如果不设置,则会显示attribute关联的label
  49. */
  50. public $label;
  51. /**
  52. * @var bool 是否需要html转义
  53. */
  54. public $encodeLabel = true;
  55. /**
  56. * @var string|Closure 字符串或匿名函数.
  57. */
  58. public $value;
  59. /**
  60. * @var string|array|Closure 每个数据模型的值应以哪种格式显示(例如“raw”`、“text”`、“html”`,`[date','php:y-m-d']`)。
  61. * 默认格式为“文本”
  62. */
  63. public $format = 'text';
  64. /**
  65. * @var bool 有时候我们记录数据时候,使用了整数值来代替字符,当此属性被设置成true时候,
  66. * 会视图找到数值对应的字符,并显示出来
  67. */
  68. public $showConstText = false;
  69. /**
  70. * @var bool 是否开始排序
  71. */
  72. public $enableSorting = true;
  73. /**
  74. * @var bool 排序a标签的html属性
  75. */
  76. public $sortLinkOptions = [];
  77. /**
  78. * @var bool 筛选表单单
  79. */
  80. public $filter;
  81. /**
  82. * 初始化参数
  83. */
  84. public function init() {
  85. parent::init();
  86. }
  87. /**
  88. * 渲染头单元.
  89. */
  90. public function renderHeaderCell() {
  91. return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function renderHeaderCellContent() {
  97. if ($this->header !== null || $this->label === null && $this->attribute === null) {
  98. return parent::renderHeaderCellContent();
  99. }
  100. $label = $this->getHeaderCellLabel();
  101. if ($this->encodeLabel) {
  102. $label = Html::encode($label);
  103. }
  104. if ($this->attribute !== null && $this->enableSorting &&
  105. ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
  106. return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
  107. }
  108. return $label;
  109. }
  110. /**
  111. * {@inheritdoc]
  112. */
  113. protected function getHeaderCellLabel() {
  114. $provider = $this->grid->dataProvider;
  115. if ($this->label === null) {
  116. if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
  117. /* @var $modelClass Model */
  118. $modelClass = $provider->query->modelClass;
  119. $model = $modelClass::instance();
  120. $label = $model->getAttributeLabel($this->attribute);
  121. } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
  122. /* @var $modelClass Model */
  123. $modelClass = $provider->modelClass;
  124. $model = $modelClass::instance();
  125. $label = $model->getAttributeLabel($this->attribute);
  126. } else {
  127. $models = $provider->getModels();
  128. if (($model = reset($models)) instanceof Model) {
  129. /* @var $model Model */
  130. $label = $model->getAttributeLabel($this->attribute);
  131. } else {
  132. $label = Inflector::camel2words($this->attribute);
  133. }
  134. }
  135. } else {
  136. $label = $this->label;
  137. }
  138. return $label;
  139. }
  140. /**
  141. * 返回单元格值
  142. * @param mixed $model
  143. * @param mixed $key 模型ID
  144. * @param int $index 行号
  145. * @return string
  146. */
  147. public function getDataCellValue($model, $key, $index) {
  148. if ($this->value !== null) {
  149. if (is_string($this->value)) {
  150. return ArrayHelper::getValue($model, $this->value);
  151. }
  152. return call_user_func($this->value, $model, $key, $index, $this);
  153. } elseif ($this->attribute !== null) {
  154. $val = ArrayHelper::getValue($model, $this->attribute);
  155. if ($this->showConstText) {
  156. $constArrKey = Inflector::variablize($this->attribute, '_');
  157. if (isset($model::$$constArrKey)) {
  158. $val = $model::$$constArrKey[$val];
  159. }
  160. }
  161. return $val;
  162. }
  163. return null;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. protected function renderDataCellContent($model, $key, $index) {
  169. if ($this->content === null) {
  170. return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
  171. }
  172. return parent::renderDataCellContent($model, $key, $index);
  173. }
  174. }