dsfhd78978 2017-06-07 15:18
浏览 175
已采纳

使用contextmenu获取所选行的数据

I want to edit/delete data in a row within a table by using the Jquery contextmenu onclick event. The problem is when i click in a row the last row will be fetched rather than on the selected row.

table:

<table id="ppmpsupplies" class="table table-bordered table-hover" cellspacing="0" width="100%">
          <thead>
            <tr>
              <th>Code</th>
              <th>General Description</th>
              <th>Unit</th>
              <th>Quantity</th>
              <th>Estimated Budget</th>
              <th>Mode of Procurement</th>                  

            </tr>
          </thead>
          <tbody>
            <?php foreach($items as $item){?>
            <tr>
             <td><?php echo $item->id;?></td>
             <td><?php echo $item->description;?></td>
             <td><?php echo $item->unit;?></td>
             <td><?php echo $item->quantity;?></td>
             <td><?php echo $item->budget;?></td>
             <td><?php echo $item->mode;?></td>                     
          </tr>
          <?php }?>

        </tbody>
        <tfoot>
          <td colspan="3"></td>
          <td>Total</td>
          <td></td>
        </tfoot>
      </table>

context menu:

                        sep3: "----",
                        "edit": {
                          name: "Edit",
                          icon: "fa-pencil-square-o",
                          callback: function(itemKey, options) {
                            $('#ppmpsupplies tbody tr').on('click', edit_item(<?php print $item->id ?>));
                            // var m = "clicked: " + itemKey;
                            // window.console && console.log(m) || alert(m);
                            return true;
                          }
                        },
                        sep4: "----",
                        "delete": {
                          name: "Delete",
                          icon: "fa-trash-o",
                          callback: function(itemKey, options) {
                            $('#ppmpsupplies tbody tr').on('click', delete_item(<?php print $item->id ?>));                                 
                            return true;
                          }
                        },

source

  • 写回答

1条回答 默认 最新

  • dsf4s5787 2017-06-23 15:52
    关注

    Use data-* attributes to pass the ID you want to edit / delete within your JS code.

    Change the following.

    From:

    <tbody>
    <?php foreach($items as $item){?>
    <tr>
    

    To

    <tbody>
    <?php foreach($items as $item){?>
    <tr data-id="<php print $item->id?>">
    

    Then update your JS to pull out the .data('id') value.

    Change the following.

    From:

    // Edit
    $('#ppmpsupplies tbody tr').on('click', edit_item(<?php print $item->id ?>));
    
    // Delete
    $('#ppmpsupplies tbody tr').on('click', delete_item(<?php print $item->id ?>));
    

    To:

    // Edit
    var that = $(this);
    $('#ppmpsupplies tbody tr').on('click', edit_item(that.data('id')));
    
    // Delete
    var that = $(this);
    $('#ppmpsupplies tbody tr').on('click', delete_item(that.data('id')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测