dongwu9972 2017-02-08 05:37
浏览 50

PHP:如何根据gridview中单击按钮的行ID在模式框中显示数据?

I've a Yii2-basic application that has view of index.php that contain a kartik GridView. There is an action column that contain view button that will redirect the page to view of view.php if clicked.

enter image description here

If the view button clicked, page will redirect to view.php and show data based on id,

For example from image above, that table contain two button in view column. If the first(top) button clicked, it will show data base on id from id column (left side of table), so it will show data that has id = 1. And I've successfully create this action.

But I need to show data in modal box. So I use yii\bootstrap\Modal. And I've succesfully show the data in modal box. For the first clicked, it will popup modal box that contain data based on id, but for the next click button it will also show the data based on id that got from the first clicked button.

For example from picture above: For the first time I click the button from the second row (id = 2) and then popup a modal box that contain data that has id=2, and then I click the button from the first row(id = 1) and it will also popup modalbox that contain data that has id = 2.

Here's my code from index.php:

<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
use yii\bootstrap\Modal;

<div class="students-index">
<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table table-hover'],
    'columns' => [
        [
            'label' => 'id',
            'value' => function($data) {
                return $data->id;
            }
        ],
        [
            'label' => 'Name',
            'value' => function($data) {
                return $data->name;
            }
        ],
        [
            'label' => 'Birthdate',
            'value' => function($data) {
                return $data->Birthdate;
            }
        ],
        [
            'label' => 'Sex',
            'value' => function($data) {
                return $data->Sex;
            }
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'header' => 'View',
            'template' => '{view}',
            'buttons' => [
                'view' => function ($url, $model) {
            $icon = '<span class="glyphicon glyphicon-eye-open"></span>';
            return Html::a($icon, $url, [
                        'title' => Yii::t('app', 'View'),
                        'data-toggle' => "modal",
                        'data-target' => "#myModal",
            ]);
        },
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
        if ($action === 'view') {
            $url = Url::toRoute(['/school/students/view', 'id' => $model->faktur_out_id], ['data-method' => 'post',]);
            return $url;
        }
    }
        ]
    ],
]);
?>

</div>

<?php
Modal::begin([
'id' => 'myModal',
]);
 Pjax::begin([
     'id'=>'pjax-modal','timeout'=>false,
     'enablePushState'=>false,
     'enableReplaceState'=>false,
 ]); 

 Pjax::end();

 Modal::end();
?>

<?php
$this->registerJs("
$('#myModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget)
    var href = button.attr('href') 
    $.pjax.reload('#pjax-modal', {
    'url' => href,
    });
"); ?>

How do I can show the data in modal box based on id?

But actually I didn't understand $this->registerJs("......") yet. Maybe anyone can explain about it in simple language.

Any help will be appreciated, thanks.

  • 写回答

1条回答 默认 最新

  • douyueju2912 2017-02-08 08:14
    关注

    $this->registerJs(" "); is simply a function in the Yii framework that allows you to write the javascript in between the " ".

    Try this:

    <?php
    Modal::begin([
    'id' => 'myModal',
    ]);
     Pjax::begin([
         'id'=>'myModal','timeout'=>false,
         'enablePushState'=>false,
         'enableReplaceState'=>false,
     ]); 
    
     Pjax::end();
    
     Modal::end();
     ?>
    
    <?php
    $this->registerJs("
    $('#myModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget)
        var href = button.attr('href') 
        $.pjax.reload('#myModal', {
        'url' => href,
        });
    "); ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler