Search code examples
phpjquerycontextmenucodeigniter-3jquery-ui-contextmenu

fetch data on selected row using 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


Solution

  • 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')));