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

我必须将数据插入“生产”表. 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);
  ?>

解决方法

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

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

echo GridView::widget([
        'dataProvider' => $dataProvider,'id' => 'my_gridview_id','columns' => [
            // ...
        [
            'class' => 'yii\grid\CheckBoxColumn',// you may configure additional properties here
        ],

用户可以单击复选框以选择网格的行.可以通过调用以下JavaScript来获取所选行

var keys = $('#my_gridview_id').yiiGridView('getSelectedRows');

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...