Junie30 0 Newbie

Hello,

I'm trying to edit data from a selected table row by using contextmenu. But when I try to select a row for edting instead the LAST DATA in a row will fetch.

Table:

<div class="container">
      <div class="row">
        <div class="col-md-12">
          <div class="table-responsive">
            <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>
                  <th>Actions</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>

                  <td>
                    <button class="btn btn-warning" onclick="edit_item(<?php echo $item->id;?>)"><i class="glyphicon glyphicon-pencil"></i></button>
                    <button class="btn btn-danger" onclick="delete_item(<?php echo $item->id;?>)"><i class="glyphicon glyphicon-remove"></i></button>

                  </td>
                </tr>
                <?php }?>

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

contextmenu:

"edit": {
                              name: "Edit",
                              icon: "fa-pencil-square-o",
                              callback: function(itemKey, options) {                               
                                $('#ppmpsupplies').on('click', 'tr', edit_item( <?php echo $item->id ?> ));
                                // 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 ?>));
                                var m = "clicked: " + itemKey;
                                window.console && console.log(m) || alert(m);
                                return true;
                              }
                            },

function:

 function edit_item(id) {
        save_method = 'update';
        $('#gcjform')[0].reset();
        $.ajax({
          url: "<?php echo site_url('ppmp/ajax_edit/')?>/" + id,
          type: "GET",
          dataType: "JSON",
          success: function(data) 
          {

            $('[name="id"]').val(data.id);
            $('[name="description"]').val(data.description);
            $('[name="unit"]').val(data.unit);
            $('[name="quantity"]').val(data.quantity);
            $('[name="budget"]').val(data.budget);
            $('[name="mode"]').val(data.mode);

            $('#gcjmodal').iziModal('open');
            $('#gcjmodal').iziModal('setTitle','Editor');
            $('#gcjmodal').iziModal('setSubtitle','Project Procurement Management Plan'); 
          },
          error: function(jqXHR, textStatus, errorThrown) {
            alert('Error get data from ajax');
          }
        });
      }

       function delete_item(id) {
        if (confirm('Are you sure delete this data?')) {
          $.ajax({
            url: "<?php echo base_url('ppmp/delete_item')?>/" + id,
            type: "POST",
            dataType: "JSON",
            success: function(data) {
              location.reload();
            },
            error: function(jqXHR, textStatus, errorThrown) {
              alert('Error deleting data');
            }
          });

        }
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.