2017-06-21 17 views
0

我想在上下文菜單中調用一個函數。我嘗試了按鈕,它完美的作品。當我嘗試放置在上下文菜單中時,我無法調用該函數。我使用這個庫https://github.com/swisnl/jQuery-contextMenu作爲上下文菜單。Jquery contextmenu調用函數

我的表:

<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>      
       </tr> 
       <?php }?> 

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

我的上下文菜單:

"edit": { 
     name: "Edit", 
     icon: "fa-pencil-square-o", 
     callback: function(item, id) { 
     $('#gcjmodal').on('click', edit_item('$item->id')); 
     // $('#gcjmodal').click(edit_item('$item->id')); 
     return true; 
     } 
     }, 
"delete": { 
     name: "Delete", 
     icon: "fa-trash-o", 
     callback: function(item, id) { 
     //$(this).delete_item('$item->id'); 
     // $(this).on('click', delete_item('$item->id')); 
     return true; 
     } 
     }, 

我的功能:

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'); 
      }, 
      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 site_url('ppmp/delete_item')?>/" + id, 
       type: "POST", 
       dataType: "JSON", 
       success: function(data) { 
        location.reload(); 
       }, 
       error: function(jqXHR, textStatus, errorThrown) { 
        alert('Error deleting data'); 
       } 
      }); 
+0

什麼是'gcjmodal'?這將在調用上下文菜單時可用,我問這是因爲在上下文菜單回調中,您將單擊事件處理程序附加到'gcjmodal'。 –

+0

@BhushanKawadkar,它來自插件iziModal的模式。當我嘗試編輯/刪除這是錯誤。 'POST http:// localhost/csc/ppmp/delete_item/$ item-%3Eid 400(Bad Request)' – Larigyn

回答

1

問題的代碼行是

$('#gcjmodal').on('click', edit_item('$item->id')); 

當您將字符串傳遞給edit_item函數(即, '$item->id')。

您需要使用PHP標籤解析該文件。所以行應該是:

$('#gcjmodal').on('click', edit_item('<?php print $item->id ?>')); 
+0

,這就是缺少的一段代碼。你說得對,先生。感謝您的幫助 – Larigyn

+0

先生,當我嘗試編輯。最後一行數據已被提取。我想選擇其他數據行。 – Larigyn

+0

'$ item'將始終引用數組中的最後一個元素。您需要將每行的ID動態傳遞到edit_item()函數中。 – steadweb