1.python Flask程序
@app.route('/_init_treeviews')
def init_treeviews():
sqlhost = """select mo.host_name as id,mo.host_caption as text,io.img_name as img
from host_obj mo,img_obj io
where mo.img_id = io.img_id"""
rows = g.db.query(sqlhost).dictresult()
hrows = [dict(id=row['id'],text=unicode(row['text'],"UTF8"),img=row['img']) for row in rows]
return jsonify(mview = hrows)
@app.route('/sidebar')
def sidebar():
return render_template('sidebar.html')
2.web页面sidebar.html
<div id="sidebar" style="height: 500px; width: 200px;"></div>
<!-- Flask AJAX -->
<script type=text/javascript>
$SCRIPT_ROOT={{request.script_root|tojson|safe}};
</script>
<script type="text/javascript">
$(function (){
$.getJSON($SCRIPT_ROOT+'/_init_treeviews',function(data){
// alert(data.mview)
$('#sidebar').w2sidebar({
name: 'sidebar',
nodes: [
{ id: 'master', text: 'Master', img: 'icon-master', expanded: false, group: false,
// Add Nodes into sidebar
//nodes: data.mview
}
]
});
// Insert Nodes into sidebar , same as Add Nodes into sidebar
w2ui.sidebar.insert('master', null, data.mview);
});
});
</script>
本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1349235