2017-06-21 10 views
0

コンテキストメニューの中で関数を呼び出したい。私はボタンに取り組んだり、それは完全に動作します。コンテキストメニューに配置しようとしたとき、私はその関数を呼び出すことができません。このライブラリをコンテキストメニューに使用しました。https://github.com/swisnl/jQuery-contextMenu関数を呼び出すJqueryコンテキストメニュー

マイテーブル:

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

My機能:

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からのモーダルです。これを編集/削除しようとするとエラーになります。 '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

関連する問題