从yii2

时间:2017-04-14 08:33:42

标签: yii2

我有一个表单rawmaterial,我有一个带有checkboxcolumn和一些表单字段的Gridview。我想将所选行的所有字段都作为数组。 我试图解决这里提到的问题 - http://www.yiiframework.com/forum/index.php/topic/53777-gridview-get-selected-colum/。但是找不到错误。 index.php的代码

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

控制台 enter image description here

输出 enter image description here

2 个答案:

答案 0 :(得分:0)

为GridView提供一个id:

<?= GridView::widget([
  'id'=>'mytable',
   ...

在你的js代码中,请调用:

var rows = $('#mytable').yiiGridView('getSelectedRows');

答案 1 :(得分:0)

您可以尝试两种可能的(临时)灵魂:         解决方案1 ​​::

el.addEventListener("keydown", e => {
    if (e.which === 13) // Enter key, or which ever key code you'd like
        el.addEventListener("keyup", function keyUp(e) {
            el.removeEventListener("keyup", keyUp, false) // Memory clean-up

            // Your code here
        }, false)
}, false)

解决方案2

UIKeyInput