python遞歸實現Easyui combotree樹

原文連接:http://www.huangdc.com/43 javascript

自動化發佈系統在選擇文件時,使用jQuery EasyUI 建立頁面 樹形菜單(Tree) 及 後端python 遞歸實現Easyui combotree 樹。這裏主要分享2點: css

    一、linux 中 python 遞歸實現Easyui combotree 樹 html

    二、jQuery EasyUI就不用介紹了,一款輕量級UI框架,集成了各類用戶界面插件。 前端

關於jQuery EasyUI 能夠參考: http://www.w3cschool.cc/jeasyui/jqueryeasyui-tutorial.html java


關於自動化發佈系統,能夠查看:http://my.oschina.net/u/588586/blog/345670 python

本文環境基礎:http://my.oschina.net/u/588586/blog/345675 jquery

目的:用戶在頁面選擇對應項目,前端展現對應項目svn目錄列表 linux


一、廢話很少說,先上圖看看UI

選擇項目時,展現項目 目錄的 tree ajax


二、jQuery EasyUI tree  代碼 html 

    ##須要調用easyui.css 和 jquery.easyui.min.js npm

    index.html 

<!DOCTYPE html>
<!-- saved from url=(0050)http://getbootstrap.com/examples/starter-template/ -->
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="http://getbootstrap.com/favicon.ico">

    <title>Python EasyUI tree @DcHuang</title>
    <!-- 須要加上easyui.css 和 bootstrap.min.css -->
    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
    <!-- Bootstrap core CSS -->
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    </head>

    <body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div>
        <div>
          <a href="http://getbootstrap.com/examples/starter-template/#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a href="http://getbootstrap.com/examples/starter-template/#">Home</a></li>
            <li><a href="http://getbootstrap.com/examples/starter-template/#about">About</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
      <div class="starter-template">
        <p>@DcHuang</p>
        </br>
        </br>
      </div>
      <!--重點關注一下-->
      <div class="well">
        <form class="form-horizontal" role="form" action="/deployserver/deploy/" method="post" id="Form-DeployProject">
                                <div class="form-group">
                                        <label class="col-sm-2 control-label">標題<span style="color:red">*</span></label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" name="DeployTitle" id="DeployTitle">
                                        </div>
                                </div>


                                <div class="form-group">
                                        <label class="col-sm-2 control-label">發佈項目<span style="color:red">*</span></label>
                                                <div class="col-sm-4">
                                                        <select class="form-control" name="DeployProject" id="DeployProject" >
                                                                <option value="0" selected >--項目0--</option>
                                                                <option value="1" selected >--項目1--</option>
                                                        </select>
                                                </div>

                                                <div class="col-sm-4">
                                                        <select class="form-control" name="DeployTransitip" id="DeployTransitip">
                                                                <option value="0" selected >--可選中轉IP--</option>
                                                        </select>
                                                                                                        </div>

                                                <div class="col-sm-4 col-sm-offset-2" style="margin-top:10px">
                                                        <div style="border: 1px solid #ccc;border-radius: 6px 6px 6px 6px;">
                                                                <div class="control-group" style="height:25px;text-align:center;">
                                                                --選擇目標IP--
                                                                </div>
                                                                <div class="control-group" style="margin-bottom:6px;margin-left:20px;width:100%" id="div_server">
                                                                </div>
                                                        </div>
                                                </div>

                                                <div class="col-sm-4" style="margin-top:10px">
                                                        <div style="border: 1px solid #ccc;border-radius: 6px 6px 6px 6px;">
                                                                <div class="control-group" style="height:25px;text-align:center;">
                                                                --中轉IP--
                                                                </div>
                                                                <div class="control-group" style="margin-bottom:6px;margin-left:20px;width:100%" id="div_transitip">
                                                                </div>
                                                        </div>
                                                </div>
                                </div>

                                <!-- 重點:easyui-tree -->
                                <div class="form-group">
                                        <label class="col-sm-2 control-label">更新文件<span style="color:red">*</span></label>
                                        <div class="col-sm-9">
                                                <button type="button" class="btn btn-primary btn-xs" name="reloadAll" onclick="reloadall();">刷新列表</button>
                                                <button type="button" class="btn btn-primary btn-xs" name="expandAll" onclick="expandall();">展開</button>
                                                <button type="button" class="btn btn-primary btn-xs" name="collapseAll" onclick="collapseall();">收縮</button>
                                                <input type="button"  class="btn btn-success btn-xs" id="svnversion" value="SVN版本" disabled="disabled"/>

                                        </div>
                                        <div class="col-sm-9 col-sm-offset-2" style="margin-top:10px">
                                                <div class="easyui-panel" style="padding:5px;" id="div_filelist">
                                                        <ul id="file_list" class="easyui-tree"></ul>
                                                </div>
                                        </div>
                                </div>

                                <div class="form-group">
                                        <label class="col-sm-2 control-label">發佈說明</label>
                                        <div class="col-sm-8">
                                            <input type="text" class="form-control" name="DeployNotes" id="DeployNotes">
                                        </div>
                                </div>

                                <div class="form-group">
                                        <div class="col-sm-offset-2 col-sm-10">
                                                <button type="button" class="btn btn-default" name="b_deploy_reset" onclick="form_deploy_reset();">重置</button>
                                                <button type="button" class="btn btn-primary" name="b_deploy_upload" onclick="form_deploy();">提交</button>
                                        </div>
                                </div>
        </form>
    </div>

    </div><!-- /.container -->
        <!-- jquery JavaScript -->
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
    <script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
    <!-- Bootstrap core JavaScript -->
    <script src="/static/js/bootstrap.min.js"></script>
    </body>
    </html>


三、js代碼 

能夠放在index.html文件後面,根據所選擇項目id,後端生成tree文件

    <script>
        $("#DeployProject").change(function(){
                var project_id=$("#DeployProject").val();
                //後端刷新tree json文件
                $.ajax({
                    type:"POST",
                    url:"/",
                    data:{'type':"select_project",'project_id':project_id},
                    success:function(response){
                        //alert(response);
                        // tree 
                        $("#file_list").tree({
                                // 記得加上new Date() ,緣由是cache問題 ;有興趣你試試不加,更改目錄文件看看
                                url:'/static/data/tree_data_'+project_id+'.json?tm=' + new Date(),
                                method:'get',
                                animate:true,
                                checkbox:true
                        });
                        $('#file_list').tree('reload');
                    },
                    error:function(xhr,textStatus){
                        alert(textStatus);
                    }
                });
        });
    </script>


四、python 遞歸實現Easyui combotree 樹,後端pyhton代碼

    views.py 文件 

from django.shortcuts import render,render_to_response
from django.http import HttpResponse,HttpResponseRedirect
import time,json,os,commands,urllib,sys,subprocess,datetime,signal
#coding=utf-8
#import time,simplejson,json,os,commands
##
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
        reload(sys)
        sys.setdefaultencoding(default_encoding)

# Create your views here.
def index(req):
        ##
        if req.method == 'POST':
                if req.POST.has_key('type') and req.POST['type']=="select_project":
                        project_id=req.POST['project_id']
                        ##調用CreateDirTree函數
                        CreateDirTree('/data/myproject/static/','/data/myproject/static/data/tree_data_'+str(project_id)+'.json')
                        return HttpResponse("ok")
        #return HttpResponse("aa")
        return render_to_response('index.html',)

## 建立文件樹SVN
## python遞歸實現Easyui combotree樹
def CreateDirTree(path,file_json):
        global id_num
        id_num=0
        def createDict(path):
                global id_num
                tree_list=[]
                pathList = os.listdir(path)
                if '.svn' in pathList:
                        pathList.remove('.svn')
                for i,item in enumerate(pathList):
                        id_num+=1
                        children_map={}
                        children_map['id']=id_num
                        children_map['text']=item
                        #children_map['checked']='true'
                        children_map['attributes']={'url':path}
                        if isDir(getJoinPath(path,item)):
                                path = getJoinPath(path,item)
                                tmp_listdir=os.listdir(path)
                                if '.svn' in tmp_listdir:
                                        tmp_listdir.remove('.svn')
                                if  len(tmp_listdir) != 0:
                                        children_map['state']='closed'
                                        """開始遞歸子節點"""
                                        children_map['children'] = createDict(path)
                                tree_list.append(children_map)
                                path = '/'.join(path.split('/')[:-1])
                        else:
                                tree_list.append(children_map)
                return tree_list

        '''合併路徑和目錄,返回完整路徑'''
        def getJoinPath(path,item):
                return os.path.join(path,item)

        '''判斷是否爲目錄'''
        def isDir(path):
                if os.path.isdir(path):
                        return True
                return False

        '''返回json格式數據'''
        def getJson(treestr):
                return json.dumps(treestr)

        '''json 文件'''
        tree_list=createDict(path)
        fjson = getJson(tree_list)
        with open(file_json,'w') as lf:
                lf.write(fjson)
        ##end

生成的文件格式以下:

[{"text": "css", "state": "closed", "children": [{"text": "bootstrap.css", "id": 2, "attributes": {"url": "/data/myproject/static/css"}}, {"text": "bootstrap-theme.min.css", "id": 3, "attributes": {"url": "/data/myproject/static/css"}}, {"text": "bootstrap.min.css", "id": 4, "attributes": {"url": "/data/myproject/static/css"}}, {"text": "bootstrap.css.map", "id": 5, "attributes": {"url": "/data/myproject/static/css"}}, {"text": "bootstrap-theme.css", "id": 6, "attributes": {"url": "/data/myproject/static/css"}}, {"text": "bootstrap-theme.css.map", "id": 7, "attributes": {"url": "/data/myproject/static/css"}}], "id": 1, "attributes": {"url": "/data/myproject/static/"}}, {"text": "js", "state": "closed", "children": [{"text": "bootstrap.js", "id": 9, "attributes": {"url": "/data/myproject/static/js"}}, {"text": "bootstrap.min.js", "id": 10, "attributes": {"url": "/data/myproject/static/js"}}, {"text": "npm.js", "id": 11, "attributes": {"url": "/data/myproject/static/js"}}, {"text": "jquery-1.11.1.min.js", "id": 12, "attributes": {"url": "/data/myproject/static/js"}}], "id": 8, "attributes": {"url": "/data/myproject/static"}}, {"text": "data", "state": "closed", "children": [{"text": "tree_data_0.json", "id": 14, "attributes": {"url": "/data/myproject/static/data"}}, {"text": "tree_data_1.json", "id": 15, "attributes": {"url": "/data/myproject/static/data"}}], "id": 13, "attributes": {"url": "/data/myproject/static"}}, {"text": "fonts", "state": "closed", "children": [{"text": "glyphicons-halflings-regular.ttf", "id": 17, "attributes": {"url": "/data/myproject/static/fonts"}}, {"text": "glyphicons-halflings-regular.svg", "id": 18, "attributes": {"url": "/data/myproject/static/fonts"}}, {"text": "glyphicons-halflings-regular.woff", "id": 19, "attributes": {"url": "/data/myproject/static/fonts"}}, {"text": "glyphicons-halflings-regular.eot", "id": 20, "attributes": {"url": "/data/myproject/static/fonts"}}], "id": 16, "attributes": {"url": "/data/myproject/static"}}]


原文連接:http://www.huangdc.com/43

相關文章
相關標籤/搜索