Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Materialize css Tabs in Meteor?

New to Meteor.js and the Materialize CSS Framework. I don't fully understand how one would connect the Tabs to 3 different ones dynamically so when a user clicks it, the .tab indicator/active property slides over along with the desired route path. If I do this:

client/app.html

  <template name="tabs">
    <ul class="tabs nav-tabs hide-on-med-and-down">
      <li class="tab col s4" id="nt1"><a href="/page1">Page One</a></li>
      <li class="tab col s4" id="nt2"><a href="/page2">Page Two</a></li>
      <li class="tab col s4" id="nt3"><a href="/page3">Page Three</a></li>
    </ul>
  </template>

client/app.js

  Template.layout.rendered = function() {
    $('ul.tabs').tabs();
  }

The tab indicators work but it doesn't change the links to the rightful page. Its like its blocking the ability to go to page. I need help on how to do this, I've been at this for some time now. Thanks.

like image 975
John Huntington Avatar asked Nov 29 '15 16:11

John Huntington


2 Answers

Another Update if you dont want to define each tab you can do something like that

Js

//Manual Method
/*Template.tabs.rendered = function() {
   if(Router.current().route.path() === '/page2'){
      $("#nt2 a").addClass('active');
    }else if(Router.current().route.path() === '/page3'){
      $("#nt3 a").addClass('active');
    }  
    $('ul.tabs').tabs(); 
}


Template.tabs.events({
  'click #nt1': function(){
    Router.go('/');
  },
  'click #nt2': function(){
    Router.go('page2');
  },
  'click #nt3': function(){
    Router.go('page3');
  }

})*/ 

//Auto Method
Template.tabs.rendered = function() {   
    $("#"+Router.current().route.getName()).addClass('active');     
    $('ul.tabs').tabs(); 
  }


Template.tabs.events({
  'click .tabs li': function(e, t){
    var href = e.target.id;     
    Router.go(href);
  }  
})

Router

Router.configure({
  layoutTemplate: 'layout',  
});

Router.route('/', function () {  
  this.render('page-one');  
},{
  name: 'page-one'
});

Router.route('/page2', function () {
  this.render('page-two');
},{
  name: 'page-two'
});

Router.route('/page3', function () {
  this.render('page-three');
},{
  name: 'page-three'
});

Html

<template name="layout">
  {{> tabs}}

  {{> yield}} 
</template>
<template name="tabs">
    <!--Manual Method-->
    <!--<ul class="tabs nav-tabs hide-on-med-and-down">
       <li class="tab col s4" id="nt1"><a href="/">Page One</a></li>
       <li class="tab col s4" id="nt2"><a href="/page2">Page Two</a></li>
       <li class="tab col s4" id="nt3"><a href="/page3">Page Three</a></li>
    </ul>-->

    <!--Auto Method-->
    <ul class="tabs nav-tabs hide-on-med-and-down">
      <li class="tab col s4"><a id="page-one" href="/">Page One</a></li>
      <li class="tab col s4"><a id="page-two" href="/page2">Page Two</a></li>
      <li class="tab col s4"><a id="page-three" href="/page3">Page Three</a></li>
    </ul>
</template>

<template name="page-one">
    <p>i am page-one</p>
</template>

<template name="page-two">
    <p>i am page-two</p>
</template>


<template name="page-three">
     <p>i am page-three</p>
</template>
like image 69
Mat. Avatar answered Oct 23 '22 02:10

Mat.


This maybe useful if you are okay with rendering tabs without Router.

Let's say these are the templates you want to render stuff based on tab click:

<template name="page1">
.....
</template>

<template name="page2">
.....
</template>

<template name="page3">
.....
</template>

Now in your rendering area:

<template name="display">
    {{>tabs}}
    {{#if activeTab 'nt1'}}
        {{>page1}}
    {{/if}}

    {{#if activeTab 'nt2'}}
        {{>page2}}
    {{/if}}

    {{#if activeTab 'nt3'}}
        {{>page3}}
    {{/if}}
</template>

Finally in your javascript file:

activeTab = new ReactiveVar('nt1'); //Your default tab

Template.display.helpers({
    activeTab: function(tab){
        return (activeTab.get() == tab);
});

Template.tabs.events({
  'click #nt1': function(){
    activeTab.set('nt1');
  },
  'click #nt2': function(){
    activeTab.set('nt2');
  },
  'click #nt3': function(){
    activeTab.set('nt3');
  }
});
like image 1
Tabrez Ahmed Avatar answered Oct 23 '22 03:10

Tabrez Ahmed