Kartik gridview and related field update as id with editable

Hi,

i’ve setup a table with a related field that outputs the data fetched from a related table, based on a classic id field relation.

When I close the edit popover (with EditableColumn and dropdown inputtype that output the same related field), the table display the id and not the related (nomeAssicurato) data. If I refresh the page, the correct datas are displayed.

This is the column definition:




[

	'class' => 'kartik\grid\EditableColumn',

	'attribute' => 'idAssicurato',

	'value'  => 'idAssicurato0.nomeAssicurato',

	'pageSummary' => true,

	'editableOptions'=> function ($model, $key, $index, $widget) {

		return [

			'header' => 'Assicurato',

			'size' => 'md',

			'inputType' => Editable::INPUT_DROPDOWN_LIST,

			'data' => ArrayHelper::map(Assicurato::find()->all(), 'id', 'nomeAssicurato')

		];

	}

],



and this is the controller method that updates the data (adapted from How to setup an editable column to manipulate records in Yii2 grid view)




    public function actionIndex()

 	{

	    // your default model and dataProvider generated by gii

	    $searchModel = new PraticaSearch;

	    $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());


	    // validate if there is a editable input saved via AJAX

	    if (Yii::$app->request->post('hasEditable')) {

	        // instantiate your pratica model for saving

	        $id = Yii::$app->request->post('editableKey');

	        $model = Pratica::findOne($id);


	        // store a default json response as desired by editable

	        $out = Json::encode(['output'=>'', 'message'=>'']);


	        // fetch the first entry in posted data (there should 

	        // only be one entry anyway in this array for an 

	        // editable submission)

	        // - $posted is the posted data for Pratica without any indexes

	        // - $post is the converted array for single model validation

	        $post = [];

	        $posted = current($_POST['Pratica']);

	        $post['Pratica'] = $posted;


	        // load model like any single model validation

	        if ($model->load($post)) {

	            // can save model or do something before saving model

	            $model->save();


	            // custom output to return to be displayed as the editable grid cell

	            // data. Normally this is empty - whereby whatever value is edited by 

	            // in the input by user is updated automatically.

	            $output = '';


	            // similarly you can check if the name attribute was posted as well

	            // if (isset($posted['name'])) {

	            //   $output =  ''; // process as you need

	            // } 

	            $out = Json::encode(['output'=>$output, 'message'=>'']);

	        } 

	        // return ajax json encoded response and exit

	        echo $out;

	        return;

	    }


	    // non-ajax - render the grid by default

	    return $this->render('index', [

	        'dataProvider' => $dataProvider,

	        'searchModel' => $searchModel

	    ]);

	}



Where is the issue?

Thank you,

Alex

Hi. I would like to ask you, if you find any solution to your problem of gridview editable dropdown problem, because i have the same problem. After submiting the popover, the value in the griudview is the ID and not the related field what i want to be displayed. Please, if you have any solution, be so kind and inform me. Thanks

The way I solved this issue was to use the ‘displayValueConfig’ setting in the EditableOptions. You would do it like this:




$assicurato = ArrayHelper::map(Assicurato::find()->all(), 'id', 'nomeAssicurato');


...

[

        'class' => 'kartik\grid\EditableColumn',

        'attribute' => 'idAssicurato',

        'value'  => 'idAssicurato0.nomeAssicurato',

        'pageSummary' => true,

        'editableOptions'=> function ($model, $key, $index, $widget) {

                return [

                        'header' => 'Assicurato',

                        'size' => 'md',

                        'inputType' => Editable::INPUT_DROPDOWN_LIST,

                        'displayValueConfig' => $assicurato,

                        'data' => $assicurato

                ];

        }

]



The ‘displayValueConfig’ uses the provided value to lookup what display value should be shown.

Thanks Luke Lewis. It’s Works.