@@ -84,9 +84,11 @@ public function run()
84
84
echo Html::beginTag ('table ' , [
85
85
'class ' => 'multiple-input-list table table-condensed '
86
86
]);
87
+
87
88
if ($ this ->hasHeader ()) {
88
89
$ this ->renderHeader ();
89
90
}
91
+
90
92
echo Html::beginTag ('tbody ' );
91
93
if (!empty ($ this ->data )) {
92
94
foreach ($ this ->data as $ index => $ data ) {
@@ -96,8 +98,10 @@ public function run()
96
98
$ this ->renderRow (0 );
97
99
}
98
100
echo Html::endTag ('tbody ' );
101
+
99
102
echo Html::endTag ('table ' );
100
103
echo Html::endTag ('div ' );
104
+
101
105
$ this ->registerClientScript ();
102
106
}
103
107
/**
@@ -131,6 +135,7 @@ private function renderHeader()
131
135
echo Html::endTag ('tr ' );
132
136
echo Html::endTag ('thead ' );
133
137
}
138
+
134
139
/**
135
140
* Check that at least one column has a header.
136
141
*
@@ -145,6 +150,7 @@ private function hasHeader()
145
150
}
146
151
return false ;
147
152
}
153
+
148
154
private function getRowTemplate ()
149
155
{
150
156
if (empty ($ this ->template )) {
@@ -180,17 +186,17 @@ private function getRowTemplate()
180
186
$ options ['selectedOption ' ] = $ value ;
181
187
$ this ->template .= Html::$ type ($ name , null , $ column ['items ' ], $ options );
182
188
break ;
183
- case 'custom ' :
184
- $ this ->template .= $ value ;
185
- break ;
186
189
default :
187
190
if (method_exists ('yii\helpers\Html ' , $ type )) {
188
191
$ this ->template .= Html::$ type ($ name , $ value , $ options );
192
+ // TODO https://github.com/unclead/yii2-multiple-input/issues/1
193
+ /*
189
194
} elseif (class_exists($type) && method_exists($type, 'widget')) {
190
195
$this->template .= $type::widget(array_merge($options, [
191
196
'name' => $name,
192
197
'value' => $value,
193
198
]));
199
+ */
194
200
} else {
195
201
throw new InvalidConfigException ("Invalid column type ' $ type' " );
196
202
}
@@ -236,13 +242,15 @@ private function renderRow($index, $data = null)
236
242
$ btnType = $ index == 0 ? 'btn-default ' : 'btn-danger ' ;
237
243
$ search = ['{index} ' , '{btn_action} ' , '{btn_type} ' ];
238
244
$ replace = [$ index , $ btnAction , $ btnType ];
245
+
239
246
foreach ($ this ->getColumns () as $ column ) {
240
247
if (!array_key_exists ('name ' , $ column )) {
241
248
throw new InvalidConfigException ("The 'name' option is required. " );
242
249
}
243
250
$ search [] = '{ ' . $ column ['name ' ] . '_value} ' ;
244
251
$ replace [] = $ this ->prepareColumnValue ($ column , $ data );
245
252
}
253
+
246
254
echo str_replace ($ search , $ replace , $ this ->getRowTemplate ());
247
255
}
248
256
@@ -258,6 +266,8 @@ private function getColumns()
258
266
}
259
267
260
268
/**
269
+ * Preparing column's value.
270
+ *
261
271
* @param $column
262
272
* @param $data
263
273
* @return mixed
@@ -284,7 +294,9 @@ private function prepareColumnValue($column, $data)
284
294
}
285
295
286
296
/**
287
- * @param $name
297
+ * Returns element's name.
298
+ *
299
+ * @param string $name
288
300
* @param string $index
289
301
* @return string
290
302
*/
@@ -293,25 +305,33 @@ private function getElementName($name, $index = null)
293
305
if ($ index === null ) {
294
306
$ index = '{index} ' ;
295
307
}
296
- return $ this ->getAttributeName ( ) . (
308
+ return $ this ->getInputNamePrefix ( $ name ) . (
297
309
count ($ this ->columns ) > 1
298
310
? '[ ' . $ index . '][ ' . $ name . '] '
299
311
: '[ ' . $ name . '][ ' . $ index . '] '
300
312
);
301
313
}
302
314
303
315
/**
316
+ * Return prefix for name of input.
317
+ *
318
+ * @param string $name input name
304
319
* @return string
305
320
*/
306
- private function getAttributeName ( )
321
+ private function getInputNamePrefix ( $ name )
307
322
{
308
323
if ($ this ->hasModel ()) {
309
- return empty ($ this ->columns ) ? $ this ->model ->formName () : Html::getInputName ($ this ->model , $ this ->attribute );
324
+ if (empty ($ this ->columns ) || (count ($ this ->columns ) == 1 && $ this ->model ->hasProperty ($ name ))) {
325
+ return $ this ->model ->formName ();
326
+ }
327
+ return Html::getInputName ($ this ->model , $ this ->attribute );
310
328
}
311
329
return $ this ->name ;
312
330
}
313
331
314
332
/**
333
+ * Returns element id.
334
+ *
315
335
* @param $name
316
336
* @return mixed
317
337
*/
@@ -321,6 +341,8 @@ private function getElementId($name)
321
341
}
322
342
323
343
/**
344
+ * Normalization name.
345
+ *
324
346
* @param $name
325
347
* @return mixed
326
348
*/
0 commit comments