Yii2 Kartik可编辑的输入值只会在刷新后更改

时间:2019-05-03 04:41:35

标签: ajax yii2

我使用Kartik Editable输入小部件。我这里有一个home模型和tema模型属性。每当我在字段中输入并提交值时,该值都不会立即更改,而只会在刷新页面后更改。我该怎么办?谢谢!

我的控制器:

public function actionIndex()
    {
        $searchModel = new HomeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


// table only has one row
$model= Home::find()->one();

    // 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)) {
            // read or convert your posted information. Based on the 'name' property set in the view. So this 'tema' of $model-> tema comes from 'name' property set in the view.

            $value = $model->tema;
           $model->save();
            // return JSON encoded output in the below format
            return ['output'=>$value, 'message'=>'output berhasil'];

            // alternatively you can return a validation error
            // return ['output'=>'', 'message'=>'Validation error'];
        }
        // else if nothing to do always return an empty JSON encoded output
        else {
            return ['output'=>'', 'message'=>'output gagal'];
        }

        };

return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'model'=>$model,
        ]);
    }

视图

  <?php 

    echo Editable::widget([
    'model' => $model, 
    'attribute' => 'tema',
    'value'=>$model->tema,
/*'asPopover'=>'false',*/
    'type' => 'post',

    'header'=>'tema', 
    'valueIfNull'=>'value-nya NULL',
    'format'=>'link',
    'size'=> 'lg',
    'inputType' => Editable::INPUT_TEXT,
    'editableValueOptions' => ['class' => 'text-success h3']
]);         ?>

另一个问题是,每当我使用'asPopover'=>'false'时,它都不会显示任何错误,但是当我单击所谓的editable-input字段时却什么也没有发生。可编辑内联字段将不会显示。当我使用popOver选项时,弹出窗口会自动触发而无需单击,并且还会在页面的左上角弹出。只有在单击触发弹出窗口的可编辑窗口小部件之后,它才会将自身重新校正到正确的位置。是虫子吗?我在引导程序4中使用了最新的Yii2,并且已按照文档中的params.php'bsVersion' => '4.x',配置中设置了全局参数

1 个答案:

答案 0 :(得分:0)

在控制器中,尝试以下操作:

    public function actionIndex()
    {
        $searchModel = new HomeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if (isset($_POST['hasEditable'])) 
    {
        $tema = Yii::$app->request->post('editableKey');
        $modelHome = Home::findOne($tema);

        $posted = current($_POST['Home']);
        $post = ['Home' => $posted];

        if ($modelHome->load($post)) {
           $modelHome->save();
           $out = Json::encode(['output'=>$modelHome->tema, 'message'=>'']);
           return $out;

        }
        return;
    };

return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'model'=>$model,
        ]);
    }