Yii2: Get selected rows data from gridView checkbox columns into controller(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> <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 复选框列中获取选定的行数据到控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii2:从 gridView 复选框列中获取选定的行数据到控制器中
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01