如何在yii2中从gridview中选择数据

时间:2016-04-18 07:54:48

标签: yii2

我必须将数据插入' production'表。 productid,productname,batchno存储在表' productbatch'中。我想在gridview上单击gridview时从gridview中选择这些字段。我怎样才能做到这一点? producbatch的index.php -

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\production\models\ProductbatchSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productbatches';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productbatch-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Productbatch', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\CheckboxColumn'],
            'itemid',
            'productname',
            'batchno:ntext',
            //'mfgdate',
            //'expdate',
            // 'mrp',
            // 'rate',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

</div>

_生产形式

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;
/* @var $this yii\web\View */
/* @var $model frontend\modules\production\models\Production */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="production-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'productiondate')->textInput() ?>

    <?= $form->field($model, 'itemid')->textInput() ?>

    <?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'batchno')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'prodqty')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php
    $this->RegisterJs ( "
    $('document').ready(function(){
        // $('#".Html::getInputId($model, 'mrp')."').keyup(function(){ 
        //              var total = this.value;
        //              var percent = 10;
        //              var discount_value = this.value - ((total / 100) * percent);
        //             $('#".Html::getInputId($model, 'rate')."').val(discount_value);    
        //          });
         var keys = $('#grid').yiiGridView('getSelectedRows');
    });

    ", View::POS_END);
  ?>

1 个答案:

答案 0 :(得分:3)

最简单的方法是基于检查列并获取数组中的重新显示的行

这是来自yii指南http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#checkbox-column

polys[[k+(v-1)*(length(xs))]] <- ...

用户可以单击复选框以选择网格行。可以通过调用以下JavaScript

来获取所选行
    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'id' => 'my_gridview_id',
        'columns' => [
            // ...
        [
            'class' => 'yii\grid\CheckboxColumn',
            // you may configure additional properties here
        ],
    ],