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 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景