Friday, November 5, 2010

Re: Tree Behavior

On Thu, Nov 4, 2010 at 11:37 PM, Meroe <whmeroe@gmail.com> wrote:
> Hello folks,
>
> I'm working with the tree behavior and having issues getting the
> results to display more than the 'name' field in my table.  I have
> many additional fields that I've added and would like to display in
> the view such as 'start_date','details'.  If I print_r the $nodelist I
> see:
>
> Array ( [52] => Task #1 [54] => - Sub Task of Task #1 [53] => Task
> #2 )
>
> Here is my model:
> <?php
> class Projecttask extends AppModel {
>        var $name = 'Projecttask';
>        var $actsAs = array('Tree');
> }
> ?>
>
> Here is my Controller:
> function index() {
>         $nodelist = $this->Projecttask->generatetreelist(null,null,null," -
> ");
>     $this->set('nodelist',$nodelist);
>        }


You're using the wrong method. Look at the API:
http://api.cakephp.org/class/tree-behavior#method-TreeBehaviorgeneratetreelist

"A convenience method for returning a hierarchical array used for HTML
select boxes"

IOW, it returns data in the same format as find('list'), ie. $id =>
$name. You should use find('threaded', $filters), where $filters is an
array with the usual 'conditions', 'fields', etc. arrays. You can even
use 'contain' as you would normally. Each Projecttask (shouldn't that
be ProjectTask?) entry will have a 'children' key pointing to an array
of Projecttasks (if any).


> Here is my View:
> <?
> echo "<ul>";
> foreach($nodelist as $key=>$value){
>    $editurl = $html->link("Edit", array('action'=>'edit', $key));
>    $upurl = $html->link("Up", array('action'=>'moveup', $key));
>    $downurl = $html->link("Down", array('action'=>'movedown', $key));
>    $deleteurl = $html->link("Delete", array('action'=>'delete',
> $key));
>    echo "<li>[$editurl|$upurl|$downurl|$deleteurl] $value</li>";
> }
> echo "</ul>";
> ?>

You should take a look at Andy Dawson's TreeHelper, which takes the
pain out of creating nested lists from nested tree data. I found it a
bit confusing at first but it works great.
http://bakery.cakephp.org/articles/AD7six/2008/01/24/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: