doupiai5597
2017-11-10 16:36
浏览 70
已采纳

如何使用javascript获取列值而不是yii2中的ID

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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doukuang1897 2017-11-11 11:59
    已采纳

    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;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题