Mike Ross
Mike Ross

Reputation: 2972

How to get value of checked gridview column

Hi guys i have a gridview like below and i want to get the 'user_id' of the checked column how can i do that???

I could easily get the checked column id but i dont know how to get data of those checked column in gridview i want to get it via javascript so that i can pass it to service

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'showOnEmpty'=>true,
        'columns' => [
            ['class' => 'yii\grid\CheckboxColumn'],
            [

                'attribute' => 'event_id',
                'label' => 'Event Title',
                'value' => 'event.title'
            ],
            [
                'attribute' => 'user_id',
                'label' => 'Email',
                'value' => 'users.email',
            ],
            'user_type',
        ],
    ]);
 ?>

and here is my javascript to get ids of checked column

jQuery(document).ready(function() {
btnCheck  = $("#send");
btnCheck.click(function() {
    var keys = $("#w0").yiiGridView("getSelectedRows");
  }
});

Let me tell you the flow of this

On homepage is a gridview like this enter image description here Now user will click on that small user sign and that will open the page you can see below

Thats when i want to send messages to all selected users

enter image description here Because in my case title is from different table and name and email are from different table so i want ids of user table

For that i want user_id but i am getting some random values What can i do here???

I tried this but its returning some random string

public function search($params)
{

    if(!isset($_GET['id'])){
        $id='';
    }
    else{
        $id=$_GET['id'];
    }

    $query = Checkin::find()->where(['event_id'=> $id]);

    $query->joinWith(['event', 'users']);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'created_date' => $this->created_date,
        'created_by' => $this->created_by,
        'updated_date' => $this->updated_date,
        'updated_by' => $this->updated_by,
    ]);

    $query->andFilterWhere(['like', 'user.email', $this->user_id]);
    $query->andFilterWhere(['like', 'user_type', $this->user_type]);

    $dataProvider->keys ='user_id';

    return $dataProvider;
}

Upvotes: 2

Views: 1699

Answers (2)

Double H
Double H

Reputation: 4160

Update your DataProvider set $dataProvider->keys ='userId' then you will able to get all keys of user_id

data-id of GridView and get allSelectedColumns

Upvotes: 2

GAMITG
GAMITG

Reputation: 3818

You need to just replace this code

['class' => 'yii\grid\CheckboxColumn'],

with below code

[
    'class' => 'yii\grid\CheckboxColumn',
    'checkboxOptions' => function($data) {
        return ['value' => $data->user_id];
    },
],

Upvotes: 0

Related Questions