昨天想了一下没啥思路。今天早上又想了一下发现找到思路了。大概可以这样理解:
从根往上找的话可以定位应用的ID。还有其父节点的ID。可以放到一个全局变量里面比如[]
然后在遍历生成树的时候判断一下当前应用的ID是否在这个[]里面如果在就可以显示出来否则就不显示 出来即可。
代码如下:
- # -*- coding: utf-8 -*-
-
"""
-
表结构:
-
CREATE TABLE `javahost_apptree` (
-
`id` int(4) NOT NULL auto_increment,
-
`appID` varchar(32) default NULL,
-
`appName` varchar(128) default NULL,
-
`parentID` varchar(32) default NULL,
-
`isLeaf` smallint(6) default NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-
"""
-
import sqlite3,json,MySQLdb,sys
-
conn=sqlite3.connect('data.db')
-
#conn = MySQLdb.connect('localhost','root','321','test',charset='utf8')
-
cu = conn.cursor()
-
parentidlist = []
-
-
"""
-
递归调用获取某个节点其父节点顺序的父节点ID到全局的LIST里面
-
"""
-
def getparentappid(appid):
-
sql = "select parentID from javahost_apptree where appid='%s'" % str(appid)
-
cu.execute(sql)
-
rs = cu.fetchone()
-
if int(rs[0]) > 0:
-
parentidlist.append(int(rs[0]))
-
getparentappid(rs[0])
-
-
"""
-
依据应用名称提取当前全部应用的一级父节点的ID列表.分别进行遍历往上提取这个树的全部父节点列表
-
@param appname:应用名称
-
"""
-
def appsearch(appname):
-
"""当前节点本身的ID"""
-
sql = "select appID from javahost_apptree where appName like '%"+appname+"%' and isLeaf=1"
-
cu.execute(sql)
-
rs = cu.fetchall()
-
for row in rs:
-
eachparentid = row[0]
-
parentidlist.append(int(eachparentid))
-
getparentappid(eachparentid)
-
appidlist = {}.fromkeys(parentidlist).keys()
-
return appidlist
-
-
"""
-
判断当前应用节点下面是否还有子节点
-
@return: true表示还有
-
"""
-
def hasChild(appId):
-
sql = "select count(*) from javahost_apptree where parentID=%d"%int(appId)
-
cu.execute(sql)
-
rs = cu.fetchone()
-
return int(rs[0]) > 0
-
"""判断当前这个应用ID是不是叶节点"""
-
def isleaf(appid):
-
sql = "select count(*) from javahost_apptree where appID=%d and isleaf=1"%int(appid)
-
cu.execute(sql)
-
rs = cu.fetchone()
-
return int(rs[0]) > 0
-
"""提取当前指定应用下面的全部的子节点。返回指定节点其下面的子节点列表
-
[
-
{
-
id:1,
-
text:33,
-
value:01
-
complete:true,
-
hasChileren:true,
-
}
-
]
-
"""
-
def toJsonString(appId,mylist=[]):
-
resl= []
-
sql = "select appID,appName,parentID,isLeaf from javahost_apptree where parentID=%d" % int(appId)
-
cu.execute(sql)
-
rs = cu.fetchall()
-
for row in rs:
-
if int(row[0]) in mylist:
-
result = {}
-
result['complete']=True
-
if hasChild(row[0]):
-
result['hasChildren']=True
-
result['ChildNodes'] = toJsonString(row[0],mylist)
-
else:
-
result['hasChildren']=False
-
result['id']=row[0]
-
-
if int(row[0]) in mylist:
-
result['i***pand']=True
-
-
result['text']=row[1]
-
if isleaf(row[0]):
-
result['value']='0'+row[0]
-
else:
-
result['value']=row[0]
-
resl.append(result)
-
return resl
-
-
-
-
def generateInitTreeString(mylist=[]):
-
"""获取根节点数据不断迭代生成树"""
-
jsonString = []
-
cu.execute('select appID,appName,parentID,isLeaf from javahost_apptree where parentID=0 and id!=1046')
-
rs = cu.fetchall()
-
for row in rs:
-
if int(row[0]) in mylist:
-
result = {}
-
result['complete']=True
-
if hasChild(row[0]):
-
result['hasChildren']=True
-
result['ChildNodes'] = toJsonString(row[0],mylist)
-
else:
-
result['hasChildren']=False
-
result['ChildNodes'] =None
-
result['i***pand']=True
-
result['id']=row[0]
-
result['text']=row[1]
-
if isleaf(row[0]):
-
result['value']='0'+row[0]
-
else:
-
result['value']=row[0]
-
jsonString.append(result)
-
return json.dumps(jsonString,ensure_ascii=True)
-
-
if __name__ == '__main__':
-
result = "var treedata = "
-
appidlist = appsearch('ISV')
-
result = result + generateInitTreeString(appidlist)
-
file_object = open('tree.js', 'w')
-
file_object.write(result)
-
file_object.close()
爱生活爱技术更爱python!!!
阅读(4325) | 评论(0) | 转发(0) |