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

全部博文(264)

文章存档

2011年(264)

分类: Python/Ruby

2011-05-30 12:06:26

  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. cu = conn.cursor()
  16. """
  17. 判断当前应用节点下面是否还有子节点
  18. @return: true表示还有
  19. """
  20. def hasChild(appId):
  21.     sql = "select count(*) from javahost_apptree where parentID=%d"%int(appId)
  22.     cu.execute(sql)
  23.     rs = cu.fetchone()
  24.     return int(rs[0]) > 0
  25. """判断当前这个应用ID是不是叶节点"""
  26. def isleaf(appid):
  27.     sql = "select count(*) from javahost_apptree where appID=%d and isleaf=1"%int(appid)
  28.     cu.execute(sql)
  29.     rs = cu.fetchone()
  30.     return int(rs[0]) > 0
  31. """提取当前指定应用下面的全部的子节点。返回指定节点其下面的子节点列表
  32. [
  33.     {
  34.         id:1,
  35.         text:33,
  36.         value:01
  37.         complete:true,
  38.         hasChileren:true,
  39.     }
  40. ]
  41. """
  42. def toJsonString(appId):
  43.     resl= []
  44.     sql = "select appID,appName,parentID,isLeaf from javahost_apptree where parentID=%d" % int(appId)
  45.     cu.execute(sql)
  46.     rs = cu.fetchall()
  47.     for row in rs:
  48.         result = {}
  49.         result['complete']=True
  50.         if hasChild(row[0]):
  51.             result['hasChildren']=True
  52.             result['ChildNodes'] = toJsonString(row[0])
  53.         else:
  54.             result['hasChildren']=False
  55.         result['id']=row[0]
  56.         result['text']=row[1]
  57.         if isleaf(row[0]):
  58.             result['value']='0'+row[0]
  59.         else:
  60.             result['value']=row[0]
  61.         resl.append(result)
  62.     return resl

  63. def generateInitTreeString():
  64.     """获取根节点数据不断迭代生成树"""
  65.     jsonString = []
  66.     cu.execute('select appID,appName,parentID,isLeaf from javahost_apptree where parentID=0 and id!=1046')
  67.     rs = cu.fetchall()
  68.     for row in rs:
  69.         result = {}
  70.         result['complete']=True
  71.         if hasChild(row[0]):
  72.             result['hasChildren']=True
  73.             result['ChildNodes'] = toJsonString(row[0])
  74.         else:
  75.             result['hasChildren']=False
  76.             result['ChildNodes'] =None
  77.         
  78.         result['id']=row[0]
  79.         result['text']=row[1]
  80.         if isleaf(row[0]):
  81.             result['value']='0'+row[0]
  82.         else:
  83.             result['value']=row[0]
  84.         jsonString.append(result)
  85.     return json.dumps(jsonString,ensure_ascii=True)
  86. if __name__ == '__main__':
  87.     result = "var treedata = "
  88.     result = result + generateInitTreeString()
  89.     file_object = open('tree.js', 'w')
  90.     file_object.write(result)
  91.     file_object.close()


前端JS的代码
    Big Tree
    
    
  
    
    
        
            
        
        
    
 



阅读(12808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~