88namespace nullref \datatable ;
99
1010use nullref \datatable \assets \DataTableAsset ;
11+ use yii \base \Model ;
1112use yii \base \Widget ;
13+ use yii \data \ActiveDataProvider ;
14+ use yii \data \ArrayDataProvider ;
15+ use yii \db \ActiveQueryInterface ;
1216use yii \helpers \ArrayHelper ;
1317use yii \helpers \Html ;
1418use yii \helpers \Inflector ;
@@ -104,26 +108,44 @@ class DataTable extends Widget
104108 public $ globalVariable = false ;
105109 protected $ _options = [];
106110
111+ /**
112+ * @var \yii\data\DataProviderInterface the data provider for the view.
113+ */
114+ protected $ _dataProvider ;
115+
107116 protected $ _extraColumns = [];
108117
118+ /**
119+ * @throws \yii\base\InvalidConfigException
120+ * @throws \Exception if ArrayHelper::getValue()
121+ */
109122 public function init ()
110123 {
111124 parent ::init ();
125+ if ($ this ->data === null ) {
126+ $ this ->data = is_null ($ this ->_dataProvider ) ? [] : $ this ->_dataProvider ->getModels ();
127+ }
112128 DataTableAsset::register ($ this ->getView ());
113129 $ this ->initColumns ();
114130 $ this ->initData ();
115131 }
116132
133+ /**
134+ * @throws \yii\base\InvalidConfigException
135+ */
117136 protected function initColumns ()
118137 {
119138 $ this ->_extraColumns = $ this ->extraColumns ;
120139 if (isset ($ this ->_options ['columns ' ])) {
140+ $ demoObject = $ this ->getModel ();
121141 foreach ($ this ->_options ['columns ' ] as $ key => $ value ) {
122142 if (!is_array ($ value )) {
123143 $ value = [
124144 'class ' => DataTableColumn::class,
125145 'attribute ' => $ value ,
126- 'label ' => Inflector::camel2words ($ value )
146+ 'label ' => $ demoObject instanceof \yii \base \Model
147+ ? $ demoObject ->getAttributeLabel ($ value )
148+ : Inflector::camel2words ($ value )
127149 ];
128150 }
129151 if (isset ($ value ['type ' ])) {
@@ -147,6 +169,35 @@ protected function initColumns()
147169
148170 }
149171
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+ */
150201 private function initData ()
151202 {
152203 $ this ->_extraColumns = array_unique ($ this ->_extraColumns );
@@ -242,11 +293,17 @@ protected function getParams()
242293
243294 public function __get ($ name )
244295 {
296+ if ($ name == 'dataProvider ' ) {
297+ return $ this ->_dataProvider ;
298+ }
245299 return isset ($ this ->_options [$ name ]) ? $ this ->_options [$ name ] : null ;
246300 }
247301
248302 public function __set ($ name , $ value )
249303 {
304+ if ($ name == 'dataProvider ' ) {
305+ return $ this ->_dataProvider = $ value ;
306+ }
250307 return $ this ->_options [$ name ] = $ value ;
251308 }
252309
0 commit comments