Thursday, July 8, 2010

Re: Tree Reorder: What Does it Do?

On Thu, Jul 8, 2010 at 12:49 PM, DragonFlyEye <dragonflyeye74@gmail.com> wrote:
> Well in my case, I'm trying to maintain a structure of a website by
> using the Tree structure. We might want to reorder the list of items
> in the tree for one reason or another - such as if we want to display
> a left hand navigation and want to display Product C above Product A.


There are two approaches that I've taken. Dardo Sordi's
SortableBehavior [1] works very well for one-dimensional lists. One
limitation is that it'll only move an item up or down by one step. (I
haven't gotten around to extend it to move things an arbitrary number
of steps. It utilises a sort_order field in the table. Here's a
snippet showing how I've implemented it:

foreach($this->data['Image'] as $image)
{
...

<div>
<?php
/* include up button if not first in list
*/
if ($image['sort_order'] != 1)
{
echo $form->create('Image', array('admin' => 1, 'action' => 'moveUp'));
echo $form->hidden('Image.id', array('value' => $image['id']));
echo $form->hidden('Image.sort_order', array('value' =>
$image['sort_order']));
echo $form->end(array('label' => 'move up', 'title' => 'move image
up within gallery'));
}
?>
</div>
<div>
<?php
/* include down button if not last in list
*/
if ($image['sort_order'] != sizeof($this->data['Image']))
{
echo $form->create('Image', array('action' => 'moveDown', 'admin' => 1));
echo $form->hidden('Image.id', array('value' => $image['id']));
echo $form->hidden('Image.sort_order', array('value' =>
$image['sort_order']));
echo $form->end(array('label' => 'move down', 'title' => 'move image
down within gallery'));
}
?>
</div>

ImagesController:

function _move($id, $method)
{
if (!$id)
{
$this->flash(
'Invalid Category',
array('action' => 'index')
);
}

if ($this->Image->{$method}($id))
{
$this->flash('position updated');
}
else
{
$this->flash('position not updated');
}

$this->redirect($this->referer());
}

function admin_moveUp()
{
$this->_move($this->data['Image']['id'], 'moveUp');
}

function admin_moveDown()
{
$this->_move($this->data['Image']['id'], 'moveDown');
}


Another site I have has a model, Section, that follows an MPTT
structure. The sections can be re-ordered using the jQuery UI
drag&drop and Sortable extensions [2] which then sends an AJAX
request, passing the direction (up or down), the Section.id, and the
number of positions to move. Child Sections are kept contained within
the parent (meaning that they can only be sorted within the parent
list).

It also uses Andy Dawson's excellent TreeHelper [3] to generate the
list of sections. It's a bit confusing to set up so send me a mail if
you'd like the full code I used for that (plus the drag&drop stuff).


public function admin_move($direction, $id, $delta = null)
{
$this->autoRender = false;

$delta = abs($delta);

if ($delta > 0)
{
$this->Section->id = intval($id);
$action = "move${direction}";

$result = $this->Section->{$action}($id, $delta);

if ($result)
{
$this->__removeGroupSections();

echo 'section moved';
return;
}
else
{
echo 'Move failed ' . $id;
return;
}
}
else
{
echo "Provide a number of positions the section should be moved
${direction}.";
return;
}
}

[1] http://bakery.cakephp.org/articles/view/sortablebehavior-sort-your-models-arbitrarily
[2] http://jqueryui.com/
[3] http://bakery.cakephp.org/articles/view/tree-helper-1

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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 For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: