Jquery 调用函数的上下文菜单
Jquery contextmenu calling a function
我想在上下文菜单中调用一个函数。我尝试在按钮上工作,它完美地工作。当我试图放置在上下文菜单中时,我无法调用该函数。我将此库 https://github.com/swisnl/jQuery-contextMenu 用于上下文菜单。
我的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>
<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');
}
});
违规代码行是
$('#gcjmodal').on('click', edit_item('$item->id'));
因为您将字符串传递给 edit_item
函数(即 '$item->id'
)。
您需要用 PHP 标签解析它。所以该行应该是:
$('#gcjmodal').on('click', edit_item('<?php print $item->id ?>'));
我想在上下文菜单中调用一个函数。我尝试在按钮上工作,它完美地工作。当我试图放置在上下文菜单中时,我无法调用该函数。我将此库 https://github.com/swisnl/jQuery-contextMenu 用于上下文菜单。
我的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>
<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');
}
});
违规代码行是
$('#gcjmodal').on('click', edit_item('$item->id'));
因为您将字符串传递给 edit_item
函数(即 '$item->id'
)。
您需要用 PHP 标签解析它。所以该行应该是:
$('#gcjmodal').on('click', edit_item('<?php print $item->id ?>'));