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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?