yii2如何简化重复代码索引,ph

时间:2017-03-09 07:31:10

标签: php yii

php中的新内容并使用yii2作为Web开发的基础。请帮我简化这段代码,我试着在弹出的ActionColumn按钮中显示例如查看,更新和删除按钮。这是我的代码

'class' => 'yii\grid\ActionColumn',
    'buttons'=>[
    'view'=>function($url,$model){
        return Html::a('<span class="glyphicon glyphicon-eye-open")></span>',$url,['class'=>'view', 'data-pjax'=>0]);
    },
    'update'=>function($url,$model){
        return Html::a('<span class="glyphicon glyphicon-pencil")></span>',$url,['class'=>'update', 'data-pjax'=>0]);
                        }
                    ],
                ],
            ],  
        ]);

    $this->registerJs(
        "$(document).on('ready pjax:success', function() {  // 'pjax:success' use if you have used pjax
            $('.view').click(function(e){
                e.preventDefault();      
                    $('#pModal').modal('show')
                                .find('#modalContent')
                                .load($(this).attr('href'));  
            });
        });
    ");

        Modal::begin([
            //'header'=>'<span id="modalHeaderTitle"></span>',
            //'headerOptions'=>['id'=>'modalHeader'],
            'id'=>'pModal',
            'size'=>'modal-lg',
                    //keeps from closing modal with esc key or by clicking out of the modal.
                    // user must click cancel or X to close
                'clientOptions' => ['backdrop' => 'static', 'keyboard' => FALSE]
            ]);

            echo "<div id='modalContent'></div>";

            Modal::end();


    $this->registerJs(
        "$(document).on('ready pjax:success', function() {  // 'pjax:success' use if you have used pjax
            $('.update').click(function(e){
                e.preventDefault();      
                    $('#qModal').modal('show')
                                .find('#modalContent')
                                .load($(this).attr('href'));  
            });
        });
    ");

        Modal::begin([
            //'header'=>'<span id="modalHeaderTitle"></span>',
            //'headerOptions'=>['id'=>'modalHeader'],
            'id'=>'qModal',
            'size'=>'modal-lg',
                    //keeps from closing modal with esc key or by clicking out of the modal.
                    // user must click cancel or X to close
                'clientOptions' => ['backdrop' => 'static', 'keyboard' => FALSE]
            ]);

            echo "<div id='modalContent'></div>";

            Modal::end()
            ?>

请帮助我

2 个答案:

答案 0 :(得分:0)

'buttons'=>[
'view'=>function($url,$model){
    return Html::a('<span class="glyphicon glyphicon-eye-open")></span>',$url,['class'=>'view action-btn', 'data-pjax'=>0]);
},
'update'=>function($url,$model){
    return Html::a('<span class="glyphicon glyphicon-pencil")></span>',$url,['class'=>'update action-btn', 'data-pjax'=>0]);
                    }
                ],
            ],
        ],
两个按钮都添加了

action-btn。现在从您的视图中调用ajax并在其中编写模态代码。

$this->registerJs( "$.ajax({
           url: '$url',
           type: 'POST',
           data: {  },
           success: function(data) { 
            //your code                       
           }
        });");

$url = \Yii::$app->urlManager->createUrl(['/your-action-here']); 在要重定向的视图文件上写下modal代码。

答案 1 :(得分:0)

'buttons' => [
    'view' => function ($url, $model) {
        return Html::a(
            '<span class="glyphicon glyphicon-eye-open")></span>',
            $url,
            [
                'class' => 'openModal', 
                'data-pjax' => 0
            ]
        );
    },
    'update' => function ($url, $model) {
        return Html::a(
            '<span class="glyphicon glyphicon-pencil")></span>',
            $url,
            [
                'class' => 'openModal', 
                'data-pjax' => 0
            ]
        );
    },
]

<强>模态

Modal::begin([
    'id' => 'modal-popup',
    'size' => 'modal-lg',
    'clientOptions' => [
        'backdrop' => 'static', 
        'keyboard' => FALSE
    ]
]);
echo "<div id='modalContent'></div>";
Modal::end();

<强> JS

$this->registerJs(
    "$(document).on('ready pjax:success', function() {
        $('.openModal').click(function(e) {
            e.preventDefault();      
            $('#modal-popup').modal('show').find('#modalContent').load($(this).attr('href'));  
        });
    });
");