How to use renderAjax Yii2

0

A query, I have been trying to use renderAjax but there is no way, could anyone show me an example of how to use it correctly? When I try to use it, I absolutely do not load any style, layout or anything, just the view I indicated. Thanks

Part of the controller

public function actionCreate()
{
    $model = new Sectores();
    if(Yii::$app->request->isAjax){
        if ($model->load(Yii::$app->request->post()) && $model->save()) { 
            Yii::$app->getSession()->setFlash('success',['message' => Html::encode('El registro se guardó correctamente')]);
            return $this->renderAjax('create',['model' => new Sectores]);                           
        }                                       
    } else {           
        return $this->render('create', [
            'model' => $model
        ]);
    }
}

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $searchModel = new SectoresSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    if(Yii::$app->request->isAjax){
        if ($model->load(Yii::$app->request->post()) && $model->save()) {               
            Yii::$app->getSession()->setFlash('success',[
                'message' => Html::encode('El registro se actualizo correctamente'),
            ]);
        }else{
            Yii::$app->getSession()->setFlash('error',[
                'message' => Html::encode('El registro no se pudo actualizar'),
            ]); 
        } 
        return $this->renderAjax('index',['searchModel' => $searchModel,'dataProvider' => $dataProvider]); 
    } else {
        return $this->render('update', ['model' => $model]);
    }
}

View

<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
use yii\web\View;
use kartik\form\ActiveForm;
use demogorgorn\ajax\AjaxSubmitButton;

?>
<!-- Notificaciones -->
<?php foreach (Yii::$app->session->getAllFlashes() as $message):; ?>
        <?php
        \lavrentiev\widgets\toastr\NotificationFlash::widget([
            'type' => (!empty($message['type'])) ? $message['type'] : 'danger',
            'title' => (!empty($message['title'])) ? Html::encode($message['title']) : 'Title Not Set!',
            'message' => (!empty($message['message'])) ? Html::encode($message['message']) : 'Message Not Set!',
        ]);
        ?>
<?php endforeach; ?>
<!-- Fin notificaciones -->

<div class="form-group mt-3 card-grid p-3">
    <?php Pjax::begin(['id' => 'pjax-container']); ?>   
    <?php $form = ActiveForm::begin(
        [
            'id' => $model->formName(), 
            'enableClientValidation' => true, 
            'enableAjaxValidation' => true,
            'validationUrl' => Url::toRoute(['sectores/validation','scenario' => $model->isNewRecord ? 'create' : 'default']), 
            'fieldConfig' => ['errorOptions' => ['encode' => false, 'class' => 'd-none']],
            'errorSummaryCssClass' => 'alert alert-danger',
        ]
    ); ?>
    <?php echo $form->errorSummary($model); ?> 

    <div class="row">
        <div class="col-md-4">
            <?= $form->field($model, 'descripcion')->textInput(['maxlength' => true, 'autocomplete' => "off", 'autofocus' => true]) ?>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <?php AjaxSubmitButton::begin([
                'label' => 'Guardar',
                'useWithActiveForm' => $model->formName(),
                'ajaxOptions' => [
                    'type'=>'POST',
                    'url'=>$model->isNewRecord ? 'create' : Url::to(['update', 'id' => $model->id_sector]),
                    'success' => new \yii\web\JsExpression('function(data){                        
                        $(".main-content").html(data)                                              
                    }')
                ],
                'options' => ['class' => 'btn btn-primary', 'type' => 'submit'],
                ]);
            AjaxSubmitButton::end(); ?> 
            <?= Html::a('Cancelar', ['cancelar'], ['class'=>'btn btn-danger']) ?>            
        </div>
    </div>
    <?php ActiveForm::end(); ?>  
    <?php Pjax::end(); ?>
   
</div>
    
asked by Pablo JS 08.10.2018 в 15:11
source

0 answers