1
votes

I am trying to populate a select box from a Category table with Tree Behavior.

$categories = $this->Category->generateTreeList(null,null,null,'   ');

This lists all the categories. As the tree is upto 5 level deep, the select box gets populated massively.

Is there a way to limit the number of levels to generate so that I don't need to display all tree data ? I would like to display upto 3rd level only.

I am using CakePHP 2.3.5.

2

2 Answers

1
votes
$this->Category->generateTreeList(null, null, null,'   ', 3);

According to the docs for generateTreeList() there is a fith parameter for levels deep.

generateTreeList($conditions=null, $keyPath=null, $valuePath=null, $spacer= '_', $recursive=null)

Parameters:
  $conditions – Uses the same conditional options as find().
  $keyPath – Path to the field to use for the key.
  $valuePath – Path to the field to use for the label.
  $spacer – The string to use in front of each item to indicate depth.
  $recursive – The number of levels deep to fetch associated records

0
votes

You can use this behavior which stores the depth/level of each node when it's created/updated. Then when fetching records using generateTreeList() you can just specify the depth in conditions.