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

Yii2: Get selected rows data from gridView checkbox columns into controller(Yii2:从 gridView 复选框列中获取选定的行数据到控制器中)
本文介绍了Yii2:从 gridView 复选框列中获取选定的行数据到控制器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于Yii2:从 gridView 复选框列中获取选定的行数据到控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)