简体   繁体   English

Yii2:从gridView复选框列中选择行数据到控制器

[英]Yii2: Get selected rows data from gridView checkbox columns into controller

I've view page( index.php ) in my Yii2 project, and I'm using Kartik gridView for showing the data 我在Yii2项目中查看了页面( index.php ),我正在使用Kartik gridView来显示数据

This the view from index.php: 这是index.php的视图:

在此输入图像描述

On the right side of view, I've a checkbox column. 在视图的右侧,我有一个复选框列。 And I've an Export button. 我有一个导出按钮。 I want to export the selected name (selected by checkbox) into name.txt file. 我想将所选名称(由复选框选中)导出到name.txt文件中。

I've finally make the export function, but I don't know how to get the selected data from view into controller. 我终于制作了导出功能,但我不知道如何将所选数据从视图中获取到控制器中。

I've try suggestions that I got from many forums, for example: 我尝试过从很多论坛获得的建议,例如:

I put this javascript code in my view index.php : 我把这个javascript代码放在我的视图index.php

<script>
function getRows(){
    var keys = $('#grid').yiiGridView('getSelectedRows');
    $.post({
        url: FakturOutController / exportAction,
        dataType: 'json',
        data: {keylist: keys},
        success: function(data) {
            alert('I did it! Processed checked rows.')
        },
    });
}

and set the export button like this: 并设置导出按钮,如下所示:

<p>
    <button type="button" onclick="getRows()" class="btn btn-success">Export</button>
</p>

But I got nothing, the button didn't showed any action/reaction when clicked. 但我没有得到任何东西,点击时按钮没有显示任何动作/反应。

This is the gridView code in index.php: 这是index.php中的gridView代码:

`<?php Pjax::begin(); ?>
<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table table-hover'],
    'columns' => [
        ['class' => 'yii\grid\SerialColumn',
            'header' => 'No',
        ],
        [
            'label' => 'Name',
            'value' => function($data) {
            return $data->name;
    }
        ],
        ['class' => '\kartik\grid\CheckboxColumn'],
    ],
    'toolbar' => [
        ['content' =>
            Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => false, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
        ],
        '{export}',
        '{toggleData}'
    ],
    'panel' => [
        'heading' => '<i class="glyphicon glyphicon-align-left"></i>&nbsp;&nbsp;<b>Data</b>',
        'before' => '', //IMPORTANT
    ],
]);
?>
<?php Pjax::end(); ?>

<?=
    Html::a('<i class=" glyphicon glyphicon-export"></i> Export', ['export', 'userId' => $userId], ['class' => 'btn btn-success']);
?>`

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

By inspect element on checkbox column you can find name of row ( checkbox name ). 通过检查复选框元素 ,你可以找到行( 复选框名称 )的名称 it contain id as value . 它包含id作为

from that you can find how many rows are selected. 从中可以找到选择了多少行。

in my case i get ' selection []' in checkbox name. 在我的情况下,我在复选框名称中得到' selection []'。

ex. 恩。

<input type="checkbox" class="kv-row-checkbox" name="selection[]" value="1">

i write jquery code to get selected rows below. 我写了jquery代码来获取下面的选定行。

<script>

    function getRows()
    {
        var strvalue = "";
        $('input[name="selection[]"]:checked').each(function() {
            if(strvalue!="")
                strvalue = strvalue + ","+this.value;
            else
                strvalue = this.value;
        });
     // strvalue contain selected row by comma separated      
    $.post({
        url: FakturOutController / exportAction,
        dataType: 'json',
        data: {keylist: keys},
        success: function(data) {
            alert('I did it! Processed checked rows.')
        },


       });
    }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM