使用gridview

时间:2015-08-03 19:36:51

标签: gridview yii2 yii2-advanced-app

你能给我一个关于如何在yii2 gridview中获取所选行的简单基本示例。我已经尝试过在论坛上存在的所有示例,但它不起作用。我收到了这个错误:无法读取属性' selectioncolumn'未定义的

这是我的代码:

视图:

<?= GridView::widget([
    'dataProvider'=> $dataProvider,
    'filterModel' => $searchInstance,
        //'containerOptions' => ['class' => 'instance-pjax-container'],
        'id' => 'grid',

        'export' => false,
    'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'codebien',
            'designationbien',
            'codesousfamille',
            'numfacture',
            'dt',
            ['class' => 'yii\grid\CheckboxColumn'],

            ['class' => 'yii\grid\ActionColumn'],
        ],


]);?>


  <?= Html::SubmitButton( 'Affecter', [ 'class' => 'btn btn-success' , 'id' =>'x']) ?>    



  <?php
$script = <<< JS
$(function () {
         $('#x').click(function(){

            $.post(
   [ "listeaffecter", 
    {
        pk : $('#grid').yiiGridView('getSelectedRows')
    },]

);

         });

  });

JS;
$this->registerJs($script);
?>

这是我的控制器

public function actionListeaffecter(){

                    $searchInstance = new InstanceSearch();
                    $dataProvider = $searchInstance->search(Yii::$app->request->queryParams);
                    $pk = Yii::$app->request->post('pk'); 
                    if ($pk) {
                        print ($pk);
                    }

                         return $this->render('vueListeAaffecter', [
                            'searchInstance' => $searchInstance,
                            'dataProvider' => $dataProvider,
                            ]);

                }

1 个答案:

答案 0 :(得分:1)

你可以试试这种方式

$('#your-grid-id').yiiGridView('getSelectedRows');

在你的情况下

$('#grid').yiiGridView('getSelectedRows');