我有一個原材料形式和我已經一個GridView與checkboxcolumn和某種形式的領域。我想要將所選行的所有字段作爲數組。 我試圖來解決這裏提到 - http://www.yiiframework.com/forum/index.php/topic/53777-gridview-get-selected-colum/。但沒有找到錯誤。的index.php 代碼獲得從選擇複選框數據yii2
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use frontend\models\Rmtemplate;
use kartik\form\ActiveForm;
/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\rmprod\models\RmtemplateSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Templates';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="rmtemplate-index">
<?php $form = ActiveForm::begin([
'id' => 'rmtemplate-index',
'action' => ['/rmprod/Rmtemplate/processSelected',],
'method' => 'get',
'enableClientScript' => false,
]); ?>
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'id'=>'mytable',
'columns' => [
['class' => 'kartik\grid\CheckboxColumn'],
//'id',
//'productname',
[
'attribute'=>'productname',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Rmtemplate::find()->orderBy(['productname' => SORT_ASC])->asArray()->all(), 'productname', 'productname'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Charge Name'],
],
'rmname',
'qty',
// [
// 'attribute' => 'unitcost',
// 'value' => 'unitcost.unitcost',
// ],
'cost',
//['class' => 'kartik\grid\ActionColumn'],
],
]); ?>
<div class="form-group pull-right">
<?= Html::submitButton('Next', ['class' => 'btn btn-primary search','id'=>'tabbutton',]) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php
/* start getting the data */
$script = <<<EOD
$('tabbutton').one('click',function() {
var keys = $('#w0').yiiGridView('getSelectedRows'); // returns an array of pkeys, and #grid is your grid element id
$.post({
url: '/rmtemplate/processSelected', // your controller action
dataType: 'json',
data: {keylist: keys},
success: function(data) {
if (data.status === 'success') {
alert('I did it! Processed checked rows.');
}
},
});
});
EOD;
$this->registerJs($script);
/*end getting the data */
?>
控制器動作
public function actionProcessSelected() {
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
// you will have the array of pk ids to process in $keys
// perform batch action on these keys and return status
// back to ajax call above
}
}
嗨gmc,我試過你的解決方案,並嘗試顯示陣列爲警報。但它是空的。我已經更新了問題中的當前代碼。也不知道它爲什麼重定向創建。到現在爲止,我並沒有在任何地方調用create action。 – Tanmay
被檢查的行然後在'rows'變量中,你不使用。把'的console.log(行)'和檢查JS控制檯,你會看到 – gmc
使用$( '#W0')yiiGridView( 'getSelectedRows')。爲價值。並檢查下面的鏈接also.it可能會幫助你http://www.yiiframework.com/forum/index.php/topic/53777-gridview-get-selected-colum/ –