TreeGridWidget.php
9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace common\components\artboxtree\treegrid;
use common\modules\rubrication\models\TaxOption;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
class TreeGridWidget extends \common\components\artboxtree\ArtboxTreeWidget {
/**
* @var array grid column configuration. Each array element represents the configuration
* for one particular grid column.
* @see \yii\grid::$columns for details.
*/
public $columns = [];
/**
* @var string the default data column class if the class name is not explicitly specified when configuring a data column.
* Defaults to 'leandrogehlen\treegrid\TreeGridColumn'.
*/
public $dataColumnClass;
/**
* @var array the HTML attributes for the container tag of the grid view.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'table table-striped table-bordered'];
/**
* @var array The plugin options
*/
public $pluginOptions = [];
/**
* @var boolean whether to show the grid view if [[dataProvider]] returns no data.
*/
public $showOnEmpty = true;
public $rowOptions = [];
/**
* @var Closure an anonymous function that is called once BEFORE rendering each data model.
* It should have the similar signature as [[rowOptions]]. The return result of the function
* will be rendered directly.
*/
public $beforeRow;
/**
* @var Closure an anonymous function that is called once AFTER rendering each data model.
* It should have the similar signature as [[rowOptions]]. The return result of the function
* will be rendered directly.
*/
public $afterRow;
/**
* @var boolean whether to show the header section of the grid table.
*/
public $showHeader = true;
/**
* @var array the HTML attributes for the table header row.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $headerRowOptions = [];
/**
* @var boolean whether to show the footer section of the grid table.
*/
public $showFooter = false;
/**
* @var string the HTML display when the content of a cell is empty
*/
public $emptyCell = ' ';
public $levelSymbol = '–';
/**
* Init the widget object.
*/
public function init() {
parent::init();
$this->initColumns();
}
/**
* Runs the widget.
*/
public function run() {
$run = parent::run();
if (!is_null($run))
return $run;
if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
$pagination = $this->dataProvider->getPagination();
$pagination->setPageSize($this->dataProvider->getTotalCount());
$header = $this->showHeader ? $this->renderTableHeader() : false;
$body = $this->renderItems();
$footer = $this->showFooter ? $this->renderTableFooter() : false;
$content = array_filter([
$header,
$body,
$footer
]);
return Html::tag('table', implode("\n", $content), $this->options);
} else {
return $this->renderEmptyResult();
}
}
/**
* Renders the table header.
* @return string the rendering result.
*/
public function renderTableHeader()
{
$cells = [];
foreach ($this->columns as $column) {
/* @var $column TreeGridColumn */
$cells[] = $column->renderHeaderCell();
}
$content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);
return "<thead>\n" . $content . "\n</thead>";
}
/**
* Renders the table footer.
* @return string the rendering result.
*/
public function renderTableFooter()
{
$cells = [];
foreach ($this->columns as $column) {
/* @var $column TreeGridColumn */
$cells[] = $column->renderFooterCell();
}
$content = Html::tag('tr', implode('', $cells), $this->footerRowOptions);
return "<tfoot>\n" . $content . "\n</tfoot>";
}
/**
* Renders the data models for the grid view.
*/
public function renderItems()
{
$rows = [];
$models = array_values($this->dataProvider->getModels());
$keys = $this->dataProvider->getKeys();
$models = TaxOption::find()->normalizeTreeData($models, $this->rootParentId);
foreach ($models as $index => $model) {
$key = $keys[$index];
if ($this->beforeRow !== null) {
$row = call_user_func($this->beforeRow, $model, $key, $index, $this);
if (!empty($row)) {
$rows[] = $row;
}
}
$rows[] = $this->renderTableRow($model, $key, $index);
if ($this->afterRow !== null) {
$row = call_user_func($this->afterRow, $model, $key, $index, $this);
if (!empty($row)) {
$rows[] = $row;
}
}
}
if (empty($rows)) {
$colspan = count($this->columns);
return "<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>";
} else {
return implode("\n", $rows);
}
}
/**
* Renders a table row with the given data model and key.
* @param mixed $model the data model to be rendered
* @param mixed $key the key associated with the data model
* @param integer $index the zero-based index of the data model among the model array returned by [[dataProvider]].
* @return string the rendering result
*/
public function renderTableRow($model, $key, $index)
{
$cells = [];
/* @var $column TreeGridColumn */
$i = 0;
foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($model, $key, $index, $i == 0, $this->levelSymbol);
$i++;
}
if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
} else {
$options = $this->rowOptions;
}
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
$id = ArrayHelper::getValue($model, $this->keyNameId);
Html::addCssClass($options, "treegrid-$id");
$parentId = ArrayHelper::getValue($model, $this->keyNameParentId);
if ($parentId) {
Html::addCssClass($options, "treegrid-parent-$parentId");
}
return Html::tag('tr', implode('', $cells), $options);
}
/**
* Creates column objects and initializes them.
*/
protected function initColumns()
{
if (empty($this->columns)) {
$this->guessColumns();
}
foreach ($this->columns as $i => $column) {
if (is_string($column)) {
$column = $this->createDataColumn($column);
} else {
$column = Yii::createObject(array_merge([
'class' => $this->dataColumnClass ? : TreeGridColumn::className(),
'grid' => $this,
], $column));
}
if (!$column->visible) {
unset($this->columns[$i]);
continue;
}
$this->columns[$i] = $column;
}
}
/**
* Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label".
* @param string $text the column specification string
* @return DataColumn the column instance
* @throws InvalidConfigException if the column specification is invalid
*/
protected function createDataColumn($text)
{
if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
}
return Yii::createObject([
'class' => $this->dataColumnClass ? : TreeGridColumn::className(),
'grid' => $this,
'attribute' => $matches[1],
'format' => isset($matches[3]) ? $matches[3] : 'text',
'label' => isset($matches[5]) ? $matches[5] : null,
]);
}
/**
* This function tries to guess the columns to show from the given data
* if [[columns]] are not explicitly specified.
*/
protected function guessColumns()
{
$models = $this->dataProvider->getModels();
$model = reset($models);
if (is_array($model) || is_object($model)) {
foreach ($model as $name => $value) {
$this->columns[] = $name;
}
}
}
}