hiryu85
6/19/2017 - 1:38 PM

TreeBehavior spacer char callback: i want add character only to end (no repeat)

TreeBehavior spacer char callback: i want add character only to end (no repeat)

// ...


    public function formatTreeList(Query $query, array $options = [])
    {
        return $query->formatResults(function ($results) use ($options) {
            /* @var \Cake\Collection\CollectionTrait $results */
            $options += [
                'keyPath' => $this->_getPrimaryKey(),
                'valuePath' => $this->_table->getDisplayField(),
                'spacer' => '_',
            ];

            return $results
                ->listNested()
                ->printer($options['valuePath'], $options['keyPath'], $options['spacer']);
        });
    }
public function add() { 
  // ....
  $parentCategories = $this->StoreProductCategories->ParentStoreCategories->find('treeList', [

      // Current output:
      //
      // Root Category
      // ↳ Shoes (level 1)
      // ↳↳ Nike (level 2)
      //      
      'spacer' => '',



      // Expected output:
      // (s) = space character 
      //
      // Root Category
      // (s)↳ Shoes
      // (s)(s)↳ Nike
      //
      'spacer' => function($item) {
          $spaceChar = ' ';
          $level = $item->level - 1;
          return str_repeat($spaceChar, $level) . '↳';
      }
  ]);
}