2017-04-14 38 views
0

我有一個原材料形式和我已經一個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 
     } 
    } 

控制檯 enter image description here

輸出 enter image description here

回答

0

給像一個ID到GridView:

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

而在你的js代碼,稱之爲:

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

嗨gmc,我試過你的解決方案,並嘗試顯示陣列爲警報。但它是空的。我已經更新了問題中的當前代碼。也不知道它爲什麼重定向創建。到現在爲止,我並沒有在任何地方調用create action。 – Tanmay

+0

被檢查的行然後在'rows'變量中,你不使用。把'的console.log(行)'和檢查JS控制檯,你會看到 – gmc

+0

使用$( '#W0')yiiGridView( 'getSelectedRows')。爲價值。並檢查下面的鏈接also.it可能會幫助你http://www.yiiframework.com/forum/index.php/topic/53777-gridview-get-selected-colum/ –

0

有兩種可能的(臨時)soutions你可以嘗試: 解決方案1 ​​::

In Index.php=> 
    //define action :: 
    $productupdate = '/rmprod/Rmtemplate/processSelected'; 
    In column add this........ 
    [ 
    'class' => 'kartik\grid\CheckboxColumn' 
    'checkboxOptions' => function ($data) { 
       return ['value' => $data['product_primary_key'], 'class' => 'class_checkbox']; //primary_key=>product_primary_key 
         }, 
    ], 
    Add Jquery code(To post data) :::: 
     $('.class_checkbox').change(function() 
     { 
      var action_url = '<?= $productupdate; ?>'; 
      if (this.checked) { 
       var product_data = $(this).attr('value'); //get product_primary_key 
       $.ajax({ 
        method: "post", 
        url: action_url, 
        data: {product_data:product_data,_csrf: csrfToken} 
       }).done(function(data) 
       { 
        $('#LoadingMSG').hide(); 
        console.log(data); 
       }); 
      } 
      else 
      { 
       //you can apply another ajax to post data when checkbox unselect 
      } 
     }); 
    In Controller=> 
    public function processSelected() 
    { 
    $getdata = Yii::$app->request->post(); //get posted data 
    //apply query to play with database by $getdata (primary_key) 
    } 

解決方案2

In Index.php............. 
    [ 
    'class' => 'kartik\grid\CheckboxColumn' 
    'checkboxOptions' => function ($data) { 
       return ['value' => $value1.'+'.$value2.'+'.$value3.'+'.$value4, 'class' => 'class_checkbox']; 
         }, 
    ], 
    Add Jquery code(To post data) :::: 
     $('.class_checkbox').change(function() 
     { 
      var action_url = '<?= $productupdate; ?>'; 
      if (this.checked) { 
       var all_flds_data = $(this).attr('value'); 
       $.ajax({ 
        method: "post", 
        url: action_url, 
        data: {all_flds_data:all_flds_data,_csrf: csrfToken} 
       }).done(function(data) 
       { 
        $('#LoadingMSG').hide(); 
        console.log(data); 
       }); 
      } 
      else 
      { 
       //you can apply another ajax to post data when checkbox unselect 
      } 
     }); 
In Controller=> 
    public function processSelected() 
    { 
    $getdata = Yii::$app->request->post(); //get posted data 
    //after json_deocode you will get all posted data from ajax 
    } 
+0

如果您可以解釋相同的解決方案和假設,將會非常有幫助 –