I am using yii2-formwizard which is a handy tool in my project with kartik\select2
. Everything is work perfectly except when I press add to get next group select2
drop-down on previous groups are disappeared.
This has happen when I modify my controller to capture data from my model as explained in my previous post, I have missed something in scripting side I'm bit poor in jquery/JS etc, anyhow except that data is being saved and widget work perfectly
my controller
<?php
public function actionCreatemulti()
{
$this->layout = 'layout2';
$model = [new Todelete()];
$sex = [['id' => 1, 'name' => 'male'], ['id' => 2, 'name' => 'female']];
if (Yii::$app->request->isPost) {
$count = count(Yii::$app->request->post('Todelete', []));
//start the loop from 1 rather than 0 and use the $count for limit
for ($i = 1; $i < $count; $i++) {
$model[] = new Todelete();
}
if (Model::loadMultiple($model, Yii::$app->request->post()) && Model::validateMultiple($model)) {
foreach ($model as $md) {
$md->save(false);
}
return $this->render('index');
}
}
return $this->render('create', [
'model' => $model,
'sex' => $sex
]);
}
my view
echo FormWizard::widget(
[
'formOptions' => [
'id' => 'my_form_tabular'
],
'steps' => [
[
//should be a single model or array of Activerecord model objects but for a single model only see wiki on github
'model' => $model,
//set step type to tabular
'type' => FormWizard::STEP_TYPE_TABULAR,
'fieldConfig' => [
'sex' => [
'widget' => Select2::class,
'containerOptions' => [
'class' => 'form-group'
],
'options' => [
'data' => $data,
'options' => [
'class' => 'form-control'
],
'theme' => Select2::THEME_BOOTSTRAP,
'pluginOptions' => [
'allowClear' => true,
'placeholder' => 'Select sex'
]
],
//set tabular events for select2 fix which doesnot work correctly after cloning
'tabularEvents' => [
'beforeClone' => "function(event, params){
//fix for select2 destroy the plugin
let element = $(this);
element.select2('destroy');
}",
"afterClone" => "function(event, params){
//bind select2 again after clone
let element = $(this);
let elementId = $(this).attr('id');
let dataKrajee = eval(element.data('krajee-select2'));
let dataSelect2Options = element.data('s2-options');
$.when(element.select2(dataKrajee)).done(initS2Loading(elementId, dataSelect2Options));
}",
"afterInsert" => "function(event,params){
//initialize the options for the select2 after initializing
//changed according to my environment
let selectElement = $(this).find('.field-todelete-'+params.rowIndex+'-sex > select');
let dataKrajee = eval(selectElement.data('krajee-select2'));
selectElement.select2(dataKrajee);
}"
]
]
]
]
]
]
);
https://cdn1.imggmi.com/uploads/2019/8/31/158dc0f338e0d780747c5f72fa2ed6bb-full.png https://cdn1.imggmi.com/uploads/2019/8/31/4e394e87aa162d3f457c32af8d30373b-full.png