【问题标题】:Yii2 Kartik editable input value will only change after refreshYii2 Kartik 可编辑输入值只会在刷新后改变
【发布时间】:2019-05-03 04:41:34
【问题描述】:

我使用 Kartik 可编辑输入小部件。我在这里有一个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'=&gt;'false' 时,它没有显示错误,但当我单击所谓的可编辑输入字段时没有任何反应。可编辑的内联字段不会显示。当我使用 popOver 选项时,弹出窗口只是自动触发而无需单击,并且在页面的左上角弹出。只有在我单击触发弹出窗口的可编辑小部件后,它才会将自身重新校正到正确的位置。它是一个错误吗?我将最新的 Yii2 与 bootstrap 4 一起使用,并且我在 params.php 配置中设置了全局参数 'bsVersion' =&gt; '4.x', 在文档中

【问题讨论】:

    标签: ajax yii2


    【解决方案1】:

    在控制器中,试试这个:

        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,
            ]);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      相关资源
      最近更新 更多