472,993 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

HTML Tree View with <ol> and <a href>

Hi,

Before I reinvent the wheel I`d like to ask if someone has done this
before since I did not find an advice at Google.

The goal is to create a dynamic Tree View in HTML.

Say I have a data strucure like this:

structList =
{'Sun':{'Sun.1':['Sun1.1','Sun1.2'],'Sun.2':['Sun2.1','Sun2.2']},'Kupa':['Kupa1']}

I want to transform this into HTML:

<ol>
<li><a href="?a=Sun">Sun</a></li>
<ol>
<li><a href="?a=Sun.1">Sun.1</a></li>
<ol>
<li><a href="?a=Sun1.1">Sun1.1</a></li>
<li><a href="?a=Sun1.2">Sun1.2</a></li>
</ol>
<li><a href="?Sun.2">Sun.2</a></li>
<ol>
<li><a href="?a=Sun2.1">Sun2.1</a></li>
<li><a href="?a=Sun2.2">Sun2.2</a></li>
</ol>
</ol>
<li><a href="?a=Kupa">Kupa</a></li>
<ol>
<li><a href="?a=Kupa1">Kupa1</a></li>
</ol>
</ol>

If the user clicks on a branch-link say 'Sun.1' then the branch below
opens/closes (not printed by the servlet). Like a tree view control in
an native GUI app.

Has this, or a similar approach, been done before in python ( I am using
webware/cheetah)?

--
Greg
Jul 18 '05 #1
2 3826
While not EXACTLY what you outlined, I've used TreeView
http:www.treeview.net along with Python to create the
dynamic tree menus you describe. I had to write a
couple or very short JavaScript additions, but it is
working just fine. All the tree opening and closing
is done on the client via-javascript so it is very
fast.

Larry Bates
Gregor Horvath wrote:
Hi,

Before I reinvent the wheel I`d like to ask if someone has done this
before since I did not find an advice at Google.

The goal is to create a dynamic Tree View in HTML.

Say I have a data strucure like this:

structList =
{'Sun':{'Sun.1':['Sun1.1','Sun1.2'],'Sun.2':['Sun2.1','Sun2.2']},'Kupa':['Kupa1']}
I want to transform this into HTML:

<ol>
<li><a href="?a=Sun">Sun</a></li>
<ol>
<li><a href="?a=Sun.1">Sun.1</a></li>
<ol>
<li><a href="?a=Sun1.1">Sun1.1</a></li>
<li><a href="?a=Sun1.2">Sun1.2</a></li>
</ol>
<li><a href="?Sun.2">Sun.2</a></li>
<ol>
<li><a href="?a=Sun2.1">Sun2.1</a></li>
<li><a href="?a=Sun2.2">Sun2.2</a></li>
</ol>
</ol>
<li><a href="?a=Kupa">Kupa</a></li>
<ol>
<li><a href="?a=Kupa1">Kupa1</a></li>
</ol>
</ol>

If the user clicks on a branch-link say 'Sun.1' then the branch below
opens/closes (not printed by the servlet). Like a tree view control in
an native GUI app.

Has this, or a similar approach, been done before in python ( I am using
webware/cheetah)?

--
Greg

Jul 18 '05 #2
Gregor Horvath <g.*******@gmx.at> writes:
Hi, Before I reinvent the wheel I`d like to ask if someone has done this
before since I did not find an advice at Google. The goal is to create a dynamic Tree View in HTML. Say I have a data strucure like this: structList =
{'Sun':{'Sun.1':['Sun1.1','Sun1.2'],'Sun.2':['Sun2.1','Sun2.2']},'Kupa':['Kupa1']} I want to transform this into HTML: <ol>
<li><a href="?a=Sun">Sun</a></li>
<ol>
<li><a href="?a=Sun.1">Sun.1</a></li>
<ol>
<li><a href="?a=Sun1.1">Sun1.1</a></li>
<li><a href="?a=Sun1.2">Sun1.2</a></li>
</ol>
<li><a href="?Sun.2">Sun.2</a></li>
<ol>
<li><a href="?a=Sun2.1">Sun2.1</a></li>
<li><a href="?a=Sun2.2">Sun2.2</a></li>
</ol>
</ol>
<li><a href="?a=Kupa">Kupa</a></li>
<ol>
<li><a href="?a=Kupa1">Kupa1</a></li>
</ol>
</ol> If the user clicks on a branch-link say 'Sun.1' then the branch below
opens/closes (not printed by the servlet). Like a tree view control in
an native GUI app. Has this, or a similar approach, been done before in python ( I am using
webware/cheetah)?


Here is a browser side solution that you can have a play with:

---------------------------- htmltree.py ---------------------------
import sys

id = 0
def new_id ():
global id
id += 1
return id

def mk_expander (id, lab='+'):
return '<A href="javascript:void 0;" class="expander" onclick="togglen(%d);" onmouseover="return true;" id="e.%s">%s</A>' % (id,id,lab)

def start_invisible (id):
return '<DIV id="%d" style="display: none">' % id

def finish_invisible (dst):
dst.write ('</DIV>')

def build_tree (dst, node):
dst.write ('<ol>\n')
for branch in node:
##sys.stderr.write ('-- %s\n'%branch)
id = new_id()
if type(node) == type([]):
dst.write ('<li>%s\n' % branch)
else:
dst.write ('<li>%s\n%s' % (mk_expander (id,branch),
start_invisible(id)))
build_tree (dst, node[branch])
finish_invisible (dst)

style_stuff = '''
<style type="text/css">
A.expander {border-style: solid; border-width: thin 0 thin thin}
A {text-decoration: none}
..mono {font-family : monospace}
TD {text-align : right}
TR.col {background: #9F9 }
..link {background: #99f;
border-style : solid}
</style>'''

print '<HEAD><TITLE>BLAH</TITLE>'
print '<script src="hier.js"></script>'
print style_stuff
print '''
</HEAD>
<body>
'''

structList = {'Sun':{'Sun.1':['Sun1.1','Sun1.2'],'Sun.2':['Sun2.1','Sun2.2']},'Kupa':['Kupa1']}

build_tree (sys.stdout, structList)
print '</body>\n</html>\n'

-----------------------------------------------------------------

and the javascript file that goes with it
---------------------------------hier.js --------------------------
visibility_state = new Array();
highlighted = new Array();
selected_node = null;
selected_id = null;
function get_visibility (id) {
var m = visibility_state[id];
if (m == undefined)
m = "none";
return m;
}
function set_visibility (id, e, vis) {
e.style.display = vis;
visibility_state[id] = vis;
}
function togglen(id) {
toggle_branch (id);
do_select (id);
return false;
}
function toggle_branch(id) {
var e = document.getElementById(id);
var m = get_visibility (id);
if (m == "none")
m = "inline";
else
m = "none";
set_visibility (id, e, m);
}
function show_branch (id) {
var e = document.getElementById(id);
set_visibility (id, e, "inline");
}
function hide_branch (id) {
var e = document.getElementById(id);
set_visibility (id, e, "none");
}
function do_select (id) {
if (selected_id != null) {
var e = document.getElementById("e."+selected_id);
e.style.backgroundColor = 'transparent';
}
var n = document.getElementById("e."+id);
n.style.backgroundColor = 'red';
selected_node = document.getElementById(id);
selected_id = id;
}
function set_visibility_all (n, vis) {
if (n == null) return false;
if (n.nodeType == 1) {
if (n.tagName == "DIV" && n.id != "")
set_visibility (n.id, n, vis);
for (var c = n.firstChild; c != null; c = c.nextSibling)
set_visibility_all (c, vis);
}
}

-------------------------------------------------------------------

the javascript code is extracted from a larger set of functions that include a
popup menu with inbuilt search (which opens tree levels as necessary), if
you're interested.

Eddie
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark | last post by:
Hopefully I 'm missing something silly, but I can't find an easy way to loop all list items in a simple <ol>. I was hoping a for loop as shown below would be enough, however clicking "alert all" in...
3
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink' href='javascript:AddFileInput()">Attach More Files </a> <input...
12
by: hawat.thufir | last post by:
I'm trying do some "screen scraping", and am using <http://www.oreilly.com/catalog/xmlhks/> for inspiration. First I'd like to convert XHTML to XML, or extract XML from XHTML, I'm not sure how...
2
by: hawat.thufir | last post by:
cross-posted to: mailing.database.myodbc,comp.text.xml I have an xhtml file whose data I'd like to import to MySQL. Unfortunately, mysqlimport will only work with text files. Mixed in with text...
7
by: hawat.thufir | last post by:
Given an xhtml file, how can I "export" the data to plain-text? That is, I want: google www.google.com Whereas, if I copy and paste what the browser shows, I lose the URL and end up with:...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
3
by: Bill | last post by:
Hi In a css page I have the following: ..OLC{float:left; width:150px;text-align:center; } OL{ list-style-position:inside;} LI{ height:160px;}
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.