Dependent Double DropDown on Yii2

1

How can I solve this implementation below, so that it executes the 2 dependency routines associated with the same onChange event:

Example:

<?php echo $form->field($model, 'id_data_type', [
        'hintType' => ActiveField::HINT_SPECIAL,
        'hintSettings' => [
            'placement' => 'right',
            'iconBesideInput' => false,
            'onLabelClick' => true,
            'onLabelHover' => false,
            'onIconClick' => true,
            'onIconHover' => false,
            'title' => '<i class="glyphicon glyphicon-info-sign">'.Yii::t('app','Tipo de Dado')
        ]])->dropDownList(\yii\helpers\ArrayHelper::map(DataTypes::find()->asArray()->all(),'id_data_type','name'),
        ['prompt' => Yii::t('app', 'Selecione opção'),
            'onchange'=>'$.get( "'. \yii\helpers\Url::toRoute('dependent/getviewcomponentinput').'", { id: $(this).val() } ).done(function( data ) { $( "#'.Html::getInputId($model,'id_data_view_component_input').'" ).html( data ); } );',
            'onchange'=>'$.get( "'. \yii\helpers\Url::toRoute('dependent/getviewcomponentoutput').'", { id: $(this).val() } ).done(function( data ) { $( "#'.Html::getInputId($model,'id_data_view_component_output').'" ).html( data ); } );',
        ]);
    ?>


<?php echo $form->field($model, 'id_data_view_component_input', [
        'hintType' => ActiveField::HINT_SPECIAL,
        'hintSettings' => [
            'enableAjaxValidation' => true,
            'placement' => 'right',
            'iconBesideInput' => false,
            'onLabelClick' => true,
            'onLabelHover' => false,
            'onIconClick' => true,
            'onIconHover' => false,
            'title' => '<i class="glyphicon glyphicon-info-sign">'.Yii::t('app','Componente Visual de Entrada')
        ]])->dropDownList([],['prompt' => Yii::t('app', 'Selecione Opção')]);
    ?>


    <?php echo $form->field($model, 'id_data_view_component_output', [
            'hintType' => ActiveField::HINT_SPECIAL,
            'hintSettings' => [
                'enableAjaxValidation' => true,
                'placement' => 'right',
                'iconBesideInput' => false,
                'onLabelClick' => true,
                'onLabelHover' => false,
                'onIconClick' => true,
                'onIconHover' => false,
                'title' => '<i class="glyphicon glyphicon-info-sign">'.Yii::t('app','Componente Visual de Saída')
            ]])->dropDownList([],['prompt' => Yii::t('app', 'Selecione Opção')]);
    ?>

class DependentController extends \yii\web\Controller
{
    public function actionGetviewcomponentinput($id)
    {
        switch ($id) {
            case 1: // Boolean

                $types = ComponentDataView::find()
                    ->where(['input_representation'=>true,'boolean_representation' => true])
                    ->all();
                break;

            case 2: // Integer

                $types = ComponentDataView::find()
                    ->where(['input_representation'=>true,'integer_representation' => true])
                    ->all();
                break;
            case 3: // String

                $types = ComponentDataView::find()
                    ->where(['input_representation'=>true,'string_representation' => true])
                    ->all();
                break;
            case 4: // Float

                $types = ComponentDataView::find()
                    ->where(['input_representation'=>true,'float_representation' => true])
                    ->all();
                break;
        }

        if (!empty($types)) {
            foreach ($types as $type) {
                echo "<option value='" . $type->id_data_view_component . "'>" . $type->name . "</option>";
            }
        } else {
            echo "<option>-</option>";
        }
    }

    public function actionGetviewcomponentoutput($id)
    {
        switch ($id) {
            case 1: // Boolean

                $types = ComponentDataView::find()
                    ->where(['output_representation'=>true,'boolean_representation' => true])
                    ->all();
                break;

            case 2: // Integer

                $types = ComponentDataView::find()
                    ->where(['output_representation'=>true,'integer_representation' => true])
                    ->all();
                break;
            case 3: // String

                $types = ComponentDataView::find()
                    ->where(['output_representation'=>true,'string_representation' => true])
                    ->all();
                break;
            case 4: // Float

                $types = ComponentDataView::find()
                    ->where(['output_representation'=>true,'float_representation' => true])
                    ->all();
                break;

        }

        if (!empty($types)) {
            foreach ($types as $type) {
                echo "<option value='" . $type->id_data_view_component . "'>" . $type->name . "</option>";
            }
        } else {
            echo "<option>-</option>";
        }
    }

}

What I need: When selecting the DropDownList from the / strong> and view_add_component_output routines, through the actionGetviewcomponentinput and actionGetviewcomponentoutput .     

asked by anonymous 19.03.2018 / 14:17

1 answer

0

If I understand you correctly, do you want to call the two routines (input and output) in the same event?

If this is the case, in your example you are registering two events of "onChange", causing the second to overwrite the first and be the only one called. The correct one is to send the two gets and calls the two routines in the same event (as in the code below).

If you need to call one event after another, use the "success" callbacks for the first routine and "done" for the second.

['prompt' => Yii::t('app', 'Selecione opção'),
'onchange' => '
    $.get("'.\yii\helpers\Url::toRoute('dependent/getviewcomponentinput').'", { id: $(this).val() } ).done(function( data ) { $( "#'.Html::getInputId($model,'id_data_view_component_input').'").html(data);});
    $.get("'.\yii\helpers\Url::toRoute('dependent/getviewcomponentoutput').'", { id: $(this).val() } ).done(function( data ) { $( "#'.Html::getInputId($model,'id_data_view_component_output').'").html(data);});
',
]);
    
14.08.2018 / 14:51