How can show tree list (recursive) in joomla for custome component?

1. First in Your component module class with function

function getTotal()
{
if (empty($this->_total))
{
$query = $this->_buildQuery();
$this->_total = $this->_getListCount($query);
}
return $this->_total;
}

replace with function
function getData()
{
if (empty($this->_data))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList($query);
$children = array ();
// first pass – collect children
foreach ( $this->_data as $v ) {
$pt = $v->parent_id;
$list = @$children [$pt] ? $children [$pt] : array ();
array_push ( $list, $v );
$children [$pt] = $list;
}
// second pass – get an indent list of the items
$list = JHTML::_ ( ‘menu.treerecurse’, 0, ”, array (), $children, 9999, 0, 0 );
$this->_data = array_slice ( $list, $this->getState(‘limitstart’), $this->getState(‘limit’));
}
return $this->_data;
}

2. In View (folder) Your default view listing
for ($i=0, $n=count( $this->items ); $i items[$i];
//category name print like
treename ; ?>
}

3. In Your Form view

add code for recursive function like for example

$value)
{
$stateslist1[] = JHTML::_(‘select.option’, $key, $value, ‘id’, ‘title’ );
}
$stateslist2 = array_merge( $stateslist,$stateslist1 );
$lists[‘id’] = JHTML::_(‘select.genericlist’, $stateslist2, ‘parent_id’, ‘class=”inputbox” size=”1″ style=”width:200px;”‘,’id’, ‘title’, $this->category->parent_id );

echo $lists[‘id’];

function getTree($id=0)
{
static $cates = array();
static $times = 0;
$times++;
$db =& JFactory::getDBO();
$query = ‘SELECT * FROM #__dir_categories where parent_id=’.$id;
$db->setQuery( $query );
$rowdata = $db->loadObjectList();
foreach($rowdata as $val)
{
$cates[$val->id] = str_repeat(“… “,$times-1).$val->title;
getTree($val->id);
}
$times=$times-1;
return $cates;
}
?>

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s