forked from mdmsoft/yii2-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridInput.php
188 lines (172 loc) · 5.08 KB
/
GridInput.php
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
<?php
namespace mdm\widgets;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\Model;
use yii\base\InvalidConfigException;
/**
* Description of GridInput
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class GridInput extends TabularWidget
{
/**
* @inheritdoc
*/
public $tag = 'table';
/**
* @var Column[]
*/
public $columns = [];
/**
* @var array
*/
public $hiddens = [];
/**
*
* @var string
*/
public $header;
/**
*
* @var string
*/
public $footer;
/**
*
* @var array
*/
public $headerOptions = [];
/**
*
* @var array
*/
public $footerOptions = [];
/**
*
* @var string
*/
public $defaultColumnClass = 'mdm\widgets\DataColumn';
/**
* @inheritdoc
*/
public $options = ['class' => 'table table-striped'];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!($this->model instanceof Model)) {
$property = __CLASS__ . '::$model';
throw new InvalidConfigException("Value of \"{$property}\" must be specified.");
}
foreach ($this->columns as $i => $column) {
if (is_string($column)) {
$column = Yii::createObject([
'class' => $this->defaultColumnClass,
'attribute' => $column,
'grid' => $this,
]);
} elseif (is_array($column)) {
if (!isset($column['class'])) {
$column['class'] = $this->defaultColumnClass;
}
$column['grid'] = $this;
$column = Yii::createObject($column);
}
$this->columns[$i] = $column;
}
$this->containerOptions['tag'] = 'tbody';
$this->clientOptions = array_merge([
'container' => "tbody.mdm-container{$this->level}",
'itemSelector' => "tr.mdm-item{$this->level}"
], $this->clientOptions);
Html::addCssClass($this->itemOptions, "mdm-item{$this->level}");
Html::addCssClass($this->containerOptions, "mdm-container{$this->level}");
}
/**
* @inheritdoc
*/
public function renderHeader()
{
if ($this->header === false) {
return '';
}
if ($this->header === null) {
$cols = [];
foreach ($this->columns as $column) {
$cols[] = $column->renderHeaderCell();
}
if (count($this->hiddens)) {
$cols[] = '<th style="display:none;" class"hidden-col"></th>';
}
$rows = [Html::tag('tr', implode("\n", $cols), $this->headerOptions)];
} else {
$rows = [];
foreach ((array) $this->header as $header) {
$rows[] = Html::tag('tr', $header, $this->headerOptions);
}
}
return Html::tag('thead', implode("\n", $rows));
}
/**
* @inheritdoc
*/
public function renderItem($model, $key, $index)
{
$cols = [];
/* @var $column Column */
foreach ($this->columns as $column) {
$cols[] = $column->renderDataCell($model, $key, $index);
}
if (count($this->hiddens)) {
$hiddens = [];
foreach ($this->hiddens as $options) {
if (is_string($options)) {
$attribute = $options;
$options = [];
} else {
$attribute = ArrayHelper::remove($options, 'attribute');
}
$field = str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $attribute);
$options['data-field'] = $field;
$options['id'] = false;
$hiddens[] = Html::activeHiddenInput($model, "[$key]{$attribute}", $options);
}
$cols[] = Html::tag('td', implode("\n", $hiddens), ['style' => ['display' => 'none'], 'class' => 'hidden-col']);
}
$options = $this->itemOptions;
$options['data-key'] = (string) $key;
$options['data-index'] = (string) $index;
return Html::tag('tr', implode("\n", $cols), $options);
}
/**
* @inheritdoc
*/
public function renderFooter()
{
if ($this->footer === false) {
return '';
}
if ($this->footer === null) {
$cols = [];
foreach ($this->columns as $column) {
$cols[] = $column->renderFooterCell();
}
if (count($this->hiddens)) {
$cols[] = '<td style="display:none;" class"hidden-col"></td>';
}
$rows = [Html::tag('tr', implode("\n", $cols), $this->footerOptions)];
} else {
$rows = [];
foreach ((array) $this->footer as $footer) {
$rows[] = Html::tag('tr', $footer, $this->footerOptions);
}
}
return Html::tag('tfoot', implode("\n", $rows));
}
}