You could create a deleteItem action in InvoicesController but the
only reason I would do that is if I needed to do something with the
Invoice at the same time. (And so would pass both the Invoice.id and
Item.id) If not, just create an admin_delete action in ItemsController
and use AJAX.
foreach($data['Item'] as $item)
{
?>
<div id="item_<?= $item['id'] ?>">
...
<?= $html->link(
'delete',
array(
'admin' => 1,
'controller' => 'Items',
'action' => 'delete',
'id' => $item['id']
),
array(
'title' => 'delete this item',
'class' => 'ItemDelete',
'rel' => $item['id'] // so we don't need a regex to parse the id from URL
)
) ?>
</div>
<?php
}
Basic JQuery:
$('a.ItemDelete').click(function(ev){
ev.preventDefault();
var id = $(this).attr('rel');
$.ajax({
url: $(this).attr('href'),
success:
function(data, textStatus, jqXHR)
{
// ...
$('#item_' + id).remove();
}
});
});
See the manual for howto handle AJAX calls from within your controller actions.
If you don't want to use AJAX, you have to pass the Invoice.id as well
so that Cake knows where to redirect to.
On Tue, Jan 1, 2013 at 8:05 AM, Insert Code <contact@insertcode.co.uk> wrote:
> Hi all,
>
> I'm new to CakePHP and I could do with some newbie advice please.
>
> I'm creating an invoicing application and the way I have it set up is with
> seperate tables (and so Models and Controlllers) for Invoices and Items. The
> Invoice table holds the data such as 'Invoice Number', 'Date' and 'Client
> ID' for example, and the Items table stores individual records/items that
> make up each invoice with 'Quantity', 'Unit Price' etc. The two are
> associated correctly.
>
> When I call the 'edit' view for Invoices, obviously passing an ID, I
> retrieve the record and then also loop through the all associated records
> returned from the Items table to - all works as planned and are retrieved
> and displayed correctly.
>
> What I want to be able to do from this view though, is to be able to delete
> individual items from the invoice by clicking on a delete button next to
> each - ideally with a confirmation box. How would this be acheived exactly?
> What would be the best method? Would I use a function in the Items
> controller to do this without leaving the page? How would I call the
> function in that controller from the Invoice controller/view? Or would
> another method such as the CakePHP query() function be used for this?
>
> I hope I've explained this setup clearly enough for someone to offer any
> help and/or advice for which I would be very thankful.
>
> Thanks in advance,
>
> Adam
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
Tuesday, January 1, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment