Yii2:从 gridView 复选框列中获取选定的行数据到控制器中

2022-01-07 00:00:00 php javascript yii2 kartik-v

我在 Yii2 项目中查看了页面(index.php),并且我正在使用 Kartik gridView 来显示数据

I've view page(index.php) in my Yii2 project, and I'm using Kartik gridView for showing the data

在视图的右侧,我有一个复选框列.我有一个导出按钮.我想将选定的名称(由复选框选择)导出到 name.txt 文件中.

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.

我终于实现了导出功能,但我不知道如何将选定的数据从视图中获取到控制器中.

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:

我把这段 javascript 代码放在我的视图中 index.php:

I put this javascript code in my view 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.')
        },
    });
}

并像这样设置导出按钮:

<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.

`<?php Pjax::begin(); ?>
<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table table-hover'],
    'columns' => [
        ['class' => 'yiigridSerialColumn',
            'header' => 'No',
        ],
        [
            'label' => 'Name',
            'value' => function($data) {
            return $data->name;
    }
        ],
        ['class' => 'kartikgridCheckboxColumn'],
    ],
    '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

推荐答案

通过检查元素在checkbox列你可以找到行的名称(复选框名称).它包含 id 作为 value.

By inspect element on checkbox column you can find name of row ( checkbox name ). it contain id as value.

从中您可以找到选择了多少行.

from that you can find how many rows are selected.

就我而言,我在复选框名称中得到选择[]".

in my case i get 'selection[]' in checkbox name.

例如

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

我编写了 jquery 代码来获取下面的选定行.

i write jquery code to get selected rows below.

<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>

相关文章