自定义按钮操作列,yii2

时间:2015-12-08 02:30:11

标签: javascript yii2

我想使用模态弹出窗口更改ActionColumn(GridView :: widget)上的默认按钮。这是我的代码:

            ['class' => 'yii\grid\ActionColumn',
            'contentOptions' => ['style' => 'width: 8.7%'],
            'visible'=> Yii::$app->user->isGuest ? false : true,
            'buttons'=>[
                'view'=>function ($url, $model) {
                    $t = 'index.php?r=site/view&id='.$model->id;
                    return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs', 'id'=>'modalButtonView']);
                },
                'update'=>function ($url, $model) {
                    $t = 'index.php?r=site/update&id='.$model->id;
                    return Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs', 'id'=>'modalButtonView']);
                },
            ],
        ],

只有第一个按钮效果很好,另一个按钮没有效果。我应该改变什么?我可以使用锚标签代替按钮吗? 这是我的js和ModalWidget:

$(function(){
$('#modalButtonView').click(function(){
    $('#modalView').modal('show').find('#modalContentView').load($(this).attr('value'));

});});


        Modal::begin(['id'=>'modalView', 'size'=>'modal-md']);
    echo "<div id='modalContentView'></div>";
    Modal::end();

4 个答案:

答案 0 :(得分:2)

html中的基本规则。

ID必须唯一。

使用此代码

    ['class' => 'yii\grid\ActionColumn',
    'contentOptions' => ['style' => 'width: 8.7%'],
    'visible'=> Yii::$app->user->isGuest ? false : true,
    'buttons'=>[
        'view'=>function ($url, $model) {
            $t = 'index.php?r=site/view&id='.$model->id;
            return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs custom_button']);
        },
        'update'=>function ($url, $model) {
            $t = 'index.php?r=site/update&id='.$model->id;
            return Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs custom_button']);
        },
    ],
],

然后yor jquery

$(function(){
$('.custom_button').click(function(){
    $('#modalView').modal('show').find('#modalContentView').load($(this).attr('value'));

});});

    Modal::begin(['id'=>'modalView', 'size'=>'modal-md']);
echo "<div id='modalContentView'></div>";
Modal::end();

答案 1 :(得分:1)

您在按钮html选项中使用id ... Id对于Html元素是唯一的。

而不是id,你必须使用setState()的类并根据它改变你的JS ..

答案 2 :(得分:0)

如果您不想使用Html :: button(使用jquery)。您可以使用Html :: a:

['class' => 'yii\grid\ActionColumn',
            'contentOptions' => ['style' => 'width: 7%'],
            'visible'=> Yii::$app->user->isGuest ? false : true,
            'buttons'=>[
                'view'=>function ($url) {
                    return Html::a('', $url, ['class' => 'glyphicon glyphicon-eye-open btn btn-default btn-xs custom_button']);
                },
                'update'=>function ($url) {
                    return Html::a('', $url, ['class' => 'glyphicon glyphicon-pencil btn btn-default btn-xs custom_button']);
                },
            ],
        ],

答案 3 :(得分:-2)

这个例子很容易理解我  有一个疑问

`['class' => 'yii\grid\ActionColumn',
                        'contentOptions' => ['style' => 'width: 8.7%'],
                        'visible'=> false,
                        'buttons'=>[
                                'view'=>function ($url, $model) {
                                    $t = 'index.php?r=site/view&id='.$model->id;
                                    return Html::button('', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs custom_button']);
                                },
                                'update'=>function ($url, $model) {
                                    $t = 'index.php?r=site/update&id='.$model->id;
                                    return Html::button('', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs custom_button']);
                                },
                                ],
                                ],`