Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1288114
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: Python/Ruby

2011-05-31 08:13:22

昨天想了一下没啥思路。今天早上又想了一下发现找到思路了。大概可以这样理解:
从根往上找的话可以定位应用的ID。还有其父节点的ID。可以放到一个全局变量里面比如[]
然后在遍历生成树的时候判断一下当前应用的ID是否在这个[]里面如果在就可以显示出来否则就不显示 出来即可。
代码如下:
  1. # -*- coding: utf-8 -*-
  2. """
  3. 表结构:
  4. CREATE TABLE `javahost_apptree` (
  5.   `id` int(4) NOT NULL auto_increment,
  6.   `appID` varchar(32) default NULL,
  7.   `appName` varchar(128) default NULL,
  8.   `parentID` varchar(32) default NULL,
  9.   `isLeaf` smallint(6) default NULL,
  10.   PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
  12. """
  13. import sqlite3,json,MySQLdb,sys
  14. conn=sqlite3.connect('data.db')
  15. #conn = MySQLdb.connect('localhost','root','321','test',charset='utf8')
  16. cu = conn.cursor()
  17. parentidlist = []

  18. """
  19. 递归调用获取某个节点其父节点顺序的父节点ID到全局的LIST里面
  20. """
  21. def getparentappid(appid):
  22.     sql = "select parentID from javahost_apptree where appid='%s'" % str(appid)
  23.     cu.execute(sql)
  24.     rs = cu.fetchone()
  25.     if int(rs[0]) > 0:
  26.         parentidlist.append(int(rs[0]))
  27.         getparentappid(rs[0])

  28. """
  29. 依据应用名称提取当前全部应用的一级父节点的ID列表.分别进行遍历往上提取这个树的全部父节点列表
  30. @param appname:应用名称
  31. """
  32. def appsearch(appname):
  33.     """当前节点本身的ID"""
  34.     sql = "select appID from javahost_apptree where appName like '%"+appname+"%' and isLeaf=1"
  35.     cu.execute(sql)
  36.     rs = cu.fetchall()
  37.     for row in rs:
  38.         eachparentid = row[0]
  39.         parentidlist.append(int(eachparentid))
  40.         getparentappid(eachparentid)
  41.     appidlist = {}.fromkeys(parentidlist).keys()
  42.     return appidlist

  43. """
  44. 判断当前应用节点下面是否还有子节点
  45. @return: true表示还有
  46. """
  47. def hasChild(appId):
  48.     sql = "select count(*) from javahost_apptree where parentID=%d"%int(appId)
  49.     cu.execute(sql)
  50.     rs = cu.fetchone()
  51.     return int(rs[0]) > 0
  52. """判断当前这个应用ID是不是叶节点"""
  53. def isleaf(appid):
  54.     sql = "select count(*) from javahost_apptree where appID=%d and isleaf=1"%int(appid)
  55.     cu.execute(sql)
  56.     rs = cu.fetchone()
  57.     return int(rs[0]) > 0
  58. """提取当前指定应用下面的全部的子节点。返回指定节点其下面的子节点列表
  59. [
  60.     {
  61.         id:1,
  62.         text:33,
  63.         value:01
  64.         complete:true,
  65.         hasChileren:true,
  66.     }
  67. ]
  68. """
  69. def toJsonString(appId,mylist=[]):
  70.     resl= []
  71.     sql = "select appID,appName,parentID,isLeaf from javahost_apptree where parentID=%d" % int(appId)
  72.     cu.execute(sql)
  73.     rs = cu.fetchall()
  74.     for row in rs:
  75.         if int(row[0]) in mylist:
  76.             result = {}
  77.             result['complete']=True
  78.             if hasChild(row[0]):
  79.                 result['hasChildren']=True
  80.                 result['ChildNodes'] = toJsonString(row[0],mylist)
  81.             else:
  82.                 result['hasChildren']=False
  83.             result['id']=row[0]
  84.             
  85.             if int(row[0]) in mylist:
  86.                 result['i***pand']=True
  87.             
  88.             result['text']=row[1]
  89.             if isleaf(row[0]):
  90.                 result['value']='0'+row[0]
  91.             else:
  92.                 result['value']=row[0]
  93.             resl.append(result)
  94.     return resl



  95. def generateInitTreeString(mylist=[]):
  96.     """获取根节点数据不断迭代生成树"""
  97.     jsonString = []
  98.     cu.execute('select appID,appName,parentID,isLeaf from javahost_apptree where parentID=0 and id!=1046')
  99.     rs = cu.fetchall()
  100.     for row in rs:
  101.         if int(row[0]) in mylist:
  102.             result = {}
  103.             result['complete']=True
  104.             if hasChild(row[0]):
  105.                 result['hasChildren']=True
  106.                 result['ChildNodes'] = toJsonString(row[0],mylist)
  107.             else:
  108.                 result['hasChildren']=False
  109.                 result['ChildNodes'] =None
  110.             result['i***pand']=True
  111.             result['id']=row[0]
  112.             result['text']=row[1]
  113.             if isleaf(row[0]):
  114.                 result['value']='0'+row[0]
  115.             else:
  116.                 result['value']=row[0]
  117.             jsonString.append(result)
  118.     return json.dumps(jsonString,ensure_ascii=True)

  119. if __name__ == '__main__':
  120.     result = "var treedata = "
  121.     appidlist = appsearch('ISV')
  122.     result = result + generateInitTreeString(appidlist)
  123.     file_object = open('tree.js', 'w')
  124.     file_object.write(result)
  125.     file_object.close()
爱生活爱技术更爱python!!!
阅读(4102) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~