在yii2中将数据传递到下一页

Pass data to next page in yii2

本文关键字:一页 数据 yii2      更新时间:2023-09-26

我正在做一个项目要求是当用户单击gridview中的一行时,它应该打开一个名为check in的新控制器,并显示该特定行的所有已检入的用户。

我很困惑,而不是使用actioncolumn和创建一个新的按钮或使用rowopions

无论如何,当我像这样使用rowoptions

        [
            'class' => 'yii'grid'ActionColumn',
            'template' => '{index} {view} {update} {delete} ',
            'buttons' => [
                'index' => function ($url,$model) {
                    return Html::a('<span class="glyphicon glyphicon-user"></span>', $url);
                },
            ]
        ],

当我使用javascript的代码

<?php
        $this->registerJs("
            $('tbody td').css('cursor', 'pointer');
            $('tbody td').click(function (e) {
                var id = $(this).closest('tr').data('id');
            if (e.target == this)
                location.href = '" . Url::to(['checkin/index' ]) . "?id=' + id;
            });
        ");
    ?>

它给出了行点击事件发生的id,但它不重定向到"checkin/index"页面它在相同的日期刷新,并且行id附加在url的末尾。

但是当我使用这个脚本

 <?php
    $this->registerJs("
        $('td').click(function (e) {
            var id = $(this).closest('tr').data('id');
            if(e.target == this)
                location.href = '" . Url::to(['checkin/index']) . "?id=' + id;
        });
    ");
?>

它重定向到checkin/index页面但url没有得到任何id显示checkin/index/id=undefined

我想将id传递给checkinSearch控制器,并且只显示单击行的数据。

你可以试试这个…例如

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'rowOptions' => function ($model, $key, $index, $grid) {
                        return ['id' => $model['student_id'], 'class' => 'action-tr', 'data-link' => urldecode(Url::toRoute(['/student/student-transaction/update', 'id' => $model['student_id']]))];
    },
    'columns' => [
        ['class' => 'yii'grid'SerialColumn'],
        [
          'attribute' => 'student_roll_no',
          'value' => 'rel_Stud_Info.student_roll_no',
        ],
        [
          'class' => 'yii'grid'ActionColumn',
          'template' => '{update} {delete}',
          'contentOptions' => ['class'=>'action-td'],       
        ],
    ],
]); ?>

并在Js下面注册以防止ActionColumn重定向行点击。

<?php
    $this->registerJs("
    $(document).ready(function(){
        $('.action-tr').on('click', 'td:not(.action-td)', function(){
            //get the link from data attribute
            var the_link = $(this).parent().attr('data-link');
            //do we have a valid link      
            if (the_link == '' || typeof the_link === 'undefined') {
            //do nothing for now
            }
            else {
            //open the page
            window.location = the_link;
            }
        });
    });
"); ?>

这个例子是真实项目的一部分,对我有用。试一试…