处理ajax yii2上的面包屑

时间:2016-08-28 18:46:00

标签: php ajax yii2

我在yii2有面包屑。问题是,我正在使用pjax来处理数据库中我的表的问题。

假设这是我的代码(在视图中的index.php中):

<?php
$this->title = 'Barangs';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="barang-index">

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

<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]);  ?>

<p>
    <?= Html::a('Create Barang', ['create'], ['class' => 'btn btn-success']) ?>
</p>



<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'NO_URUT',
        'CONSIGNEE',
        'CONTAINER',
        'SIZE',
        'COIL_NO',
        'NET',
        'GROSS',
        'CONTRACT_NO',
        'KET',
        'NAMA_FILE',
        'TGL_UNSTUFF',
        ['class' => 'yii\grid\ActionColumn',
            'buttons' => [
                'view' => function($url, $model) {
                    $icon = '<span class="glyphicon glyphicon-eye-open"></span>';
                    return Html::a($icon, $url);
                },
                'update' => function($url, $model) {
                    $icon = '<span class="glyphicon glyphicon-pencil"></span>';
                    return Html::a($icon, $url);
                },
                'delete' => function($url, $model) {
                    $icon = '<span class="glyphicon glyphicon-trash"></span>';
                    return Html::a($icon, $url, [
                                "class" => 'pjaxDelete'
                    ]);
                },
                    ]
                ],
            ],
        ]);
        ?>

        <?php $this->registerJs('
            /* fungsi ini akan dijalankan ketika class pjaxDelete di klik */
            $(".pjaxDelete").on("click", function (e) {
                /* cegah link menjalankan default action */
                e.preventDefault();
                if(confirm("Are you sure you want to delete this item?")){
                    /* request actionDelete dengan method post */
                    $.post($(this).attr("href"), function(data) {
                        /* reload gridview */
                        $.pjax.reload("#pjax-gridview",{"timeout":false});
                    });
                }
            });
        ');
        ?>
        <?php Pjax::end(); ?></div>

我的问题是,如果没有ajax,breadcumbs会显示Home / Barangs / Create Barang。但是在ajax中,面包屑看起来像这样:Home / Barangs /

我仍然需要第一种面包屑格式。

请告知。

1 个答案:

答案 0 :(得分:0)

使用时Pjax =&gt;页面未刷新,但网址已更改 使用此代码可能是您的问题解决

<?php Pjax::begin(['enablePushState' => false]); $form = ActiveForm::begin(['action' => '', 'options' => ['data-pjax' => '']]); ?>