Yii2:从gridView复选框列中选择行数据到控制器 [英] Yii2: Get selected rows data from gridView checkbox columns into controller
问题描述
我在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' => '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> <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
推荐答案
通过复选框列中的检查元素,您可以找到<行的强>名称(复选框名称)。它包含 id 为值。
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.
ex。
<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复选框列中选择行数据到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!