Context menu

Datatable with context menu functionalities

by Junie Negentien

HTML

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

JavaScript

$(function() {
  $.contextMenu({
    selector: 'tr td',
    autoHide: true,
    selectableSubMenu: true,
    reposition: true,

    //start of the items in context menu_gcj
    items: {
      "print": {
        name: "Print",
        icon: "fa-print",
        callback: function(itemKey, opt) {
          return true;
        }
      },
      sep1: "--where--",
      // first submenu
      "export": {
        "name": "Export",
        icon: "fa-file-o",
        "items": {
          "pdf": {
            name: "Pdf",
            icon: "fa-file-pdf-o",
            callback: function(itemKey, opt) {
              return true;
            }
          },
          sep4: "--is--",
          "excel": {
            name: "Excel",
            icon: "fa-file-excel-o",
            callback: function(itemKey, opt) {
              return true;
            }
          },
        }
      },
      sep2: "--is--",
      // secondsubmenu
      "extra": {
        "name": "Extra\'s",
        icon: "fa-info-circle",
        "items": {
          "add": {
            name: "Supplemental",
            icon: "fa-plus-circle",
            callback: function($item, id) {
              // $('#gcjmodal').click(add_item());
              $(this).on('click', add_item());
              return true;
            }
          },
          sep3: "----",
          "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;
             ...