Reputation: 23
I want to create a tree view which fetching data from Database and after that order it by parent field So table fields are included :
product_category_id
product_category_parent_id
product_category_name
the record that is been mentioned by name " product_category_parent_id" is 0 without any root and when it wish have any ID code / number , parent Id should come in this palce so structure of the table must be sent to the View :
<ul><li><ul><li></li></ul></li></ul>
Upvotes: 2
Views: 2157
Reputation: 8072
There is an example how to create CTreeView
private function generateTree($models)
{
$data = array();
foreach ($models as $category) {
$data[$category->id] = array(
'id' => $category->id,
'text' => '<a href="/admin/catalog/category/id/'.$category->id.'">'.$category->category_name.'</a>',
);
foreach ($category->goods as $item) {
$data[$category->id]['children'][$item->id] = array(
'id' => $item->id,
'text' => '<a href="/admin/catalog/item/id/'.$item->id.'">'.$item->article.'</a>',
'expanded' => false,
);
}
}
return $data;
}
In view
$this->widget('CTreeView', array('data' => $data,'persist'=>'cookie'));
Upvotes: 4