Yii2.0 GridView 批量选择

大概要实现下面的图的效果,批量通过和批量拒绝

如

View 页面


<?= GridView::widget([

    // ------👇下面这坨-----
    "options" => ["class" => "grid-view","style"=>"overflow:auto", "id" => "grid"],// 这个id很重要
    // ------👆上面这坨------

    'dataProvider' => $dataProvider,
    'hover'=>true,
    'floatHeader'=>true,
    'floatHeaderOptions'=>['scrollingTop'=>'0','class'=>'col-sm-3'],
    'filterModel' => $searchModel,
    'columns' => [
        // ------👇下面这坨-----
        [
         'class' => 'yii\grid\CheckboxColumn',//多选
         'name'  => 'id',
         'headerOptions' => ['width'=>'30'],
         'footer' => '<button href="#" class="btn btn-default btn-xs btn-delete"
                    url="'. Url::toRoute('admin/delete') .'">删除</button>',
         'footerOptions' => ['colspan' => 5],  //设置删除按钮垮列显示;
        ],
        // ------👆上面这坨------

        // ['class' => 'yii\grid\SerialColumn'],
        'id',
        'key',
        ],
        // ['class' => 'yii\grid\ActionColumn'],
    ],
]);
?>
//

<?php
// ------👇下面这坨-----
$this->registerJs('
$(".gridview").on("click", function () {
    var msg = "请确认您的操作?\n\n请选择!";
    if (confirm(msg)===true){
        var keys = $("#grid").yiiGridView("getSelectedRows");
        if (keys=="") {
            return confirm("请勾选数据!");
        }
        console.log(keys);
        $.post("ids?id="+keys);// 控制器的方法
    }else{
        console.log("用户取消操作");
        return false;
    }
});
');
?>

// 按钮
<?= Html::a('批量通过', "javascript:void(0);", ['class' => 'btn btn-success gridview']) ?>
// ------👆上面这坨------

控制器

public function actionIds($id)
{
    // 业务逻辑
}

但是,上面的不够智能,我想传递不同的参数(参数为控制器方法)实现不同的控制器方法

按钮:

<button class = 'btn btn-success gridview' type="button" onclick="dosr('suc');">批量通过</button>
<button class = 'btn btn-success gridview' type="button" onclick="dosr('ref');">批量拒绝</button>
<script>
function dosr(action)
{
    if (action ==="suc") {
        var msg = "\n\n确定批量通过吗?";
    }
    if (action ==="ref") {
        var msg = "\n\n确定批量拒绝吗?";
    }
    if (confirm(msg)===true){
        var keys = $("#grid").yiiGridView("getSelectedRows");
        if (keys=="") {
            return confirm("请选择数据!");
        }
        console.log(keys);
        $.post(action+"?id="+keys);
    }else{
        return false;
    }
}
</script>

控制器

// 批量通过
public function actionSuc($id)
{
    Yii::$app->myfun->record($id,'suceessss');
}
// 批量拒绝
public function actionRef($id)
{
    Yii::$app->myfun->record($id,'rerererererererere');
}



导航