Thursday, March 17, 2011

Re: Saving Drag/Drop Sortable to DB

I think you have to reset the active record in your for each loop, try
this :

foreach ($ids as $id) {
$this->Item->id = $id;
$this->Item->saveField('sort_order', $i++);
$this->Item->create();
}

On 17/03/2011 12:48, Ryan wrote:
> I'm trying to display a list of items from the database and let the
> user drag them around to sort them however they like. Each time they
> drag something, a function should fire that loops through all of the
> rows and saves the sort order to the database so that the next time
> the user loads that page, everything is sorted like it was when they
> left. I don't know much at all about AJAX/javascript so have just been
> looking for tutorials/code samples to do this....I found something
> that uses CakePHP's built in AJAX sortable. I've got the drag& drop
> working, but it's not firing the function to go to the database.
>
> Please help!!
>
>
> VIEW:
>
> <div id="items">
> <?php
> foreach ($items as $item): ?>
>
> <div id="item_<?php echo $item['Item']['id']; ?>"
> class="item">
> <?php echo $item['Item']['name'];?>
> </div>
>
> <?php endforeach; ?>
> </div>
>
>
> <?php echo $ajax->sortable('items', array(
> 'tag' => 'div',
> 'only' => 'item',
> 'hoverclass' => 'selected',
> 'url' => array('controller' => 'items', 'action' => 'order')
> ));
> ?>
>
>
>
> CONTROLLER:
>
> function order() {
> if ($this->RequestHandler->isAjax()) {
> $ids= $this->params['form']['items'];
> $i = 1;
> foreach ($ids as $id) {
> $this->Item->id = $id;
> $this->Item->saveField('sort_order', $i++);
> }
> $this->autoRender = false;
> }
> }
>
>
>
>
>
> I'm importing the 'Javascript', 'Ajax' Helpers in app_controller.php.
> I also tried 'onUpdate' => array('controller' => 'items', 'action' =>
> 'order') instead of 'url' => array('controller' => 'items', 'action'
> => 'order')....but that doesn't work either.
>
> Any help would be VERY much appreciated. Thank you!!
>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate