Yii Framework Forum

Get id from Select2 widget


(hobbit2324) #1

Hi everyone i’m trying to return de id from a select2 widget and also i’m using an array map to search my model, what i need to do, it’s return the selected id value, because i’m using that id to do another search, but i’m dont know how do it, can you help me thanks.

there’s my select2 search:

<?=  $form->field($model, 'id_plantilla')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(Plantilla::find()->all(),'id_plantilla','nom_guia'),
                'language' => 'es',
                'options' => ['placeholder' => 'seleccione','id'=>'codigo'],
                'pluginOptions' => [
                    'allowClear' => true
                ],
                ])->label(false); ?>

and my another search

$modelsplantilla = Plantilla::find()->where(['id_padre' => $id_padre])->orderBy('id_plantilla')->all();

where id_padre is id_plantilla selected value.


(Alirz23) #2

you have to use javascript to listen for changes on select2 and then get the value of selected option

// something like
$('#select_input_id').on("change", function (e) { 
    console.log($(this).val()); 
});

(hobbit2324) #3

Thanks alirz23 i can see the id value from select2, but i dont know how to pass the id value, and use it on my search:
$modelsplantilla = Plantilla::find()->where([‘id_padre’ => $id_padre])->orderBy(‘id_plantilla’)->all();
where id_plantilla = id returned from selected value


(Jaimez) #4

The way how to pass id as $id_padre depends on the location of $modelsplantilla= Plantilla::find...

Is it in the controller action which is fired after form submit? Another controller or Model ?


(hobbit2324) #5

I’ts in the same view beacuse i use the search function to create a table, my code:

my code:
    <td colspan="2" valign="top"> <?=  $form->field($model, 'id_plantilla')->widget(Select2::classname(), [
            'data' => ArrayHelper::map(Plantilla::find()->all(),'id_plantilla','nom_guia'),
            'language' => 'es',
            'options' => ['placeholder' => 'seleccione','id'=>'codigo'],
            'pluginOptions' => [
                'allowClear' => true
            ],
            'pluginEvents' => [
            "change" => 'function() { 
                var data_id = $(this).val();
                alert(data_id);
                }',
            ],
            ])->label(false); 
            $id_padre = ????;
            $modelsplantilla = Plantilla::find()->where(['id_padre' => $id_padre])->orderBy('id_plantilla')->all();
            ?></td>

(Jaimez) #6

If you want to dynamically create table in the same view as your select element you should get ID from select2 widget as alirz23 proposed. Then run $.ajax call with url of controller action where you get requested Plantilla models. Controller action injects these models to a view where is the HTML code of table you want to create. And this is returned by controller as response to AJAX call. Callback function inserts this HTML code to the view where the form is. So something like this:

$('#select_input_id').on("change", function (e) { 
    var id =$(this).val();
    $.get('url/of/controller/action',{id: id}, function(data){
        $('#newTable').html(data);
    });
});

controller:

public function actionTable($id_padre)
{
$modelsplantilla = Plantilla::find()->where(['id_padre' => $id_padre])->orderBy('id_plantilla')->all();
return $this->renderAjax('_table',[
    'model' => $modelsplantilla,
]);
}

and also don’t forget to create view file _table where you write the code to generate table from $modelsplantilla


(hobbit2324) #7

Thank you so much this really help me