8
8
namespace nullref \datatable ;
9
9
10
10
use nullref \datatable \assets \DataTableAsset ;
11
+ use yii \base \Model ;
11
12
use yii \base \Widget ;
13
+ use yii \data \ActiveDataProvider ;
14
+ use yii \data \ArrayDataProvider ;
15
+ use yii \db \ActiveQueryInterface ;
12
16
use yii \helpers \ArrayHelper ;
13
17
use yii \helpers \Html ;
14
18
use yii \helpers \Inflector ;
@@ -104,26 +108,44 @@ class DataTable extends Widget
104
108
public $ globalVariable = false ;
105
109
protected $ _options = [];
106
110
111
+ /**
112
+ * @var \yii\data\DataProviderInterface the data provider for the view.
113
+ */
114
+ protected $ _dataProvider ;
115
+
107
116
protected $ _extraColumns = [];
108
117
118
+ /**
119
+ * @throws \yii\base\InvalidConfigException
120
+ * @throws \Exception if ArrayHelper::getValue()
121
+ */
109
122
public function init ()
110
123
{
111
124
parent ::init ();
125
+ if ($ this ->data === null ) {
126
+ $ this ->data = is_null ($ this ->_dataProvider ) ? [] : $ this ->_dataProvider ->getModels ();
127
+ }
112
128
DataTableAsset::register ($ this ->getView ());
113
129
$ this ->initColumns ();
114
130
$ this ->initData ();
115
131
}
116
132
133
+ /**
134
+ * @throws \yii\base\InvalidConfigException
135
+ */
117
136
protected function initColumns ()
118
137
{
119
138
$ this ->_extraColumns = $ this ->extraColumns ;
120
139
if (isset ($ this ->_options ['columns ' ])) {
140
+ $ demoObject = $ this ->getModel ();
121
141
foreach ($ this ->_options ['columns ' ] as $ key => $ value ) {
122
142
if (!is_array ($ value )) {
123
143
$ value = [
124
144
'class ' => DataTableColumn::class,
125
145
'attribute ' => $ value ,
126
- 'label ' => Inflector::camel2words ($ value )
146
+ 'label ' => $ demoObject instanceof \yii \base \Model
147
+ ? $ demoObject ->getAttributeLabel ($ value )
148
+ : Inflector::camel2words ($ value )
127
149
];
128
150
}
129
151
if (isset ($ value ['type ' ])) {
@@ -147,6 +169,35 @@ protected function initColumns()
147
169
148
170
}
149
171
172
+ /**
173
+ * Detect a model class from `dataProvider` or `data` attributes
174
+ *
175
+ * @see \yii\grid\DataColumn::getHeaderCellLabel()
176
+ *
177
+ * return Model|null NULL is returned when only property $data is defined, and is either empty or first entry is not of type model
178
+ */
179
+ protected function getModel ()
180
+ {
181
+ $ provider = $ this ->_dataProvider ;
182
+ if ($ provider instanceof ActiveDataProvider && $ provider ->query instanceof ActiveQueryInterface) {
183
+ /* @var $modelClass Model */
184
+ $ modelClass = $ provider ->query ->modelClass ;
185
+ $ model = $ modelClass ::instance ();
186
+ } elseif ($ provider instanceof ArrayDataProvider && $ provider ->modelClass !== null ) {
187
+ /* @var $modelClass Model */
188
+ $ modelClass = $ provider ->modelClass ;
189
+ $ model = $ modelClass ::instance ();
190
+ } else {
191
+ $ models = $ this ->data ;
192
+ //$model = (count($models)) ? $models[0] : null;
193
+ $ model = reset ($ models );
194
+ }
195
+ return $ model instanceof Model ? $ model : null ;
196
+ }
197
+
198
+ /**
199
+ * @throws \Exception if ArrayHelper::getValue() throws
200
+ */
150
201
private function initData ()
151
202
{
152
203
$ this ->_extraColumns = array_unique ($ this ->_extraColumns );
@@ -242,11 +293,17 @@ protected function getParams()
242
293
243
294
public function __get ($ name )
244
295
{
296
+ if ($ name == 'dataProvider ' ) {
297
+ return $ this ->_dataProvider ;
298
+ }
245
299
return isset ($ this ->_options [$ name ]) ? $ this ->_options [$ name ] : null ;
246
300
}
247
301
248
302
public function __set ($ name , $ value )
249
303
{
304
+ if ($ name == 'dataProvider ' ) {
305
+ return $ this ->_dataProvider = $ value ;
306
+ }
250
307
return $ this ->_options [$ name ] = $ value ;
251
308
}
252
309
0 commit comments