How to use Editable in Yii2 on DetailView

Follow https://github.com/kartik-v/yii2-editable to get in your Yii2 project

Add on the top of your page in yii2 view.

use kartik\editable\Editable;
// create a similar in your code replace ‘name’ with you correct attribute
[
‘attribute’=>’name’,
‘format’=>’raw’,
‘value’=>Editable::widget([
‘model’=> $model,
‘attribute’=>’name’,
‘asPopover’=>true,
‘size’=>’lg’,
‘header’=>’Name’,
‘displayValueConfig’=> $model->name,
‘placement’=>’right’,
‘format’=>Editable::FORMAT_LINK,
‘inputType’=>Editable::INPUT_TEXT,
‘data’=>$model->name, // any list of values
]),
],
// in your controller for that model add the following code
// Check if there is an Editable ajax request
if (isset($_POST[‘hasEditable’])) {
// use Yii’s response format to encode output as JSON
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
// read your posted model attributes
if ($model->load($_POST)) {
$redirectFlag = false;
$output = ”;
$posted = $_POST[‘Projects’]; // <———– This is your Model NAME
if( isset($posted[‘name’]) AND!empty($posted[‘name’]) ){
$model->name = $posted[‘name’];
$redirectFlag = true; //set to true to reload the page to update non ajax content
}
if($model->save()){
if($redirectFlag){return$this->redirect([‘view’, ‘id’=> $model->project_id]);
}else{return [‘output’=>$output, ‘message’=>”]; }
}else{return [‘output’=>”, ‘message’=>’Validation error’]; }
}else {return [‘output’=>”, ‘message’=>”];}
}
Advertisements