Wednesday, May 9, 2012

How to create Sublabels / Hierarchical Labels in Blogspot

I always wanted my label section to look like the archive section where labels can have hierarchy and the number of posts is displayed beside each label. I learned how to create the hierarchical tree from this blog but the nodes have to be added one by one and the number of posts per label are not displayed so I tweaked it a little to suit my needs. Here are the steps:
  1. Add the Labels gadget if is not yet added
    From your dashboard, click Layout -> Add a Gadget and select Labels
  2. Add an HTML gadget. This is where you will create your hierarchical labels.
    From your dashboard, click Layout -> Add a Gadget and select HTML/Javascript
  3. Ensure that the Labels gadget comes first before the HTML gadget in your layout.
    From your dashboard, click Layout and arrange your gadgets by dragging them to the desired location so that the Labels gadget is on top or comes first before the HTML gadget
  4. Update the Template
    1. Add the dtree javascript.
      From your dashboard, click Template -> Edit HTML -> Proceed and check Expand Widget Templates. Copy and paste this code
      <link href='https://sites.google.com/site/efekefek/file-js/dtree.css' rel='StyleSheet' type='text/css'/>
      <script src='https://sites.google.com/site/efekefek/file-js/createdtree.js' type='text/javascript'/>
      after this tag
      <b:skin><![CDATA[/*
    2. Remove the Label widget content
      Find the Label widget code. It looks like this
      <b:widget id='Label1' locked='false' title='Labels' type='Label'>
      <b:includable id='main'>
          
       ...  
      
      </b:includable>
      </b:widget>
      Replace the code inside <b:includable id='main'> and </b:includable> with this code
      <script type='text/javascript'>
        var labelCountMap = {};
        <b:loop values='data:labels' var='label'>
           labelCountMap ["<data:label.name/>"] = "<data:label.count/>";
         </b:loop>
      </script>
    3. Save template
  5. Prepare the code of the Hierarchical Labels
    Copy the code below and replace the data with your own Labels. The data is in format
    parentLabel: { subLabels }
    The subLabels are comma separated. If the parentLabel doesn't have subLabels, replace it with {}.
    <div class="dtree">
    <p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
    <script type="text/javascript">
    <!--
    function isEmpty(obj) {
        for(var prop in obj) {
            if(obj.hasOwnProperty(prop))
                return false;
        }
        return true;
    }
    
    function addMap(d, startingNode, parentNode, map) {
     for (var key in map) {
      if (isEmpty(map[key])) {
       d.add(startingNode++,parentNode,key+' ('+labelCountMap[key]+')','/search/label/'+key);
      } else {
       d.add(startingNode++,parentNode,key+' ('+labelCountMap[key]+')','/search/label/'+key);
       startingNode = addMap(d, startingNode, startingNode-1, map[key]);
      }
      
     }
     return startingNode; 
    }
    
    var data = {
     'Label 1' : {
      'Label 1.1' : {},
      'Label 1.2' : {
       'Label 1.2.1' : {}
      }, 
      'Label 1.3' : {}
     }, 
     'Label 2' : {
      'Label 2.1' : {}, 
      'Label 2.2' : {}
     }, 
     'Label 3' : {}, 
     'Label 4' : {}};
    
    d = new dTree('d');
    d.config.useLines = true;
    d.config.useIcons = false;
    d.config.inOrder = true;
    
    d.add(0,-1,'');   
    addMap(d, 1, 0, data);
    
    document.write(d);
    //-->
    </script>
    </div>
  6. Paste the code in the HTML widget
    From your dashboard, click Layout and Edit the HTML widget. Paste the code inside Content and Save

21 comments:

  1. I tried your code. It works well with the first 5 tags. But No more. Do you know why.

    ReplyDelete
  2. It should work with any number of labels. You just need to add the labels in the data. For example, if you want to add Label 5 and Label 6, update the code this way:

    var data = {
    'Label 1' : {
    'Label 1.1' : {},
    'Label 1.2' : {
    'Label 1.2.1' : {}
    },
    'Label 1.3' : {}
    },
    'Label 2' : {
    'Label 2.1' : {},
    'Label 2.2' : {}
    },
    'Label 3' : {},
    'Label 4' : {},
    'Label 5' : {},
    'Label 6' : {}};

    Also make sure that the format is correct - no extra or missing commas.

    ReplyDelete
  3. Hello,

    I am not sure why it is not working for me. I made changes as per steps , I am getting the open all and close all widget, but there is no post coming ..it doesn't have anything.

    you can check http://wizardofjaipur.blogspot.kr/2015/05/how-to-make-volte-device-part-1.html

    ReplyDelete
    Replies
    1. Your Labels gadget might be located below the HTML gadget. Make sure to follow Step 3.

      Delete
  4. Thank you for your insight post.
    I practiced for about 2 weeks to learn(and web programming)
    I finally made the Hierarchical label.
    and I wrote it on my blog(Korean, English Version)

    English
    http://stackrefactoring.blogspot.kr/2016/03/how-to-create-hierarchy-labelstree.html
    Korean
    http://stackrefactoring.blogspot.kr/2016/03/blog-post_30.html

    ReplyDelete
    Replies
    1. Hi thank you both!
      I'm now able to sorting my blogs really easily!

      Delete
  5. What does it mean to "replace the data with your own Labels"?

    ReplyDelete
    Replies
    1. I have the same problem as CSR, and I followed instruction 3 correctly (but perhaps it's a problem that I had another Label widget underneath the HTML widget ..?)

      Delete
    2. Is this the kind of thing that's meant to be there?

      var data = {
      'Short stories' : {
      'Humorous' : {},
      'Earnest' : {}
      },
      'Essays' : {
      'Philosophy Essays' : {
      'Ontology/truth' : {},
      'Meta-ethics' : {},
      'Philosophy of Mathematics' : {}
      },
      'Political and Economic Essays' : {}
      },
      'My Bleak Memoirs' : {},
      'Diary Entries' : {},
      'Poems' : {},
      'Plays' : {},
      'Lists' : {},
      'Sports journalism' : {}};

      Delete
    3. Yes, the code above is what I meant by "replacing you data with your own labels". You may be right that multiple Label widgets is your problem but I am not sure. Just try removing the other Label widget first.

      Delete
  6. Hi There Mr Admin

    I'm stuck after applying all these 6 steps, when only open and close link appeared but not the others

    is the css and js link still valid or not?

    have i done wrong somewhere?

    thanks for your help in advance

    ReplyDelete
  7. It is no longer working. Show me why in your blog it is still working!
    Thank you

    ReplyDelete
  8. Very nice post to keep sharing.. Thanks for giving very nice information from your post.. Oracle Training in Chennai | Oracle Training institutes in Chennai

    ReplyDelete
  9. I've followed those steps but it didn't work.
    And is it possible for those tags to be dynamically created. Like the regular ones. When I create a new message, I would just enter the tag in some nested format like
    label 1 {label 1.1}
    and it would be added to the widget.
    Thank you.

    ReplyDelete
  10. Nice Blog. Thanks for sharing with us. Such amazing information. Get Hang Tag Printing From PrintMagic and move away from boring and plain marketing materials. PrintMagic offers several customization options for water bottle hang tags and you can print them in different sizes ranging from 1.5" x 3.5” to 4" x 6”. Pick from a rectangle or square shape with the option for square or rounded corners.

    ReplyDelete
  11. Thanks for these kind of posts, its really appreciated and keep updating your posts.

    Rectangle Stickers

    ReplyDelete