如何在 Meteor 中使用 Materialise css 选项卡?

How to use Materialize css Tabs in Meteor?

Meteor.js 和 Materialize CSS Framework 的新功能。我不完全理解如何将选项卡动态连接到 3 个不同的选项卡,因此当用户单击它时,.tab indicator/active 属性 会随着所需的路线路径滑过。如果我这样做:

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();
  }

选项卡指示器有效,但它不会更改指向正确页面的链接。 就像它阻止了转到页面的能力一样。我需要有关如何执行此操作的帮助,我已经这样做了一段时间了。谢谢。

另一个更新如果你不想定义每个选项卡你可以做类似的事情

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.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>

如果您可以在没有 Router 的情况下呈现选项卡,这可能会有用。

假设这些是您要根据选项卡点击呈现内容的模板:

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

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

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

现在在您的渲染区域中:

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

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

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

终于在您的 javascript 文件中:

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');
  }
});

尝试 'Template.tabs.onRendered(function(){' 而不是 'Template.tabs.rendered = function(){'

最后,您的选项卡初始化应如下所示:

    Template.tabs.onRendered(function(){
      $("ul.tabs").tabs();
    });