Answer the question
In order to leave comments, you need to log in
Yii2 Why does pjax reload the page when sorting a column and removing (ActionColumn)?
The page is reloaded in 2 cases: when sorting and when clicking in the ActionColumn (delete icon)
view:
<?php
$js = <<< JS
var path = location.href.replace(location.hash,"");
jQuery(function(){
var loc = window.location.hash.replace("#","");
if (loc!="")
{
/*$('.tab-pane').each(function(){
$(this).removeClass('active').removeClass('show');
});
$('.nav-item').each(function(){
$(this).removeClass('active');
});
$('.nav-item[href=\"#'+loc+'\"]').addClass('active');
$('#'+loc).addClass('active').addClass('show');
console.log('.nav-item[href=\"#'+loc+'\"]');
console.log($('.nav-item[href=\"#'+loc+'\"]'));*/
$('.nav-item[href=\"#'+loc+'\"]').click();
}
jQuery('#nav-tab li:last-child a').tab('show');
jQuery('#nav-tab .nav-item').on('click',function(){
location.href = path + jQuery(this).attr("href");
});
});
JS;
$this->registerJs( $js, $position = yii\web\View::POS_READY, $key = null );
?>
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Информация</a>
<a class="nav-item nav-link" id="nav-contact-tab" data-toggle="tab" href="#nav-contact" role="tab" aria-controls="nav-contact" aria-selected="false">Работа с данными</a>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<p class="pt-4">Продукты<i><?php foreach($modelsone as $model) {echo \Yii::$app->formatter->asDatetime(date('Y-m-01 23:59:59'), "php:d.m.Y ");
}?> :</i></p>
<table class="table table-striped table-bordered table-hover text-center">
<thead>
<th>наименование</th>
<th>дата и время добавления</th>
</thead>
<?php foreach($modelsone as $model):?>
<tr>
<td><?= htmlspecialchars($model['name'])?></td>
<td><?= \Yii::$app->formatter->asDatetime($model['date'], "php:d.m.Y H:i:s")
//htmlspecialchars($model['date'])?></td>
</tr>
<?php endforeach;?>
</table>
</div>
<div class="tab-pane fade scot-form-index" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">
<?php Pjax::begin(['id' => 'comments', 'enablePushState' => false, 'linkSelector' => '.btn-custom', 'clientOptions'=> ['method'=>'POST']]); ?>
<?=
Html::a('<span class="fas fa-plus" aria-hidden="true"></span> Добавить данные', ['animal/create'], ['class' => 'btn btn-success']).'<br>'
?>
<div class="report-index card border-primary">
<table>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"<div class='bg-j'><span style='text-align:right; padding-bottom: 10px; position:absolute;'>{pager}</span><span style='text-align:right; padding-bottom: 10px;' >{summary}</span></div>\n{items}\n{pager}",
'tableOptions' => [
'class' => 'table tableusers table-striped table-bordered table-warp'
],
'options' => [
'class' => 'table-responsive',
],
'pager' => [
'options'=>['class'=>'pagination'],
'maxButtonCount'=>10,
],
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
'header' => '№',
],
[
'attribute' => name',
'label' => 'Продукты',
'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
'headerOptions' => ['class' => 'text-center']
],
[ 'attribute' => 'Дата',
'label' => 'Дата',
'attribute' => 'date',
'format' => ['DateTime', 'php:d.m.Y H:i:s'],
'contentOptions' => ['style' => 'text-align: center; vertical-align:middle;', 'class'=>'jus'],
'headerOptions' => ['class' => 'text-center']
],
[
'class' => 'yii\grid\ActionColumn',
'template' => "{view}\n{update}\n{delete}",
'contentOptions' => ['style' => 'text-align: center; vertical-align:middle; width: 100px;'],
'headerOptions' => ['class' => 'text-center'],
'buttons' => [
'view' => function ($url, $model) {
Icon::map($this, Icon::FAS);
return Html::a('<span class="fas fa-eye" aria-hidden="true"></span>', $url);
},
'update' => function ($url, $model) {
return Html::a('<span class="fas fa-edit" aria-hidden="true"></span>', $url);
},
'delete' => function ($url, $model) {
return Html::a('<span class="fas fa-trash btn-custom" aria-hidden="true"></span>', $url, [
'data-method' => 'post',
'data-pjax' => '1',
]);
}
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url = Url::to(['animal/view', 'id' => $model->id]);
//$url = 'index.php?r=animal/view&id=' . $model->id;
return $url;
}
if ($action === 'update') {
$url = Url::to(['animal/update', 'id' => $model->id]);
//$url = 'index.php?r=animal/update&id=' . $model->id;
return $url;
}
if ($action === 'delete') {
$url = Url::to(['animal/delete', 'id' => $model->id ]);
//$url = 'index.php?r=animal/delete&id=' . $model->id;
return $url;
}
}
],
],
]);
?>
</table>
<?php Pjax::end(); ?>
</div>
</div>
</div>
public function actionDelete($id)
{
$this->findModel($id)->delete();
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
}
return $this->redirect(['post/animal']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question