0

我想通过使用Jquery contextmenu onclick 事件来编辑/删除表中一行中的数据。问题是当我在一行中单击时,最后一行将被提取,而不是在选定的行上。

桌子:

<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>

上下文菜单:

                        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;
                          }
                        },

资源

4

1 回答 1

1

使用data-*属性在 JS 代码中传递要编辑/删除的 ID。

更改以下内容。

从:

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

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

然后更新您的 JS 以提取.data('id')值。

更改以下内容。

从:

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

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

至:

// 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')));
于 2017-06-23T15:52:32.140 回答