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

使用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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。