Yii Framework Forum: how to implement treeview, data coming from db - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to implement treeview, data coming from db Rate Topic: -----

#1 User is offline   che 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 12-October 11

Posted 19 October 2011 - 10:08 PM

I have this table:

CREATE TABLE ACCOUNT_TYPE
( "ACCOUNT_TYPE_UID" NUMBER PRIMARY KEY,
"ACCOUNT_TYPE_PARENT_UID" NUMBER,
"DESCRIPTION" VARCHAR2(100 BYTE),
)

ALTER TABLE ACCOUNT_TYPE
ADD CONSTRAINT ACCOUNT_TYPE FOREIGN KEY ("ACCOUNT_TYPE_PARENT_UID")
REFERENCES ACCOUNT_TYPE ("ACCOUNT_TYPE_UID") ENABLE


And it has this data:

ACCOUNT_TYPE_UID ACCOUNT_TYPE_PARENT_UID DESCRIPTION
--------------- ----------------------- ------------
1 NULL ASSETS
2 NULL LIABILITIES
3 NULL EQUITY
4 NULL INCOME
5 NULL EXPENSES
6 1 Current Assets
7 6 Cash
....


in my view/index.php

<?php
$this->widget('CTreeView',
array('url'=>array('accountType/ajaxFillTree'))
);
?>


while in my AccountTypeController


public function actionAjaxFillTree()
{
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
//$parentId = "NULL";
if (isset($_GET['root']) && $_GET['root'] !== 'source') {
$parentId = (int) $_GET['root'];
}
$sql = "SELECT m1.ACCOUNT_TYPE_UID, m1.description, "
. " DECODE(m1.account_type_parent_uid, NULL, 1, 0 ) hasChildren "
. " FROM ACCOUNT_TYPE_101 m1 "
. " LEFT OUTER JOIN ACCOUNT_TYPE_101 m2 "
. " ON m1.account_type_parent_uid = m2.account_type_uid "
. "WHERE m1.account_type_parent_uid <=> $parentId "
. " ORDER BY m1.sequence; " ;

$req = Yii::app()->db->createCommand($sql);

$children = $req->queryAll();

echo str_replace(
'"hasChildren":"0"',
'"hasChildren":false',
CTreeView::saveDataAsJson($children)
);
exit();
}


but no treeview is being render in the output?

what is wrong/missing in my code?

much need your help guys...

thanks in advance
0

#2 User is offline   Kabinenkoffer 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 04-November 10
  • Location:Karlsruhe

Posted 02 November 2011 - 04:38 AM

Hi,

maybe this extension can help you, for me it worked fine:

http://www.yiiframew...ion/simpletree/
0

#3 User is offline   Agrippa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 28-October 11

Posted 15 November 2011 - 06:03 PM

View PostKabinenkoffer, on 02 November 2011 - 04:38 AM, said:

Hi,

maybe this extension can help you, for me it worked fine:

http://www.yiiframew...ion/simpletree/


Hi Kabinenkoffer,

I try to run this extension you mentioned. Unfortunately without success, just the same issue as in the comments: No interactivity with the DB. Could you please be so kind and give me a hint how you were able to run this thing?

My settings so far:

in the Site Controller:

Quote

public function actionTree() { Yii::import('application.extensions.SimpleTreeWidget');
SimpleTreeWidget::performAjax(); }


in the view/site/index:

Quote

<?php $this->widget('application.extensions.SimpleTreeWidget',array(
'model'=>Category::model()->findByPk(43),
'modelPropertyParentId' => 'parent_id',
'modelPropertyName' => 'name',
'ajaxUrl' => '/ajax/simpletree', / also tried: 'ajaxUrl' => $this->createAbsoluteUrl('/site/simpletree'),
'onSelect'=>'
var id = data.inst.get_selected().attr("id").replace("node_","");
$("#contentBox").load("/ajax/getContent/id/"+id);
'
)); ?>


The Table:

Quote

CREATE TABLE IF NOT EXISTS category ( id int(11) NOT NULL AUTO_INCREMENT, id_parent int(11) unsigned NOT NULL, position mediumint(5) unsigned NOT NULL, title varchar(255) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;


Is it necessary to fill the table with values?

Beste Grüße

Norbert
0

#4 User is offline   François Gannaz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 24-November 09

Posted 15 November 2011 - 07:37 PM

A question beginning with "how to" is surely not a feature request. Wrong forum.
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,385
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 November 2011 - 05:13 AM

NOTE: moved to proper section (General Discussion for Yii 1.1.x instead of Feature request)
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   jover 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 21-November 11

Posted 23 November 2011 - 09:18 PM

The ORACLE query return ALL CAPS array keys but the CTreeView::saveDataAsJson($data) needs a valid array data which has the keys: id, text, and hasChildren.
Check CTreeView valid DATA.

So I use this line of code to rename the array keys:

Quote

$data = json_decode(str_replace('ID', 'id', json_encode($children)), true);


Then I changed the query into this:

Quote

$req = Yii::app()->db->createCommand(
"SELECT m1.tree_id AS id, m1.tree_name AS TEXT, DECODE(m2.tree_id, NULL, 0, 1) AS hasChildren "
. "FROM tree m1 " . "LEFT JOIN tree m2 ON m1.tree_id = m2.parent_id "
. "WHERE DECODE(m1.parent_id, NULL, 0, m1.parent_id) = $parentId "
. "GROUP BY m1.tree_id, m1.tree_name, DECODE(m2.tree_id, NULL, 0, 1) " . "ORDER BY m1.tree_name ASC "
);


I hope this will help.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users