簡體   English   中英

從yii2中的選定復選框獲取數據

[英]Get data from selected checkboxes in yii2

我有一個表單原料,其中有一個帶有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
        }
    }

安慰 在此處輸入圖片說明

產量 在此處輸入圖片說明

為GridView提供一個ID,如下所示:

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

在您的js代碼中,調用此代碼:

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

您可以嘗試兩種可能的(臨時) 解決方案解決方案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  
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM