Moeez
Moeez

Reputation: 478

How to get column value rather than ID in yii2 by using javascript

I want to display the value(s) of the column which is in my table. I have a Gridview in which I have placed a checkbox column. On selecting the check box and clicking on the to show the value(s) I have placed a javascript in which I have used a loop that will collect the information of the selected check box. But on click on the button I am able to see only the checkbox ID. Below is what I have done

Action

    $sql = "SELECT DISTINCT  ms.`meter_msn` AS 'Meter Serial Number', u.`name` AS 'Issued To',ps.`name` AS 'Store' FROM `meters` ms
INNER JOIN `ogp_header` ogph ON ms.`issued_user` = ogph.`issuer` 
INNER JOIN `project_store` ps ON ogph.`store_id` = ps.`id` 
INNER JOIN `user` u ON `ogph`.`issuer` = u.`id`
$where AND ms.`meter_status` = 'Installation Ready' AND ogph.`status` IN ('Prepared', 'Canceled')
ORDER BY ms.`id` DESC "
$dataProvider = new SqlDataProvider([
       'sql' => $sql, // $sql is the resulted query
       'totalCount' => $count,
        'pagination' => [
               'pageSize' => 30,
           ],
   ]);


    return $this->render('viewcreated', [
        'dataProvider' => $dataProvider,
        'model' => $this->findModel($id),
        'id'=> $model->id
        /*'searchModel' => $searchModel*/
    ]);

My view

<?= GridView::widget([
         'dataProvider' => $dataProvider,
          /*'filterModel' => $searchModel,*/
          'id'=>'grid',
       'columns' => [

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

        'Meter Serial Number',
        'Issued To',
        'Store',           

 ],
<button type="button" class="btn btn-success" id="myid">View Selected Rows</button>

In my javascript i have done

$(document).ready(function () {      

 $('#myid').click(function() {

    var strValue = "";

    $('input[name="selection[]"]:checked').each(function() {

    if(strValue!="")
        {
        strValue = strValue + " , " + this.value;

        }
    else 
        strValue = this.value;


});
     alert(strValue);
 })  });

Now when i click on the button I only see the ID, but I want to display the value(s) of Meter Serial Number.

Here is the view when I click on the button here is the screenshot

I have searched for it but unable to find a solution

Any help would be highly appreciated.

Upvotes: 0

Views: 829

Answers (1)

bipin panday
bipin panday

Reputation: 119

If you can give html code of this elements this would be better for give you working code.

you can get this value by targeting some identity of this element which may be related to Id( which is unique to each element). And you have to change -> this.value <- by some other code.

 if(strValue!="")
    {
    strValue = strValue + " , " + this.value;

    }
else 
    strValue = this.value;

Upvotes: 1

Related Questions