Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
How To Create Collapsable Boxes
peterska2
Join Date: Oct 2003
Posts: 6,504

 

Manchester, UK
Show Printable Version Email this Page Subscription
peterska2 peterska2 is offline 06-28-2006, 10:00 PM

This tutorial explains how to create collapsable boxes throughout vBulletin.

For the purpose of this tutorial, I will explain how the first part of the 'What's Going On' box is structured (ie WOL on forumhome) and how you can use this technique to create stand alone collapsable boxes or add sections to additional boxes (eg Forum Statistics as part of the What's Going On box)

Part One: Decipering the What's Going On box

In your forumhome template, the following code exists by default:
Code:
<!-- what's going on box -->
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<thead>
    <tr>
        <td class="tcat" colspan="2">$vbphrase[whats_going_on]</td>
    </tr>
</thead>
<if condition="$show['loggedinusers']">
<!-- logged-in users -->
<tbody>
    <tr>
        <td class="thead" colspan="2">
            <a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activeusers')"><img id="collapseimg_forumhome_activeusers" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
            <a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)
        </td>
    </tr>
</tbody>
<tbody id="collapseobj_forumhome_activeusers" style="$vbcollapse[collapseobj_forumhome_activeusers]">
    <tr>
        <td class="alt2"><a href="online.php$session[sessionurl_q]" rel="nofollow"><img src="$stylevar[imgdir_misc]/whos_online.gif" alt="$vbphrase[view_whos_online]" border="0" /></a></td>
        <td class="alt1" width="100%">
            <div class="smallfont">
                <div style="white-space: nowrap"><phrase 1="$recordusers" 2="$recorddate" 3="$recordtime">$vbphrase[most_users_ever_online_was_x_y_at_z]</phrase></div>
                <div>$activeusers</div>
            </div>
        </td>
    </tr>
</tbody>
<!-- end logged-in users -->
</if>
This is the section of the code that we are going to look at.

Firstly, you will notice that the code starts with a table row, the same as any other table anywhere in vBulletin. This is essential. However, before the
Code:
<tr>
there is an additional tag
Code:
<thead>
This tag is used to create a header row for any tables that contain collapsable boxes. Generally speaking, this is only used if the table is going to contain more than one collapsable item.

All the code until
Code:
</thead>
is the same as any other table row, so you whould already be familiar with that.

Next we have the condition for the content section to show. For the purposes of this tutorial, the conditional is being disregarded.

This is then followed by the following code:
Code:
<tbody>
    <tr>
        <td class="thead" colspan="2">
            <a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activeusers')"><img id="collapseimg_forumhome_activeusers" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
            <a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)
        </td>
    </tr>
</tbody>
There are a few important aspects in this section, so we shall look at each one in turn.

First there is the
Code:
<tbody>
This tells us that this is a new section in the table body. The codes
Code:
<tr>
and
Code:
<td>
you should already be familiar with. Normal classes and colspans can be used within these tags as usual.

The next section is essential
Code:
<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activeusers')"><img id="collapseimg_forumhome_activeusers" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
This creates the collapse image at the far right of the row, and controls the collasping of the section. By default this section is open, and the button will collapse it. We will look at the code to do the reverse later in this tutorial.

Probably the most important section of this code, and the bit that is the cause for most problems are the labels
Code:
forumhome_activeusers
and
Code:
collapseimg_forumhome_activeusers
These appear three times in this one section of code, the first one being used once, and the second being used twice. These are also used later, so it is essential that care to detail is taken here. You will notice that the difference between the two codes is the prefix of
Code:
collapseimg_
on the second one. The rest of the code is the same.

When you are creating a new collapsable box, you need to give it a name. The length of the name is not important, as long as you ensure that every time it is used, it is identical. In the example used for this tutorial (the WOL on forumhome) the name used is
Code:
forumhome_activeusers
which is pretty self explanatory. I recommend using similar explanatory names for your collaspable boxes. It is essential that this is entered exactly the same for each occurance in the code, otherwise your box will not function correctly.

Some examples of names for collapsable boxes are:
  • thishack
  • this_hack
  • page_hack
  • author_hack
  • author_page_hack
  • random_number
All the the above are perfectly acceptable for use as names for the boxes. You will have your own preference as to what you want to call them. Personally I prefer the format author_hack as it makes it easiest to see exactly what they are for.

The next section is
Code:
<a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)
        </td>
    </tr>
</tbody>
which is pretty self explanatory. Basically, it contains the text to be displayed in the row including any links, if appropriate. It is then followed by closing tags for everything except the table. It is important that we do not close the table before we have added the next row, which is the collapsable content.

The next line is
Code:
<tbody id="collapseobj_forumhome_activeusers" style="$vbcollapse[collapseobj_forumhome_activeusers]">
You will notice that again there are two instances of
Code:
collapseobj_forumhome_activeusers
It is essential that these have the same name as the ones changed previously.

This line controls the collapsing of the box by identifying it, and setting the style of the box to match that of the image used earlier. (ie when collapsed the image shows a + and when expanded the image shows a - )

To have the box collapsed by default, you should use
Code:
<tbody id="collapseobj_forumhome_activeusers" style="$vbcollapse[collapseobj_forumhome_activeusers]; display:none">
However, any users that do not have javascript enabled will not be able to open these boxes.

Finally, we have the content for the collapsable row, followed by the closure tag
Code:
</tbody>
To end the table you would also add
Code:
</table>
after this.


Part Two: Creating Standalone Collapsable Boxes

Creating a standalone collapsable box (eg the one used for topXstats) is very straightforward. Literally, it involves following all the steps layed out in part one, using the same code and making adjustments as highlighted for the box name and setting the title and content to your requirements.

Using the
Code:
<thead>
section is optional. However, I would only use it if you are creating two or more associated collaspable sections within a table as the category sections (the part containing the collapse image) does not be hidden at any point.


Part Three: Adding To Existing Tables

You can add a collapsable section to any existing table. You simply use the steps laid out in part one, but omit the table and thead sections. You can place your collapsable section anywhere in a table, providing it is located in the center of the following code
Code:
</tr>
<tr>
This is to ensure that it is located correctly in a row in the table. Simply start with your first
Code:
<tbody>
and take it from there.

You can also add collapsable sections right at the beginning of a table by adding the code immediately after the table tag, or at the end of the table by adding the code directly above the end table tag.

An example of a modification that uses this technique is the Who Has Visited Today modification.


Part Four: Multiple Items Collapsing With One Control

Using the Whats Going On box as a primary example, what if you didn't want everything to be seperate, but rather all collapse apart from the statistics if you click a collapse icon?

This is very easy to do, and you simply use the same name for each section that you wish to have collapsed by the one icon. In this case, that would be the WOL section, upcoming events and birthdays. Statistics would retain it's own name otherwise that two would collapse with the rest.



I hope that you find this useful, and if you have any questions, please feel free to ask.
Reply With Quote
  #2  
Old 07-01-2006, 02:55 PM
Justine's Avatar
Justine Justine is offline
 
Join Date: Jun 2006
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

wow .. I just noticed that you have numerous articles for us newbies.


thank you :up:
Reply With Quote
  #3  
Old 07-02-2006, 04:48 AM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Is it possible to display text in place of the image for the collapse button?
Reply With Quote
  #4  
Old 07-02-2006, 10:35 PM
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 6,504
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't see why not, but I haven't experimented with it so I'm not sure how easy it will be to implement.
Reply With Quote
  #5  
Old 07-02-2006, 11:10 PM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well getting the text to show in place of the image is easy, it's getting it to change with the status of the container.
Reply With Quote
  #6  
Old 07-03-2006, 04:12 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nice job peterska2! I just love collapsible tables, i try to use em anywhere i can to make the best use of space, etc... I recently posted a how-to for developers on including them in the admincp for new hacks, etc... It can be seen at https://vborg.vbsupport.ru/showthread.php?t=118696 if anyone's interested.
Reply With Quote
  #7  
Old 07-22-2006, 12:40 PM
SiriusBlack22 SiriusBlack22 is offline
 
Join Date: Jun 2006
Location: Florida
Posts: 133
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What code would I insert into the what's going on table to make it collapsible? I got rather lost there.
Reply With Quote
  #8  
Old 07-22-2006, 03:11 PM
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 6,504
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

part three tells you how to add to existing tables, such as the whats going on box, and part one explains how to set one up.
Reply With Quote
  #9  
Old 07-22-2006, 05:03 PM
SiriusBlack22 SiriusBlack22 is offline
 
Join Date: Jun 2006
Location: Florida
Posts: 133
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Done.

Thanks.

Actually I did it and got a bunch of weird errors! And it was collapsing different boxes, it was a mess.

I still don't fully understand where to put it...
Reply With Quote
  #10  
Old 07-22-2006, 10:11 PM
peterska2 peterska2 is offline
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 6,504
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Start a new thread with what you want to add and where you want it and also what you had done and I'll give it a look over later.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:54 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04596 seconds
  • Memory Usage 2,316KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (22)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete