2013-04-14 86 views
1

我已經改變了一些默認的opencart管理產品列表過濾功能,那如下:如果我需要從列表中刪除產品我替換複選框,只需單擊每個產品刪除圖標,截圖附加這裏...自定義產品刪除帶確認對話框

enter image description here

其工作正常,但我怎麼能設定對話框確認刪除的jQuery功能?,像我們的默認opencart產品刪除確認...當點擊刪除按鈕問確認刪除?確認並取消按鈕對話框...任何想法?

我的產品刪除按鈕,在HTML列表:

<?php if ($products) { ?> 
<?php foreach ($products as $result) { ?>  
<div class="product">  
    <a href="<?php echo $result['delete']; ?>"><img src="delete.png" title="<?php echo $button_delete; ?>" /></a> 
    <img src="<?php echo $result['image']; ?>" alt="<?php echo $result['name']; ?>"/> 
    <a href="<?php echo $result['view']; ?>"><?php echo $result['name']; ?></a>   
</div> 
<?php } ?> 
<div class="pagination"></div> 
<?php } else { ?> 
<div class="empty"><?php echo $text_empty; ?></div> 
<?php } ?> 

從上面的代碼刪除按鈕:

<a href="<?php echo $result['delete']; ?>"><img src="delete.png" title="<?php echo $button_delete; ?>" /></a> 

任何想法?

回答

1

非jQuery的

的HTML:

<a href="#" onclick='confirm_delete("<?php echo $result['delete']; ?>", "<?php echo $result['name']; ?>"); return false;'> 
    <img src="delete.png" title="<?php echo $button_delete; ?>" /> 
</a> 

的JavaScript:

<script> 
function confirm_delete(link_to_delete, product_name) { 
    var msg=confirm("Are you sure you want to delete " + product_name + " ?"); 
    if (msg==true) 
    { 
    window.location.href = link_to_delete; 
    } 
} 
</script> 

jQuery的

的HTML:

<a class="delete_button" href="<?php echo $result['delete']; ?>"> 
    <img src="delete.png" title="<?php echo $button_delete; ?>" /> 
</a> 

的JavaScript(jQuery的):

<script> 
jQuery(".delete_button").on("click", function(e) { 
    e.preventDefault(); 
    var clicked = jQuery(this); 
    var clicked_url = clicked.attr("href"); 
    var product_name = clicked.siblings("img").attr("alt"); 
    var msg=confirm("Are you sure you want to delete " + product_name + " ?"); 
    if (msg==true) 
    { 
    window.location.href = clicked_url; 
    } 
}); 
</script> 
0

你可以通過利用jQuery的一點: -

$('div.product a img[src="delete.png"]').on('click', function(event){ 
    if(! confirm('Delete?')) { 
     event.preventDefault(); 
     return false; 
    } 
    return true; 
}); 
0

以下是我做這件事是項目的一排,每一個都是稱爲「優惠」。這是模板代碼:

<a href="<?php echo $offer['delete']; ?>" data-toggle="tooltip" title="<?php echo $button_delete; ?>" class="btn btn-primary" onclick="confirm('<?php echo $text_confirm; ?>') ? $('#form-discount_chooser').submit() : false;"><i class="fa fa-trash-o"></i></a> 

之前要調用模板,您的控制器將設置$offer['delete']如下:

 $data['offers'][] = array(
      ... 
      'delete' => $this->url->link('extension/module/discount_chooser/delete', 'token=' . $this->session->data['token'] . '&offer=' . $discount['id'] . $url, true) 
     ); 

然後在控制器的delete()方法時,一定要處理的事實,這是一個單一的項目,而不是一個或多個複選框:

// Handle onesies 
if (!isset($this->request->post['selected'])) { 
    if (isset($this->request->get['offer'])) { 
    $this->request->post['selected'] = array($this->request->get['offer']); 
    } 
}