@@ -58,14 +58,14 @@ class MultipleInput extends InputWidget
58
58
protected $ template ;
59
59
60
60
/**
61
- * @var array js templates collected from js which has been registered during rendering of widgets
61
+ * @var array js templates collected from js which has been registered during rendering of widget
62
62
*/
63
63
protected $ jsTemplates = [];
64
64
65
65
/**
66
66
* @var string
67
67
*/
68
- protected $ replacementKeys ;
68
+ private $ replacementKeys ;
69
69
70
70
/**
71
71
* Initialization.
@@ -194,79 +194,100 @@ protected function renderBody()
194
194
$ rows = [];
195
195
if (!empty ($ this ->data )) {
196
196
foreach ($ this ->data as $ index => $ data ) {
197
- $ rows [] = $ this ->renderRow ($ index , $ data );
197
+ $ rows [] = $ this ->renderRowContent ($ index , $ data );
198
198
}
199
199
} else {
200
- $ rows [] = $ this ->renderRow (0 );
200
+ $ rows [] = $ this ->renderRowContent (0 );
201
201
}
202
202
return Html::tag ('tbody ' , implode ("\n" , $ rows ));
203
203
}
204
204
205
- private function getRowTemplate ()
205
+ /**
206
+ * Renders the row content.
207
+ *
208
+ * @param int $index
209
+ * @param ActiveRecord|array $data
210
+ * @return mixed
211
+ * @throws InvalidConfigException
212
+ */
213
+ private function renderRowContent ($ index = null , $ data = null )
206
214
{
207
- if ( empty ( $ this -> template )) {
208
- $ cells = [];
209
- $ hiddenInputs = [];
210
- foreach ( $ this -> columns as $ columnIndex => $ column) {
211
- /* @var $column MultipleInputColumn */
215
+ $ cells = [];
216
+ $ hiddenInputs = [];
217
+ foreach ( $ this -> columns as $ columnIndex => $ column ) {
218
+ /* @var $ column MultipleInputColumn */
219
+ if ( is_null ( $ index )) {
212
220
$ value = 'multiple- ' . $ column ->name . '-value ' ;
213
221
$ this ->replacementKeys [$ value ] = $ column ->defaultValue ;
214
222
$ value = '{ ' . $ value . '} ' ;
215
-
216
- if ($ column ->isHiddenInput ()) {
217
- $ hiddenInputs [] = $ column ->renderCellContent ($ value );
218
- } else {
219
- $ cells [] = $ column ->renderCellContent ($ value );
220
- }
221
- }
222
- if (is_null ($ this ->limit ) || $ this ->limit > 1 ) {
223
- $ cells [] = $ this ->renderActionColumn ();
223
+ } else {
224
+ $ value = $ column ->prepareValue ($ data );
224
225
}
225
226
226
- if (!empty ($ hiddenInputs )) {
227
- $ hiddenInputs = implode ("\n" , $ hiddenInputs );
228
- $ cells [0 ] = preg_replace ('/^(<td[^>]+>)(.*)(<\/td>)$/ ' , '${1} ' . $ hiddenInputs . '$2$3 ' , $ cells [0 ]);
227
+ if ($ column ->isHiddenInput ()) {
228
+ $ hiddenInputs [] = $ column ->renderCellContent ($ value , $ index );
229
+ } else {
230
+ $ cells [] = $ column ->renderCellContent ($ value , $ index );
229
231
}
232
+ }
233
+ if (is_null ($ this ->limit ) || $ this ->limit > 1 ) {
234
+ $ cells [] = $ this ->renderActionColumn ($ index );
235
+ }
230
236
231
- $ this ->template = Html::tag ('tr ' , implode ("\n" , $ cells ), [
232
- 'class ' => 'multiple-input-list__item ' ,
233
- ]);
237
+ if (!empty ($ hiddenInputs )) {
238
+ $ hiddenInputs = implode ("\n" , $ hiddenInputs );
239
+ $ cells [0 ] = preg_replace ('/^(<td[^>]+>)(.*)(<\/td>)$/ ' , '${1} ' . $ hiddenInputs . '$2$3 ' , $ cells [0 ]);
240
+ }
234
241
235
- if (is_array ($ this ->getView ()->js ) && array_key_exists (View::POS_READY , $ this ->getView ()->js )) {
236
- $ this ->collectJsTemplates ();
237
- }
242
+ $ content = Html::tag ('tr ' , implode ("\n" , $ cells ), [
243
+ 'class ' => 'multiple-input-list__item ' ,
244
+ ]);
245
+
246
+ if (is_null ($ index )) {
247
+ $ this ->collectJsTemplates ();
238
248
}
239
249
240
- return $ this -> template ;
250
+ return $ content ;
241
251
}
242
252
243
253
private function collectJsTemplates ()
244
254
{
255
+ if (is_array ($ this ->getView ()->js ) && array_key_exists (View::POS_READY , $ this ->getView ()->js )) {
245
256
$ this ->jsTemplates = [];
246
257
foreach ($ this ->getView ()->js [View::POS_READY ] as $ key => $ js ) {
247
- if (preg_match ('/\(.#[^)]+{multiple-index}[^)]+\)/ ' , $ js ) === 1 ) {
248
- $ this ->jsTemplates [] = $ js ;
249
- unset($ this ->getView ()->js [View::POS_READY ][$ key ]);
258
+ if (preg_match ('/^.* ' . $ this ->options ['id ' ] . '-{multiple-index}.*$/ ' , $ js ) === 1 ) {
259
+ $ this ->jsTemplates [] = $ js ;
260
+ unset($ this ->getView ()->js [View::POS_READY ][$ key ]);
261
+ }
250
262
}
251
263
}
252
264
}
253
265
254
266
/**
255
267
* Renders the action column.
256
268
*
269
+ * @param null|int $index
257
270
* @return string
258
271
* @throws \Exception
259
272
*/
260
- private function renderActionColumn ()
273
+ private function renderActionColumn ($ index = null )
261
274
{
275
+ if (is_null ($ index )) {
276
+ $ action = '{multiple-btn-action} ' ;
277
+ $ type = '{multiple-btn-type} ' ;
278
+ } else {
279
+ $ action = $ index == 0 ? self ::ACTION_ADD : self ::ACTION_REMOVE ;
280
+ $ type = $ index == 0 ? 'btn-default ' : 'btn-danger ' ;
281
+ }
282
+
262
283
$ button = Button::widget (
263
284
[
264
285
'tagName ' => 'div ' ,
265
286
'encodeLabel ' => false ,
266
- 'label ' => Html::tag ('i ' , null , ['class ' => 'glyphicon glyphicon-{multiple-btn-action} ' ]),
287
+ 'label ' => Html::tag ('i ' , null , ['class ' => 'glyphicon glyphicon- ' . $ action ]),
267
288
'options ' => [
268
- 'id ' => $ this ->getElementId ('button ' ),
269
- 'class ' => " {multiple-btn- type} multiple-input-list__btn btn js-input-{multiple-btn- action} " ,
289
+ 'id ' => $ this ->getElementId ('button ' , $ index ),
290
+ 'class ' => $ type . ' multiple-input-list__btn btn js-input-' . $ action ,
270
291
]
271
292
]
272
293
);
@@ -275,51 +296,21 @@ private function renderActionColumn()
275
296
]);
276
297
}
277
298
278
- /**
279
- * Renders the row.
280
- *
281
- * @param int $index
282
- * @param ActiveRecord|array $data
283
- * @return mixed
284
- * @throws InvalidConfigException
285
- */
286
- private function renderRow ($ index , $ data = null )
287
- {
288
- $ btnAction = $ index == 0 ? self ::ACTION_ADD : self ::ACTION_REMOVE ;
289
- $ btnType = $ index == 0 ? 'btn-default ' : 'btn-danger ' ;
290
- $ search = ['{multiple-index} ' , '{multiple-btn-action} ' , '{multiple-btn-type} ' ];
291
- $ replace = [$ index , $ btnAction , $ btnType ];
292
-
293
- foreach ($ this ->columns as $ column ) {
294
- /* @var $column MultipleInputColumn */
295
- $ search [] = '{multiple- ' . $ column ->name . '-value} ' ;
296
- $ replace [] = $ column ->prepareValue ($ data );
297
- }
298
-
299
- $ row = str_replace ($ search , $ replace , $ this ->getRowTemplate ());
300
-
301
- foreach ($ this ->jsTemplates as $ js ) {
302
- $ this ->getView ()->registerJs (strtr ($ js , ['{multiple-index} ' => $ index ]), View::POS_READY );
303
- }
304
- return $ row ;
305
- }
306
299
307
300
/**
308
301
* Returns element's name.
309
302
*
310
- * @param string $name
311
- * @param string $index
303
+ * @param string $name the name of cell element
304
+ * @param int|null $index
312
305
* @return string
313
306
*/
314
- public function getElementName ($ name , $ index = null )
307
+ public function getElementName ($ name , $ index )
315
308
{
316
- if ($ index === null ) {
309
+ if (is_null ( $ index) ) {
317
310
$ index = '{multiple-index} ' ;
318
311
}
319
312
return $ this ->getInputNamePrefix ($ name ) . (
320
- count ($ this ->columns ) > 1
321
- ? '[ ' . $ index . '][ ' . $ name . '] '
322
- : '[ ' . $ name . '][ ' . $ index . '] '
313
+ count ($ this ->columns ) > 1 ? '[ ' . $ index . '][ ' . $ name . '] ' : '[ ' . $ name . '][ ' . $ index . '] '
323
314
);
324
315
}
325
316
@@ -343,12 +334,13 @@ private function getInputNamePrefix($name)
343
334
/**
344
335
* Returns element id.
345
336
*
346
- * @param $name
337
+ * @param string $name
338
+ * @param null|int $index
347
339
* @return mixed
348
340
*/
349
- public function getElementId ($ name )
341
+ public function getElementId ($ name, $ index = null )
350
342
{
351
- return $ this ->normalize ($ this ->getElementName ($ name ));
343
+ return $ this ->normalize ($ this ->getElementName ($ name, $ index ));
352
344
}
353
345
354
346
/**
@@ -371,7 +363,7 @@ public function registerClientScript()
371
363
$ options = Json::encode (
372
364
[
373
365
'id ' => $ this ->getId (),
374
- 'template ' => $ this ->getRowTemplate (),
366
+ 'template ' => $ this ->renderRowContent (),
375
367
'jsTemplates ' => $ this ->jsTemplates ,
376
368
'btnAction ' => self ::ACTION_REMOVE ,
377
369
'btnType ' => 'btn-danger ' ,
0 commit comments