JavaScript/krajeeDialog.alert在GridView中似乎有问题



如果 GridView 中的复选框已被激活,我打算收到警报消息。不幸的是,我什么也得不到,既不是错误也不是消息。这段代码有什么问题。控制器中的操作实际上会完成它的工作,JavaScript 会为$search做它的工作,$checkBox它不会做它的工作。穆罕默德·奥马尔·阿斯拉姆的改写(这是完整的视图代码)

<?php
use yiihelpersHtml;
use kartikgridGridView;
use kartikalertAlert;
use yiiwebSession;
$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, yiiwebView::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' => 'yiigridSerialColumn'],
[
'class' => 'kartikgridExpandRowColumn',
'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 yiidbExpression('NOW()');
$now = (new yiidbQuery)->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' => 'kartikgridBooleanColumn',
'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' => yiihelpersArrayHelper::map(backendmodelsBankverbindung::find()->asArray()->all(), 'id', 'id'),
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Bankverbindung', 'id' => 'grid-kunde-search-bankverbindung_id']
],
*/
[
'class' => 'kartikgridCheckboxColumn', 'checkboxOptions' => function($model) {
return ['value' => $model->id];
},
],
[
'class' => 'yiigridActionColumn',
],
[
'class' => 'yiigridActionColumn',
'template' => '{bankverbindung},{termin}',
'buttons' => [
'bankverbindung' => function ($id, $model) {
if (!empty($model->bankverbindung_id)) {
$pk = backendmodelsBankverbindung::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 = frontendmodelsAdminbesichtigungkunde::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();
?>

如果它没有在控制台上抛出任何错误并且什么都不做,请尝试更改

$this->registerJs($checkBox);

$this->registerJs($checkBox,yiiwebView::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, yiiwebView::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, yiiwebView::POS_READY);

最新更新