GridView中的JavaScript / krajeeDialog.alert似乎有故障

时间:2019-04-10 16:27:27

标签: javascript jquery yii2 kartik-v

如果已激活GridView中的checkBox,我打算得到警报消息。不幸的是,我什么也没有得到,没有错误也没有消息。该代码有什么问题。 Controller中的动作实际上可以完成其工作,JavaScript可以完成$ search的工作,为了$ checkBox 的工作,的工作。 为穆罕默德·奥默·阿斯兰(Muhammad Omer Aslam)改写(这里是完整的查看代码)

<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\alert\Alert;
use yii\web\Session;

$this->title = Yii::t('app', 'Kunde');
$this->params['breadcrumbs'][] = $this->title;
$js = <<<JS
        $('.search-button').click(function(){
             $('.search-form').toggle(1000);
             return false;
        });
        $(document).on('ready pjax:success', function(){
            $('form[name=\"idKunde\"]').on('click', '#cb input[type=\'checkbox\']', function(){
                 if($(this).is(':checked')){
                     krajeeDialog.alert('Implementieren Sie die Stapelmails über den entsprechenden Button.');
                 }
            });
        });
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
?>
<?php
//Hier werden alle Flashnachrichten ausgegeben
$session = new Session();
foreach ($session->getAllFlashes() as $flash) {
    foreach ($flash as $ausgabe) {
        echo Alert::widget([
            'type' => Alert::TYPE_INFO,
            'title' => 'Information',
            'icon' => 'glyphicon glyphicon-exclamation-sign',
            'body' => $ausgabe,
            'showSeparator' => true,
            'delay' => false
        ]);
    }
}
?>
<div class="kunde-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <div class="search-form" style="display:none">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
    </div>
    <p>
        <?= Html::a(Yii::t('app', 'Advance Search'), '#', ['class' => 'btn btn-info search-button']) ?>
    </p>
    <!-- hier wird das HTMl-Formular, welches die Mailmethode im Controller aufruft, implementiert-->
    <?=
    Html::beginForm(['/kunde/send'], 'post', ['name' => 'idKunde']);
    ?>
    <?php
    $gridColumn = [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'class' => 'kartik\grid\ExpandRowColumn',
            'width' => '50px',
            'value' => function ($model, $key, $index, $column) {
                return GridView::ROW_COLLAPSED;
            },
            'detail' => function ($model, $key, $index, $column) {
                return Yii::$app->controller->renderPartial('_expand', ['model' => $model]);
            },
            'headerOptions' => ['class' => 'kartik-sheet-style'],
            'expandOneOnly' => true
        ],
        ['attribute' => 'id', 'visible' => false],
        [
            'attribute' => 'l_plz_id',
            'label' => Yii::t('app', 'Plz'),
            'value' => function($model, $id) {
                return $model->l_plz_id ? $model->lPlz->plz : null;
            },
        ],
        [
            'attribute' => 'geschlecht',
            'label' => Yii::t('app', 'Geschlecht'),
            'value' => function($model, $id) {
                return $model->geschlecht ? $model->geschlecht0->typus : null;
            },
        ],
        'vorname',
        'nachname',
        'stadt',
        'strasse',
        [
            'attribute' => 'geburtsdatum',
            'format' => 'html',
            'label' => Yii::t('app', 'Geburtsdatum'),
            'value' => function($model, $id) {
                if ($model->geburtsdatum) {
                    $expression = new yii\db\Expression('NOW()');
                    $now = (new \yii\db\Query)->select($expression)->scalar();
                    $diff = strtotime($now) - strtotime($model->geburtsdatum);
                    $hours = floor($diff / (60 * 60));
                    $year = floor($hours / 24 / 365);
                    $output = date("d.m.Y", strtotime($model->geburtsdatum)) . '<br>' . $year . " Jahre alt";
                    return $output;
                } else {
                    return NULL;
                }
            },
        ],
        [
            'class' => 'kartik\grid\BooleanColumn',
            'attribute' => 'solvenz',
            'trueLabel' => 'Ja',
            'falseLabel' => 'Nein',
            'label' => 'Ist Solvent',
            'encodeLabel' => false,
        ],
        /*
          [
          'attribute' => 'bankverbindung_id',
          'label' => Yii::t('app', 'BankverbindungID'),
          'value' => function($model) {
          if ($model->bankverbindung) {
          return $model->bankverbindung->id;
          } else {
          return NULL;
          }
          },
          'filterType' => GridView::FILTER_SELECT2,
          'filter' => \yii\helpers\ArrayHelper::map(\backend\models\Bankverbindung::find()->asArray()->all(), 'id', 'id'),
          'filterWidgetOptions' => [
          'pluginOptions' => ['allowClear' => true],
          ],
          'filterInputOptions' => ['placeholder' => 'Bankverbindung', 'id' => 'grid-kunde-search-bankverbindung_id']
          ],
         */
        [
            'class' => '\kartik\grid\CheckboxColumn', 'checkboxOptions' => function($model) {
                return ['value' => $model->id];
            },
        ],
        [
            'class' => 'yii\grid\ActionColumn',
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{bankverbindung},{termin}',
            'buttons' => [
                'bankverbindung' => function ($id, $model) {
                    if (!empty($model->bankverbindung_id)) {
                        $pk = backend\models\Bankverbindung::findOne(['id' => $model->bankverbindung_id])->id;
                        return Html::a('<span class="glyphicon glyphicon-th-list"></span>', ['/bankverbindung/view', 'id' => $pk], ['title' => 'Bankverbindung anzeigen']);
                    }
                },
                'termin' => function ($id, $model) {
                    $data = frontend\models\Adminbesichtigungkunde::find()->all();
                    foreach ($data as $item) {
                        if ($item->kunde_id == $model->id) {
                            $fk = $item->besichtigungstermin_id;
                            $link = \Yii::$app->urlManagerFrontend->baseUrl . '/termin_viewen';
                            $link .= '?id=' . $fk;
                            return Html::a('<span class="glyphicon glyphicon-flag"></span>', $link, ['title' => 'zum Termin im Frontend springen']);
                        }
                    }
                },
            ],
        ],
    ];
    ?>
    <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'filterSelector' => '.choiceRadio',
        'columns' => $gridColumn,
        'pjax' => true,
        'pjaxSettings' => [
            'neverTimeout' => true,
        ],
        'options' => [
            'style' => 'overflow: auto; word-wrap: break-word;'
        ],
        'condensed' => true,
        'responsiveWrap' => true,
        'hover' => true,
        'persistResize' => true,
        'panel' => [
            'type' => GridView::TYPE_PRIMARY,
            "heading" => "<h3 class='panel-title'><i class='glyphicon glyphicon-globe'></i> " . $this->title . "</h3>",
            'after' => Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset Grid', ['/kunde/index'], ['class' => 'btn btn-warning', 'title' => 'Setzt die GridView zurück']),
            'toggleDataOptions' => ['minCount' => 10],
        ],
        'toolbar' => [
            ['content' =>
                Html::submitButton('<span class=" fa fa-pencil-square-o">', ['', 'class' => 'btn btn-success', 'title' => 'implementiert Mails für ausgewählte Kunden', 'name' => 'button_checkBoxes', 'data' => ['pjax' => '1']])
            ],
            '{export}',
            '{toggleData}'
        ],
        'toggleDataOptions' => ['minCount' => 10],
    ]);
    ?>

</div>
<?=
Html::endForm();
?>

1 个答案:

答案 0 :(得分:0)

如果未在控制台上引发任何错误并且什么也不做,请尝试更改

$this->registerJs($checkBox);

$this->registerJs($checkBox,\yii\web\View::POS_READY);

注意事项

  • 您需要更改选择器$(idKunde).clickidKunde应该是字符串,而绑定单击时您不使用引号,因此它将解析为变量idKunde而不是字符串。
  • 首先,您已将idKunde定义为name,而不是id

    <?=
        Html::beginForm(['/kunde/send'], 'post', ['name' => 'idKunde']);
    ?>
    
  • 因此,选择器应类似于form[name="idKunde"]

  • 您应该使用heredoc而不是简单的字符串作为此类摘要。
  • 并使用一个registerJs()通话,而不要通话两次。

将您的JavaScript代码更改为以下

$js =<<<JS
        $('.search-button').click(function(){
             $('.search-form').toggle(1000);
             return false;
        });
        $('form[name=\"idKunde\"]').on('click', '#cb input[type=\'checkbox\']', function(){
             if($(this).is(':checked')){
                 krajeeDialog.alert('Implementieren Sie die Stapelmails über den entsprechenden Button.');
             }
        });
JS;

$this->registerJs($js, \yii\web\View::POS_READY);

编辑:

在将点击绑定到GridView内的复选框时,最好将当前脚本包装在pjax:success事件内,就像通过过滤或更改网格在网格上执行重新加载时一样分页脚本将不再起作用。

因此,将上述click事件的摘要包装在

$(document).on('ready pjax:success', function(){
// add the click event binding here
});

因此,您完整的javascript代码段如下所示

$js =<<<JS
        $('.search-button').click(function(){
             $('.search-form').toggle(1000);
             return false;
        });
        $(document).on('ready pjax:success', function(){
            $('form[name=\"idKunde\"]').on('click', '#cb input[type=\'checkbox\']', function(){
                 if($(this).is(':checked')){
                     krajeeDialog.alert('Implementieren Sie die Stapelmails über den entsprechenden Button.');
                 }
            });
        });
JS;

$this->registerJs($js, \yii\web\View::POS_READY);