2016-09-18 20 views
1

我使用模態創建了窗體。問題是在views/index.php中的按鈕「創建」。 Proccess測試分頁&搜索成功,但之後當我點擊按鈕「創建」在views/index.php。表單創建與模式不顯示,我不知道爲什麼。在yii2中使用模態創建錯誤

在控制器

public function actionCreate() 
 
    { 
 
     $model = new Donatur(); 
 
     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
 
       Yii::$app->session->setFlash('success', 'Data berhasil disimpan!'); 
 
       return $this->redirect(['index']); 
 
       return $this->refresh(); 
 
      } else { 
 
       if (Yii::$app->request->isAjax) { 
 
        return $this->renderAjax('create', ['model' => $model]); 
 
       } 
 
       else{ 
 
        return $this->render('create', ['model' => $model]); 
 
       } 
 
      } 
 
    }

代碼代碼在視圖/ index.php的

<?php \yii\widgets\Pjax::begin(['timeout' => false, 'id' => 'pjax-gridview']); ?> 
 

 
<?php 
 
use yii\helpers\Html; 
 
use yii\grid\GridView; 
 
use yii\widgets\Pjax; 
 
use yii\bootstrap\Modal; 
 
use yii\helpers\Url; 
 

 
/* @var $this yii\web\View */ 
 
/* @var $searchModel app\models\SearchDonatur */ 
 
/* @var $dataProvider yii\data\ActiveDataProvider */ 
 

 
$this->title = 'Data Donatur'; 
 
?> 
 

 
<?php if (Yii::$app->session->hasFlash('success')): ?> 
 
    <div class="alert alert-success alert-dismissable"> 
 
    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button> 
 
    <h4><i class="icon fa fa-check"></i>Informasi!</h4> 
 
    <?= Yii::$app->session->getFlash('success') ?> 
 
</div> 
 
<?php endif; ?> 
 

 

 
<?php if (Yii::$app->session->hasFlash('delete')): ?> 
 
    <div class="alert alert-success alert-dismissable"> 
 
    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button> 
 
    <h4><i class="icon fa fa-check"></i>Informasi!</h4> 
 
    <?= Yii::$app->session->getFlash('delete') ?> 
 
    </div> 
 
<?php endif; ?> 
 

 

 
<div class="donatur-index"> 
 
    
 
    <?php Pjax::begin(['timeout'=>false,'id'=>'pjax-gridview']); ?> 
 
     
 
    <h1><?= Html::encode($this->title) ?></h1> 
 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?> 
 

 
    <p> 
 
     <?= Html::button('Tambah Donatur', ['value'=>Url::to('create'),'class' => 'btn btn-success','id'=>'modalButton']) ?> 
 
    </p> 
 

 
    
 
    <?= GridView::widget([ 
 
     'dataProvider' => $dataProvider, 
 
     'filterModel' => $searchModel, 
 
     'emptyCell'=>'-', 
 
     'summary'=>'', 
 
     'columns' => [ 
 
      //['class' => 'yii\grid\SerialColumn'], 
 
      [ 
 
      'attribute'=>'kode_donatur', 
 
      'value'=>'kode_donatur', 
 
      'contentOptions'=>['style'=>'width: 200px;'] 
 
      ], 
 

 
      [ 
 
      'attribute'=>'nama_donatur', 
 
      'value'=>'nama_donatur', 
 
      'contentOptions'=>['style'=>'width: 250px;'] 
 
      ], 
 
      [ 
 
      'attribute'=>'alamat', 
 
      'value'=>'alamat', 
 
      'contentOptions'=>['style'=>'width: 350px;'] 
 
      ], 
 
      [ 
 
      'attribute'=>'telepon', 
 
      'value'=>'telepon', 
 
      'contentOptions'=>['style'=>'width: 290px;'] 
 
      ], 
 

 

 

 
      [ 
 
      'class' => \yii\grid\ActionColumn::className(), 
 
      'header' => 'Aksi', 
 
      'template' => '{update} {delete}', 
 
      'buttons' => [ 
 
       'update' => function($url, $model) { 
 
        $icon = '<span class="glyphicon glyphicon-pencil"></span>'; 
 
        return Html::a($icon, $url,[ 
 
         'data-toggle' => "modal", 
 
         'data-target' => "#donaturModal", 
 
         ]); 
 
       }, 
 

 
       'delete' => function($url, $model) { 
 
        $icon = '<span class="glyphicon glyphicon-trash"></span>'; 
 
        return Html::a($icon, $url, 
 
        [ 
 
         'data-confirm' => "Apakah yakin dihapus ?", 
 
         'data-method' => 'post',  
 
        ]); 
 
       }, 
 
       ] 
 
      ], 
 
     ], 
 
    ]); ?> 
 

 
    <?php \yii\widgets\Pjax::end() ?> 
 
    <?php Pjax::end(); ?> 
 

 
</div> 
 
<?php 
 
// for modal update 
 
Modal::begin([ 
 
    'id' => 'donaturModal', 
 
    'header' => '<h1 align="center">Ubah Data Donatur</h1>', 
 
    ]); 
 
    Pjax::begin(['id'=>'pjax-modal', 'timeout'=>false, 
 
     'enablePushState'=>false, 
 
     'enableReplaceState'=>false,]); 
 

 
    Pjax::end(); 
 
Modal::end(); 
 
?> 
 

 
<?php 
 
// for modal update 
 
$this->registerJs(' 
 
    $("#donaturModal").on("shown.bs.modal", function (event) { 
 
     var button = $(event.relatedTarget) 
 
     var href = button.attr("href") 
 
     $.pjax.reload("#pjax-modal", { 
 
      "timeout":false, 
 
      "url":href, 
 
      "replace":false, 
 
     }); 
 
    }) 
 
');  
 
?> 
 

 

 
<?php 
 
// for modal create 
 
    Modal::begin([ 
 
    'header' => '<h1 align="center">Tambah Donatur</h1>', 
 
    'id' => 'modal', 
 
    ]); 
 

 
    echo "<div id='modalContent'><div>"; 
 
    Modal::end() 
 
?> 
 

 
<?php 
 
// for modal create 
 
Modal::begin([ 
 
    'id' => 'modal', 
 
    'header' => '<h1 align="center">Ubah Data Donatur</h1>', 
 
    ]); 
 
    Pjax::begin(['id'=>'pjax-modal', 'timeout'=>false, 
 
     'enablePushState'=>false, 
 
     'enableReplaceState'=>false,]); 
 

 
    Pjax::end(); 
 
Modal::end(); 
 
?> 
 

 
<?php 
 
// for modal create 
 
$this->registerJs(' 
 
    $("#modal").on("shown.bs.modal", function (event) { 
 
     var button = $(event.relatedTarget) 
 
     var href = button.attr("href") 
 
     $.pjax.reload("#pjax-modal", { 
 
      "timeout":false, 
 
      "url":href, 
 
      "replace":false, 
 
     }); 
 
    }) 
 
');  
 
?>

代碼在視圖/ create.php

<?php 
 

 
use yii\helpers\Html; 
 
use yii\bootstrap\ActiveForm; 
 
use yii\widgets\Pjax; 
 
use yii\bootstrap\Modal; 
 
use yii\helpers\Url; 
 
use yii\db\ActiveRecord; 
 

 
/* @var $this yii\web\View */ 
 
/* @var $model app\models\Donatur */ 
 
?> 
 

 
<h2 align="center">Form Donatur</h2> 
 
<?php 
 
echo "&nbsp"; 
 
echo "&nbsp"; 
 
?> 
 
<?php $form = ActiveForm::begin([ 
 
    'layout' => 'horizontal', 
 
    'enableAjaxValidation' => false, 
 
    'id' => 'create-form', 
 
    ]); ?> 
 

 
<?= $form->field($model, 'kode_donatur')->textInput(['readonly' => true, 'style'=>'width:100px']) ?> 
 
<?= $form->field($model, 'nama_donatur')->textInput(['style'=>'width:350px']) ?> 
 
<?= $form->field($model, 'alamat')->textArea(['rows' => 3, 'style'=>'width:350px']) ?> 
 
<?= $form->field($model, 'telepon')->textInput(['style'=>'width:350px']) ?> 
 
<div class="form-group"> 
 
    <div class="col-sm-offset-4"> 
 
<?= Html::submitButton('Simpan', ['class'=> 'btn btn-primary']) ?> 
 

 
<?php 
 
echo "&nbsp"; 
 
echo "&nbsp"; 
 
echo Html::a('Keluar', ['index'],[ 
 
    'class'=>'btn btn-success', 
 
    'onclick' =>'$("#modal").modal("hide"); 
 
    return false;' 
 
    ]); 
 
?> 
 
    </div> 
 
</div> 
 
<?php ActiveForm::end();?>

的我創建了模態請點擊此鏈接創建表格前:click

+0

您可以檢查是否有重複的JavaScript庫,當您發送AJAX渲染模式窗體(返回$這個 - > renderAjax(「創造」,[「模型」 => $模型] );) –

+0

我認爲不重複@NuriddinRashidov – CrashBurn

+0

你可以發佈你從ajax請求獲得的代碼嗎? –

回答

0

我想用按鈕處理程序問題。 也許你忘了添加處理程序:

$(function(){ 
    $('#modalButton').click(function(){ ... }); 
})