Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Tree View

Options
  • 15-12-2004 1:04pm
    #1
    Registered Users Posts: 604 ✭✭✭


    Hi,

    Im using ASP and i want to draw a tree view of Links that are grouped into folders. Each folder has an ID and a ParentID which is the ID of the folder its in. Root folders have an ID of 0. Links also have a parentID which is the ID of the folder they are contained in.

    So i want to be able to draw this tree of links out but im having problems. i was going to loop thru all the items and then take the following actions. If the item is a folder then i would draw this folder and all its contained items.

    but how can i do this ?

    anyone have any ideas ?

    feel free to ask questions as i dont think i explained this aas well as i could.


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Here's some psuedo code that should help
    function printChildren (folderID)
       for each folder with a parent id of folderID
          print out the folder name
          if the folder has children
             printChildren(thisfoldersID)     
    

    Sounds like you've got the right idea. You might want to implement indentation and conditional expansion of branches but just get it printing out everything for now, it's not that tough.


  • Registered Users Posts: 604 ✭✭✭Kai


    Enygma wrote:
    Here's some psuedo code that should help
    function printChildren (folderID)
       for each folder with a parent id of folderID
          print out the folder name
          if the folder has children
             printChildren(thisfoldersID)     
    

    Sounds like you've got the right idea. You might want to implement indentation and conditional expansion of branches but just get it printing out everything for now, it's not that tough.


    Thanks, thats pretty much what i had in mind but one problem im having is that im pulling these links from a DB and storing them in a recordset which is fine for the first Folder but when i try to display one of the inner folders then ill have to pull the info again into the same recordset therefore loosing what i already have. Or will i ??

    one possible solution is to use multiple recordsets but that isnt really an option if there are a large number of folders.

    So how can i manage to draw the sub folders without pulling them into a new recordset. i have a feeling the answer is really obvious but i cant see it.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    If your recordset has a parentfolderID or similar, then in your for each you can do a check to see if it matches the folderID passed in to printChildren, if it does print it, if not go on to the next. Assuming of course I understand your problem correctly ?


  • Moderators, Politics Moderators Posts: 39,765 Mod ✭✭✭✭Seth Brundle


    database tables
    tblFolders
    folder_name
    folder_id

    tblLinks
    link_id
    link text
    link url
    parent_folder_id (if it is a top level link then fill this else set to null)
    parent_link_id (if it is a child link then include its parent link id here)

    select * from tblFolders order by folder_name (using a loop inside this recordset do the following)
    select * from tblLinks where parent_folder_id is not null and parent_folder_id = the current tblFolders.folder_id order by link_text (then create another loop for the following)
    select * from tblLinks where parent_link_id is not null and parent_folder_id = the current parent_folder_id order by link_text

    You may need to tweak it to fit items if they are both parent and child but you get the gist


  • Registered Users Posts: 604 ✭✭✭Kai


    kbannon wrote:
    database tables
    tblFolders
    folder_name
    folder_id

    tblLinks
    link_id
    link text
    link url
    parent_folder_id (if it is a top level link then fill this else set to null)
    parent_link_id (if it is a child link then include its parent link id here)

    select * from tblFolders order by folder_name (using a loop inside this recordset do the following)
    select * from tblLinks where parent_folder_id is not null and parent_folder_id = the current tblFolders.folder_id order by link_text (then create another loop for the following)
    select * from tblLinks where parent_link_id is not null and parent_folder_id = the current parent_folder_id order by link_text

    You may need to tweak it to fit items if they are both parent and child but you get the gist


    yea that part is fine, I can pull out what i need from the DB but the problem im having is :

    Say i have a tree structure like
    root/folderA/FolderA1/FolderA11/

    Now i can display Root no problem,
    I drop into a Subroutine which pulls out all the items in FolderA and stores it in recordset1.

    Now how do i display whats in FolderA1 and then subsequently whats in FolderA11 ??

    can i call the same subroutine which will pull all the items in FolderA1 into Recordset1 but i then loose what was in Recordset1.

    Im possibly over complicating it.


    Can anyone exlain how to draw the subfolders without using multiple recordsets. I know it can be done but i cant seem to figure it out.


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    If you write a recursive function like the one I gave above in pseudo code (note line 5 it calls itself) then you shouldn't have to worry about it too much, unless it goes really deep.

    The recordset should be private to the function so each time it is called a new recordset is automatically created for that "instance" of the function. Obviously then the deeper it goes the more recordsets are opened, you just need to be careful about what you're opening.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Use Enygma's recursive function, but have just one recordset scoped above it. Fill it with all the entries and at each stage of the recursion just use the rows in the recordset with a parentID that matches the folderID passed into it. I'm not sure of the most efficient way of filtering in the recordset but you can use something along the lines of
    For each row in recordset
       if row.parentID = folderID then
          drawItem()
       else
          'do nothing
       end if
    loop
    

    Of course for that to work, each item has to know what it's parent is but I presume you have that anyway if you were going to have seperate recordsets for each of them.


Advertisement