On Mon, Sep 28, 2009 at 3:27 PM, cronet <[email protected]> wrote:
>
> Hey,
>
> i try to do my first steps with ajax (jquery) - In a list, i would
> like to have a switch which changes the 'active' paramter in the db
> for this entry.
>
> As i understand this topic, I need to do an ajax call to a controller
> method, which do the changes, and the realod the div with the new
> entries...
>
>
>
> so this is my controller method:
>
> function admin_switchStatus($id) {
> $this->News->id = $id;
> $news = $this->News->read('active', $id);
>
> switch($news['News']['active']) {
> case "1":
> $this->News->saveField('active', 0);
> break;
> case "0":
> $this->News->saveField('active', 1);
> break;
> }
>
> $this->redirect(array('controller'=>'news', 'action'=>'index',
> 'admin'=>true));
> }
>
>
>
>
> the jQuery for this in the admin index view is:
>
> $('.switchStatus').click(function(){
> $.get($(this).attr('href'), function(data){
> $('#mainContent').html(data);
> });
> return false;
> });
>
>
>
> Is this the correct (?) way to achieve this?
>
> Basically it works - but cake reloads not the admin_index but the
> index view...
I'm not sure why it's not rendering the admin view but you shouldn't
be redirecting. Here's a simpler version:
function admin_switchStatus($id)
{
/* this also sets the model ID
*/
$news = $this->News->read(null, $id);
/* toggle field
*/
$this->News->saveField('active', 1 - $news['News']['active']);
/* need to have RequestHandler in $components
*/
if ($this->RequestHandler->isAjax())
{
Configure::write('debug', 0);
$this->layout = 'ajax';
}
$this->render('admin_index');
}
$('.switchStatus').click(function()
{
$.ajax({
url: $(this).attr('href'),
type: 'GET',
success:
function(data)
{
$('#mainContent').html(data);
},
error:
function (XMLHttpRequest, textStatus, errorThrown)
{
alert(textStatus);
}
});
return false;
});
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---