views:

1215

answers:

4

Hi,

I used to use scriptaculous in all my cakephp projects because of its easy helpers. In time, i got more and more into jquery and now I want to replace my current scriptaculous script with jquery. Until now > everything is good. Except jquery sortable.

Jquery sortable runs, but the ajax call after isn't working right. Now my programmer is on holiday so I've gotta ask you guys:

Old cakephp code (inside pages_controller.php):

function order($parent_id = 0){
 $this->autoRender=false;

 //Users with rights may view this
 $user = $this->checkRights('pages',true);

 //loop through the data sent via the ajax call
 foreach ($this->params['form']['page'] as $order => $id){
  $this->Page->id = $id;
  if(!$this->Page->saveField('order',$order)) {
   $this->flash('Really freaky errors are occuring','/');
   exit();
  }
 }

}

My jquery looks like:

    $(".sortable-list").sortable({
    update: function() {
        $.post('/pages/order/0', {
            data: $('.sortable-list').sortable("serialize")
        });
    }
});
$(".sortable-list").disableSelection();

With firebug, I see that the ajax post call produces something like this:

    page[]=14&page[]=23&page[]=18&page[]=11&page[]=26&page[]=28

However, it doesn't seem to work. I guess the page[]=id is different that the old scriptaculous format:

pages_0[] 1
pages_0[] 3
pages_0[] 2

However, does anyone know how i can adjust the cakephp file to read the string correctly?

Thx alot!

A: 

I don't have working php environment to test, but should work basically.

$pages = $_GET['page'];

foreach( $pages as $order => $id)
{
     $this->Page->id = $id;
            if(!$this->Page->saveField('order',$order)) {
                        $this->flash('Really freaky errors are occuring','/');
                        exit();
                }

}

PS. Probably you have kind of problem updating "$this->params".

Artem Barger
A: 

before foreach line, insert

debug($this->params['form']['page']);

and see how page array looks like. then iterate properly.

Sergei
i get:Notice (8): Undefined index: page [APP/controllers/pages_controller.php, line 81]andbouw2/controllers /pages_controller.php (line 81) Warning (2): Invalid argument supplied for foreach() [APP/controllers/pages_controller.php, line 82]what do you think? I'm not sure what it means. line 81 foreach ($this->params['form']['page'] as $order => $id){thx!
Maurice Kroon
than means there is no 'page' element in 'form' array.
Sergei
A: 

Ah, finally.. It turned out that jquery's output was: data: '&page_0[]=1etc'. I had to make it page_0 instead of data and its fixed!

So:

    $(".sortable-list").sortable({
    update: function() {
        $.post('/pages/order/0/, $('#pages_0').sortable("serialize", {key: 'pages_0[]'}))
    }
});
$(".sortable-list").disableSelection();

I removed the {} from the second argument of $.post and it turned out to be the winner! thx for the help guys!

Maurice Kroon
A: 

i am trying the same thing with cake and jquery but cant seem to get it. Would it be possible for you to show me the view.ctp snip you used and the jquery snip. I copied the one above but it returns errors for me.

Thanks

Dave [email protected]