简体   繁体   English

如何在Meteor中使用Materialize css Tabs?

[英]How to use Materialize css Tabs in Meteor?

New to Meteor.js and the Materialize CSS Framework . Meteor.js和Materialise 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. 我不完全理解如何动态地将Tabs连接到3个不同的选项卡,因此当用户单击它时, .tab indicator / active属性将与所需的路径路径一起滑动。 If I do this: 如果我这样做:

client/app.html 客户机/ 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 客户机/ 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. 谢谢。

Another Update if you dont want to define each tab you can do something like that 另一个更新,如果您不想定义每个选项卡,您可以执行类似的操作

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

This maybe useful if you are okay with rendering tabs without Router . 如果您可以在没有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: 最后在你的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');
  }
});

Try 'Template.tabs.onRendered(function(){' rather than 'Template.tabs.rendered = function(){' 试试'Template.tabs.onRendered(function(){'而不是'Template.tabs.rendered = function(){'

In the end, your initialization of tabs should look like: 最后,您的选项卡初始化应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM