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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?