ADVERTISEMENT


Breaking News

Recent Posts

Tuesday, June 24, 2014

Creating Module with Tabbed Content

In this Joomla Module Tutorial you will find how to create tabbed module. Creating the module with tabbed pane requires some knowledge of javascript.
1) <div> tag is added for each Tabbed Menu, which is placed in one row in <table>. Unique id is given to each tabbed menu.
2) Content to be displayed is also placed in <div> tag in another row of the <table>, with another id.
3) Click event is added to each tabbed menu in Javascript when the document is loaded using addEventListener() function for non IE and attachEvent() function for IE.
View source
  1. document.getElementById('divTitleTab1').addEventListener("click",titleDivTab1Func, false,true);
For non Internet Explorer and
View source
  1. document.getElementById('divTitleTab1').attachEvent("onclick",titleDivTab1Func);
For Internet Explorer.
titleDivTab1Func is the name of the function to be executed when click event occurs.

How to determine whether IE or non IE?

window.addEventListener returns true for non IE and false for IE.
View source
  1. if(window.addEventListener) {
  2.  
  3. }
  4. else {
  5.  
  6. }
4) titleDivTab1Func function contains the code to be executed when user clicks on the tabbed menu. If user clicks on the tabbed menu its content should be displayed below it. It is done by setting innerHTML of the element with id of the content div.
View source
  1. document.getElementById("divContentTab").innerHTML = " you are under Tab 1";
5) Calling Javascript from helper.php
It is done by script() function of the JHtml class, which is used as shown below.
View source
  1. JHTML::_('script',"tabbed.js",JURI::base().'/modules/mod_tabbed/js/',true);
The last argument determines whether to load Mootools or not. Mootools is loaded if this argument is true.
Here we have created 4 tabbed menu.

Files Required

1) mod_tabbed.php: This file is the entry point for the module. It will perform necessary initializations and call helper routine to collect necessary data and include the template which will display module output.
2) helper.php: This file contains the helper class which will collect necessary data to be used in the module from database or any other sources.
3) mod_tabbed.xml: This file contains the information about the module. This is the installation file for the module.
4) tmpl/default.php: This is the file used for displaying the module output.
5) js/tabbed.js: This file will contain necessary javascript code to be executed.

1) Creating file mod_tabbed.php

The Complete code of mod_tabbed.php is
View source
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. // Include the syndicate functions only once
  5. require_once( dirname(__FILE__).DS.'helper.php' );
  6. $hello = modTabbedHelper::getHello( $params );
  7. require( JModuleHelper::getLayoutPath( 'mod_tabbed' ) );
  8. ?>

2) Creating file helper.php

This file contains the class as defined in mod_ varreq.php ,here it is modVarreqHelper class and contains function getHello().
The complete code of helper.php is
View source
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. class modTabbedHelper
  5. {
  6. static function getHello($params)
  7. {
  8. JHTML::_('script',"tabbed.js",JURI::base().'/modules/mod_tabbed/js/',true);
  9. return 'Helper Tabbed Pane';
  10. }
  11. }
  12. ?>

3) Creating installation file mod_tabbed.xml

This file contains the information about the module.
The complete code of mod_tabbed.xml is
View source
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <extension type="module" version="2.5" client="site" method="upgrade">
  3. <name>tabbed</name>
  4. <author>Larenge Kamal</author>
  5. <version>1.7</version>
  6. <description>Tabbed Pane! module.</description>
  7. <files>
  8. <filename>mod_tabbed.xml</filename>
  9. <filename module="mod_tabbed">mod_tabbed.php</filename>
  10. <filename>index.html</filename>
  11. <filename>helper.php</filename>
  12. <filename>tmpl/default.php</filename>
  13. <filename>tmpl/index.html</filename>
  14. <filename>tmpl/logo1.bmp</filename>
  15. <filename>js/tabbed.js</filename>
  16. </files>
  17. <config>
  18. </config>
  19. </extension>

4) Creating file tmpl\default.php

This file contains the output to be displayed by the module. This file has the same scope as that of the mod_tabbed.php. So the variables defined in mod_tabbed.php can be directly accessed in this file. ‘$hello’ variable defined in mod_tabbed.php can be directly accessed here.
The complete code of tmpl\default.php is
View source
  1. <?php
  2. // no direct access
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4. ?>
  5. <html>
  6. <body>
  7. <table style="border:2px solid #E0E0E0; padding:0px; margin:0px" width="100%" cellspacing="0" cellpadding="0">
  8. <tr>
  9. <td style="padding:0px; margin:0px" width="auto">
  10. <div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab1" class="mainTitle">
  11. <b id="subTitle">Tab1</b>
  12. </div>
  13. </td>
  14. <td style="padding:0px; margin:0px">
  15. <div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab2" class="mainTitle">
  16. <b id="subTitle">Tab2</b>
  17. </div>
  18. </td>
  19.     
  20. <td style="padding:0px; margin:0px">
  21. <div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab3" class="mainTitle">
  22. <b id="subTitle">Tab3</b>
  23. </div>
  24. </td>
  25.     
  26. <td style="padding:0px; margin:0px">
  27. <div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab4" class="mainTitle">
  28. <b id="subTitle">Tab4</b>
  29. </div>
  30. </td>
  31.     
  32. </tr>
  33. <tr>
  34. <td colspan="4" style="padding:0px; margin:0px">
  35. <div style="color: black;padding: 0px 0px 0px 10px;border:2px solid #E0E0E0" id="divContentTab" class="mainTitle">
  36. here is tab content
  37. </div>
  38. </td>
  39.     
  40. </tr>
  41. </table>
  42. </body>
  43. </html>

5) Creating file index.html common to all

The complete code of index.html is
View source
  1. <html><body bgcolor="#FFFFFF"></body></html>
which will display an empty page.

6) Creating file js\tabbed.js

This file contains the javascript code.
The complete code of js\tabbed.js is
View source
  1. window.addEvent("domready",function(){
  2. if(window.addEventListener) {
  3. document.getElementById('divTitleTab1').addEventListener("click",titleDivTab1Func, false,true);
  4. document.getElementById('divTitleTab2').addEventListener("click",titleDivTab2Func, false,true);
  5. document.getElementById('divTitleTab3').addEventListener("click",titleDivTab3Func, false,true);
  6. document.getElementById('divTitleTab4').addEventListener("click",titleDivTab4Func, false,true);
  7. }
  8. else if(window.attachEvent) { //IE
  9. document.getElementById('divTitleTab1').attachEvent("onclick",titleDivTab1Func);
  10. document.getElementById('divTitleTab2').attachEvent("onclick",titleDivTab2Func);
  11. document.getElementById('divTitleTab3').attachEvent("onclick",titleDivTab3Func);
  12. document.getElementById('divTitleTab4').attachEvent("onclick",titleDivTab4Func);
  13. }
  14. });
  15.  
  16. function titleDivTab1Func() {
  17. var oDiv = document.getElementById("divContentTab");
  18. oDiv.style.display = "block";
  19. document.getElementById("divTitleTab1").style.background = "#FFFFFF";
  20. document.getElementById("divTitleTab2").style.background = "#F3F3F3";
  21. document.getElementById("divTitleTab3").style.background = "#F3F3F3";
  22. document.getElementById("divTitleTab4").style.background = "#F3F3F3";
  23. oDiv.innerHTML = " you are under Tab 1";
  24. }
  25.  
  26. function titleDivTab2Func() {
  27. var oDiv = document.getElementById("divContentTab");
  28. oDiv.style.display = "block";
  29. document.getElementById("divTitleTab2").style.background = "#FFFFFF";
  30. document.getElementById("divTitleTab1").style.background = "#F3F3F3";
  31. document.getElementById("divTitleTab3").style.background = "#F3F3F3";
  32. document.getElementById("divTitleTab4").style.background = "#F3F3F3";
  33. oDiv.innerHTML =" You are in Tab 2" ;
  34. }
  35.  
  36. function titleDivTab3Func() {
  37. var oDiv = document.getElementById("divContentTab");
  38. oDiv.style.display = "block";
  39. document.getElementById("divTitleTab3").style.background = "#FFFFFF";
  40. document.getElementById("divTitleTab1").style.background = "#F3F3F3";
  41. document.getElementById("divTitleTab2").style.background = "#F3F3F3";
  42. document.getElementById("divTitleTab4").style.background = "#F3F3F3";
  43. oDiv.innerHTML ="Congratulations! You have a Joomla! site! Joomla! makes it easy to build a website just the way you want it and keep it simple to update and maintain.in tab3" ;
  44. }
  45.  
  46. function titleDivTab4Func() {
  47. var oDiv = document.getElementById("divContentTab");
  48. oDiv.style.display = "block";
  49. document.getElementById("divTitleTab4").style.background = "#FFFFFF";
  50. document.getElementById("divTitleTab1").style.background = "#F3F3F3";
  51. document.getElementById("divTitleTab2").style.background = "#F3F3F3";
  52. document.getElementById("divTitleTab3").style.background = "#F3F3F3";
  53. oDiv.innerHTML ="Joomla! is a flexible and powerful platform, whether you are building a small site for yourself or a huge site with hundreds of thousands of visitors. You are in Tab 4" ;
  54. }

Now create the zip file of the folder ‘mod_tabbed’ which contains the following files.
1) mod_tabbed.php
2) index.html
3) mod_tabbed.xml
4) helper.php
5) tmpl\default.php
6) tmpl\index.html
7) js\tabbed.js
The above zip file can now be installed using Joomla extension manager.
After installing the module,’ tabbed’ module will appear in the module manager.
Enable this new module and set the position for display of this module to see the output of the module as shown below.

No comments:

Post a Comment

Designed By Published.. Blogger Templates