dimanche 3 juillet 2016

Yii2 Radio button clicked select2 $model data change


Radiobutton expense and income The view defaults to expense and create select2 component. I want to change the select2 content when radio is clicked. I tried with jQuery. It'd be nice if you can show me examples that I can study.

Here are the codes. View
_form.php

<?=
        $form->field($model, 'transaction_type')
        ->radioList(['1' => 'Income', '0' => 'Expense'], [
            'unselect' => null,
            'item' => function ($index, $label, $name, $checked, $value) {
                $check = $checked ? ' checked="checked"' : '';
                return "<label class="radio-inline"><input type="radio" 
name="$name" value="$value" class="transaction" $check>$label</label>";
            }]);
?>
<?=
$form->field($model, 'category_id')->widget(Select2::classname(), [
    'data' => Category::getCategoryListByType($model->transaction_type),
    'options' => [
        'id' => 'category-id',
        'placeholder' => Yii::t('frontend/common', 'Select One'),
    ],
    'pluginOptions' => ['allowClear' => true]
]);
?>

Model

public static function getCategoryListByType($category_type) {
  $categorylist = Category::findAll(['category_type' => $category_type,]);
  return ArrayHelper::map($categorylist, 'id', 'category_name');
}

Aucun commentaire:

Enregistrer un commentaire