dpn517111 2018-03-26 13:14
浏览 77
已采纳

Yii2 pjax破坏了风格

I'm using Yii2 and i have a view which displays a table:

<div class="table-responsive">
<?php  yii\widgets\Pjax::begin()?>    
<?= GridView::widget([
            'dataProvider' => $dataProvider,
            'summary'=> "",
            'tableOptions' => ['id'=>'smartTable',
                'class' => 'display table-bordered-custom',
                'cellspacing' => 0,
                'width' => "100%"
                ],
            'columns' => [
                [
                  'class' => 'yii\grid\SerialColumn'],    
                  'product_code',
                  'product_name',
                  'product_status',    
                   [
                    'class' => 'yii\grid\ActionColumn',
                    'header' => 'Operation',
                    'template' => '{view}',
                    'headerOptions' => ['class' => 'no-sort'],
                    'buttons' => [
                        'view' => function ($url, $model) {
                            return Html::a(
                                'Edit<i class="material-icons">chevron_right</i>',
                                ['update', 'id'=>$model->product_id],
                                [
                                    'class' => 'btn',
                                    'data-pjax' => '',
                                ]
                            );
                        },
                    ],
                ],
            ],    
]);    
?>
<?php Pjax::end(); ?>    
</div>        

My controller:

class ProductController extends Controller
{

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::class,
            'actions' => [
                'delete' => ['POST'],
            ],
        ],
    ];
}

/**
 * Lists all Product models.
 * @return mixed
 */
public function actionIndex()
{
    $dataProvider = new ActiveDataProvider([
        'query' => Product::find(),
        'sort' => false,
        'pagination'=> [
            'pageSize' => 4
        ]
    ]);

    return $this->render('index', [
        'dataProvider' => $dataProvider,
    ]);
}
}    

Edit: My controller has a simple action called index.

My problem is that when i click on the page number and get new info, all the styles I've given to my table disappear.
If i remove Pjax everything is okay because the entire page reloads.

Why? please help me.

  • 写回答

2条回答 默认 最新

  • doushenjia8514 2018-03-26 21:08
    关注

    I finally managed to fix this! but i'm not sure it's the best.
    With pjax events you can solve such problems:

    $(document).on("pjax:success", function() {
     $('#smartTable').DataTable({
         "paging": false,
         "responsive": true,
         "dom": 'iftlp',
         "bProcessing": true,
         "aoColumnDefs": [ // EXCEPT SORTING
             {'bSortable': false, 'aTargets': ['no-sort']}
         ],
         });
     $('#simpleTable').DataTable({
         "responsive": true,
         "dom": 't'
     });
    });
    

    So every time our pjax is successfully executed, we will re-apply the style we need.
    That's it, i hope to be useful to others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!