在yii2中使用模态创建有些错误



我已经使用modal创建了表单。问题是button "Create"在视图/index . php。进程测试分页搜索成功,但之后当我要点击按钮"创建"在视图/index . php。表单创建与模态不显示,我不知道为什么。

控制器代码

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

views/index.php中的代码

<?php yiiwidgetsPjax::begin(['timeout' => false, 'id' => 'pjax-gridview']); ?>
<?php
use yiihelpersHtml;
use yiigridGridView;
use yiiwidgetsPjax;
use yiibootstrapModal;
use yiihelpersUrl;
/* @var $this yiiwebView */
/* @var $searchModel appmodelsSearchDonatur */
/* @var $dataProvider yiidataActiveDataProvider */
$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' => 'yiigridSerialColumn'],
            [
             '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' => yiigridActionColumn::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 yiiwidgetsPjax::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,  
        });  
    })
');    
?>

views/create.php中的代码

<?php
use yiihelpersHtml;
use yiibootstrapActiveForm;
use yiiwidgetsPjax;
use yiibootstrapModal;
use yiihelpersUrl;
use yiidbActiveRecord;
/* @var $this yiiwebView */
/* @var $model appmodelsDonatur */
?>
<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();?>

在我用modal创建表单之前,按照下面的链接:点击

我认为问题与按钮处理程序。可能您忘记添加handler:

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

最新更新