yii2 - 如何从gridview到控制器检查复选框值

时间:2017-11-10 08:48:21

标签: php yii2

按下按钮时,我需要从我的视图中选中复选框值到控制器。

Gridview:

<div class="installment-ready">
<!-- <h1><?= Html::encode($this->title) ?></h1> -->
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
    [
    'class' => 'yii\grid\SerialColumn',
    'contentOptions'=>[ 'style'=>'width: 60px'],
    ],
//Checkbox
[
    'class' => 'yii\grid\CheckboxColumn',
    'contentOptions'=>[ 'style'=>'width: 50px'],
    'name' => 'checked',
    'checkboxOptions'=> function($model, $key, $index, $column) {
     return ["value" => $model->ACCOUNT_ID];
    }
],

处理所选复选框的按钮:

<?= Html::a('Submit', ['#'], ['class' => 'btn btn-success pull-right']) ?>

2 个答案:

答案 0 :(得分:0)

controller操作中使用以下代码

Yii::$app->request->post('checked');

答案 1 :(得分:0)

我找到了解决方案。我使用ajax将所选复选框发布到我的控制器。

我在视图中使用的脚本

<script type="text/javascript">
  // action for all selected rows
  function submit(){
    var dialog = confirm("Are you sure to submit the installment?");
    if (dialog == true) {
        var keys = $('#grid').yiiGridView('getSelectedRows');
        // console.log(keys);
        var ajax = new XMLHttpRequest();
        $.ajax({
            type: "POST",
            url: 'index.php?r=installment/submit', // Your controller action
            data: {keylist: keys},
            success: function(result){
              console.log(result);
            }
          });
    }
  }
</script>
<!-- Submit button -->
<button type="button" onclick="submit()" class="btn btn-success pull-right">Submit</button

您可以在控制器中使用常用的Yii :: $ app-&gt; request-&gt; post()来获取您的数据。