TreeGrid.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. namespace backend\widgets\grid;
  3. use Closure;
  4. use Yii;
  5. use yii\base\InvalidConfigException;
  6. use yii\base\Widget;
  7. use yii\grid\DataColumn;
  8. use yii\helpers\ArrayHelper;
  9. use yii\helpers\Html;
  10. use yii\helpers\Json;
  11. use yii\i18n\Formatter;
  12. /**
  13. * TreeGrid renders a jQuery TreeGrid component.
  14. * The code was based in: https://github.com/yiisoft/yii2/blob/master/framework/grid/GridView.php
  15. *
  16. * @see https://github.com/maxazan/jquery-treegrid
  17. * @author Leandro Gehlen <leandrogehlen@gmail.com>
  18. */
  19. class TreeGrid extends Widget
  20. {
  21. /**
  22. * @var \yii\data\DataProviderInterface|\yii\data\BaseDataProvider the data provider for the view. This property is required.
  23. */
  24. public $dataProvider;
  25. /**
  26. * @var string the default data column class if the class name is not explicitly specified when configuring a data column.
  27. * Defaults to 'leandrogehlen\treegrid\TreeColumn'.
  28. */
  29. public $dataColumnClass;
  30. /**
  31. * @var array the HTML attributes for the container tag of the grid view.
  32. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  33. */
  34. public $options = ['class' => 'table table-striped table-bordered'];
  35. /**
  36. * @var array The plugin options
  37. */
  38. public $pluginOptions = [];
  39. /**
  40. * @var array the HTML attributes for the table header row.
  41. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  42. */
  43. public $headerRowOptions = [];
  44. /**
  45. * @var array the HTML attributes for the table footer row.
  46. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  47. */
  48. public $footerRowOptions = [];
  49. /**
  50. * @var string the HTML display when the content of a cell is empty
  51. */
  52. public $emptyCell = '&nbsp;';
  53. /**
  54. * @var string the HTML content to be displayed when [[dataProvider]] does not have any data.
  55. */
  56. public $emptyText;
  57. /**
  58. * @var array the HTML attributes for the emptyText of the list view.
  59. * The "tag" element specifies the tag name of the emptyText element and defaults to "div".
  60. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  61. */
  62. public $emptyTextOptions = ['class' => 'empty'];
  63. /**
  64. * @var boolean whether to show the header section of the grid table.
  65. */
  66. public $showHeader = true;
  67. /**
  68. * @var boolean whether to show the footer section of the grid table.
  69. */
  70. public $showFooter = false;
  71. /**
  72. * @var boolean whether to show the grid view if [[dataProvider]] returns no data.
  73. */
  74. public $showOnEmpty = true;
  75. /**
  76. * @var array|Formatter the formatter used to format model attribute values into displayable texts.
  77. * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]]
  78. * instance. If this property is not set, the "formatter" application component will be used.
  79. */
  80. public $formatter;
  81. /**
  82. * @var array|Closure the HTML attributes for the table body rows. This can be either an array
  83. * specifying the common HTML attributes for all body rows, or an anonymous function that
  84. * returns an array of the HTML attributes. The anonymous function will be called once for every
  85. * data model returned by [[dataProvider]]. It should have the following signature:
  86. *
  87. * ```php
  88. * function ($model, $key, $index, $grid)
  89. * ```
  90. *
  91. * - `$model`: the current data model being rendered
  92. * - `$key`: the key value associated with the current data model
  93. * - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
  94. * - `$grid`: the GridView object
  95. *
  96. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  97. */
  98. public $rowOptions = [];
  99. /**
  100. * @var Closure an anonymous function that is called once BEFORE rendering each data model.
  101. * It should have the similar signature as [[rowOptions]]. The return result of the function
  102. * will be rendered directly.
  103. */
  104. public $beforeRow;
  105. /**
  106. * @var Closure an anonymous function that is called once AFTER rendering each data model.
  107. * It should have the similar signature as [[rowOptions]]. The return result of the function
  108. * will be rendered directly.
  109. */
  110. public $afterRow;
  111. /**
  112. * @var string name of key column used to build tree
  113. */
  114. public $keyColumnName;
  115. /**
  116. * @var string name of parent column used to build tree
  117. */
  118. public $parentColumnName;
  119. /**
  120. * @var mixed parent column value of root elements from data
  121. */
  122. public $parentRootValue = null;
  123. /**
  124. * @var array grid column configuration. Each array element represents the configuration
  125. * for one particular grid column.
  126. * @see \yii\grid::$columns for details.
  127. */
  128. public $columns = [];
  129. /**
  130. * Initializes the grid view.
  131. * This method will initialize required property values and instantiate [[columns]] objects.
  132. */
  133. public function init()
  134. {
  135. if ($this->dataProvider === null) {
  136. throw new InvalidConfigException('The "dataProvider" property must be set.');
  137. }
  138. if ($this->emptyText === null) {
  139. $this->emptyText = Yii::t('yii', 'No results found.');
  140. }
  141. if (!isset($this->options['id'])) {
  142. $this->options['id'] = $this->getId();
  143. }
  144. if ($this->formatter == null) {
  145. $this->formatter = Yii::$app->getFormatter();
  146. } elseif (is_array($this->formatter)) {
  147. $this->formatter = Yii::createObject($this->formatter);
  148. }
  149. if (!$this->formatter instanceof Formatter) {
  150. throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
  151. }
  152. if (!$this->keyColumnName) {
  153. throw new InvalidConfigException('The "keyColumnName" property must be specified"');
  154. }
  155. if (!$this->parentColumnName) {
  156. throw new InvalidConfigException('The "parentColumnName" property must be specified"');
  157. }
  158. $this->initColumns();
  159. }
  160. /**
  161. * Runs the widget.
  162. */
  163. public function run()
  164. {
  165. $id = $this->options['id'];
  166. $options = Json::htmlEncode($this->pluginOptions);
  167. $view = $this->getView();
  168. TreeGridAsset::register($view);
  169. $view->registerJs("jQuery('#$id').treegrid($options);");
  170. if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
  171. $header = $this->showHeader ? $this->renderTableHeader() : false;
  172. $body = $this->renderItems();
  173. $footer = $this->showFooter ? $this->renderTableFooter() : false;
  174. $content = array_filter([
  175. $header,
  176. $body,
  177. $footer
  178. ]);
  179. return Html::tag('table', implode("\n", $content), $this->options);
  180. } else {
  181. return $this->renderEmpty();
  182. }
  183. }
  184. /**
  185. * Renders the HTML content indicating that the list view has no data.
  186. * @return string the rendering result
  187. * @see emptyText
  188. */
  189. public function renderEmpty()
  190. {
  191. $options = $this->emptyTextOptions;
  192. $tag = ArrayHelper::remove($options, 'tag', 'div');
  193. return Html::tag($tag, ($this->emptyText === null ? Yii::t('yii', 'No results found.') : $this->emptyText), $options);
  194. }
  195. /**
  196. * Renders a table row with the given data model and key.
  197. * @param mixed $model the data model to be rendered
  198. * @param mixed $key the key associated with the data model
  199. * @param integer $index the zero-based index of the data model among the model array returned by [[dataProvider]].
  200. * @return string the rendering result
  201. */
  202. public function renderTableRow($model, $key, $index)
  203. {
  204. $cells = [];
  205. /* @var $column TreeColumn */
  206. foreach ($this->columns as $column) {
  207. $cells[] = $column->renderDataCell($model, $key, $index);
  208. }
  209. if ($this->rowOptions instanceof Closure) {
  210. $options = call_user_func($this->rowOptions, $model, $key, $index, $this);
  211. } else {
  212. $options = $this->rowOptions;
  213. }
  214. $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
  215. $id = ArrayHelper::getValue($model, $this->keyColumnName);
  216. Html::addCssClass($options, "treegrid-$id");
  217. $parentId = ArrayHelper::getValue($model, $this->parentColumnName);
  218. if ($parentId) {
  219. if(ArrayHelper::getValue($this->pluginOptions, 'initialState') == 'collapsed'){
  220. Html::addCssStyle($options, 'display: none;');
  221. }
  222. Html::addCssClass($options, "treegrid-parent-$parentId");
  223. }
  224. return Html::tag('tr', implode('', $cells), $options);
  225. }
  226. /**
  227. * Renders the table header.
  228. * @return string the rendering result.
  229. */
  230. public function renderTableHeader()
  231. {
  232. $cells = [];
  233. foreach ($this->columns as $column) {
  234. /* @var $column TreeColumn */
  235. $cells[] = $column->renderHeaderCell();
  236. }
  237. $content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);
  238. return "<thead>\n" . $content . "\n</thead>";
  239. }
  240. /**
  241. * Renders the table footer.
  242. * @return string the rendering result.
  243. */
  244. public function renderTableFooter()
  245. {
  246. $cells = [];
  247. foreach ($this->columns as $column) {
  248. /* @var $column TreeColumn */
  249. $cells[] = $column->renderFooterCell();
  250. }
  251. $content = Html::tag('tr', implode('', $cells), $this->footerRowOptions);
  252. return "<tfoot>\n" . $content . "\n</tfoot>";
  253. }
  254. /**
  255. * Renders the data models for the grid view.
  256. */
  257. public function renderItems()
  258. {
  259. $rows = [];
  260. $this->dataProvider->setKeys([]);
  261. $models = array_values($this->dataProvider->getModels());
  262. $models = $this->normalizeData($models, $this->parentRootValue);
  263. $this->dataProvider->setModels($models);
  264. $this->dataProvider->setKeys(null);
  265. $this->dataProvider->prepare();
  266. $keys = $this->dataProvider->getKeys();
  267. foreach ($models as $index => $model) {
  268. $key = $keys[$index];
  269. if ($this->beforeRow !== null) {
  270. $row = call_user_func($this->beforeRow, $model, $key, $index, $this);
  271. if (!empty($row)) {
  272. $rows[] = $row;
  273. }
  274. }
  275. $rows[] = $this->renderTableRow($model, $key, $index);
  276. if ($this->afterRow !== null) {
  277. $row = call_user_func($this->afterRow, $model, $key, $index, $this);
  278. if (!empty($row)) {
  279. $rows[] = $row;
  280. }
  281. }
  282. }
  283. if (empty($rows)) {
  284. $colspan = count($this->columns);
  285. return "<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>";
  286. } else {
  287. return implode("\n", $rows);
  288. }
  289. }
  290. /**
  291. * Creates column objects and initializes them.
  292. */
  293. protected function initColumns()
  294. {
  295. if (empty($this->columns)) {
  296. $this->guessColumns();
  297. }
  298. foreach ($this->columns as $i => $column) {
  299. if (is_string($column)) {
  300. $column = $this->createDataColumn($column);
  301. } else {
  302. $column = Yii::createObject(array_merge([
  303. 'class' => $this->dataColumnClass ? : TreeColumn::className(),
  304. 'grid' => $this,
  305. ], $column));
  306. }
  307. if (!$column->visible) {
  308. unset($this->columns[$i]);
  309. continue;
  310. }
  311. $this->columns[$i] = $column;
  312. }
  313. }
  314. /**
  315. * Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label".
  316. * @param string $text the column specification string
  317. * @return DataColumn the column instance
  318. * @throws InvalidConfigException if the column specification is invalid
  319. */
  320. protected function createDataColumn($text)
  321. {
  322. if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
  323. throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
  324. }
  325. return Yii::createObject([
  326. 'class' => $this->dataColumnClass ? : TreeColumn::className(),
  327. 'grid' => $this,
  328. 'attribute' => $matches[1],
  329. 'format' => isset($matches[3]) ? $matches[3] : 'text',
  330. 'label' => isset($matches[5]) ? $matches[5] : null,
  331. ]);
  332. }
  333. /**
  334. * This function tries to guess the columns to show from the given data
  335. * if [[columns]] are not explicitly specified.
  336. */
  337. protected function guessColumns()
  338. {
  339. $models = $this->dataProvider->getModels();
  340. $model = reset($models);
  341. if (is_array($model) || is_object($model)) {
  342. foreach ($model as $name => $value) {
  343. $this->columns[] = $name;
  344. }
  345. }
  346. }
  347. /**
  348. * Normalize tree data
  349. * @param array $data
  350. * @param string $parentId
  351. * @return array
  352. */
  353. protected function normalizeData(array $data, $parentId = null) {
  354. $result = [];
  355. foreach ($data as $element) {
  356. if (ArrayHelper::getValue($element, $this->parentColumnName) === $parentId) {
  357. $result[] = $element;
  358. $children = $this->normalizeData($data, ArrayHelper::getValue($element, $this->keyColumnName));
  359. if ($children) {
  360. $result = array_merge($result, $children);
  361. }
  362. }
  363. }
  364. return $result;
  365. }
  366. }