2017-05-20 11 views
0

Vuejs 2.0の複数の要素のクラスを切り替える際に、btn-primaryのクラスを持つ次のボタンがあります。ボタンをクリックすると、その特定の要素のサブグループが表示されます。これは私のコードです:私はdata()に次きvue jsの複数の要素のクラスを切り替える方法

<div v-if="tag.investor"> 
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - Mutual Funds')">Mutual Fund</button> 
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - Insurance')">Insurance</button> 
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - FII')">FII</button> 
</div> 
<div v-if="tag.research"> 
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Research - Tier I')">Research - Tier I</button> 
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Research - Tier II')">Research - Tier II</button> 
</div> 

tag: { 
    investor: false, 
    research: false, 
    company: false, 
    others: false, 
}, 

そしてmethods中:

getTags: function (tag) { 
    this.tag.investor = this.tag.research = this.tag.company = this.tag.others = false 
    if(tag == 'investor') 
    { 
     this.tag.investor = true 
    } 
    if(tag == 'research') 
    { 
     this.tag.research = true 
    } 
    if(tag == 'company') 
    { 
     this.tag.company = true 
    } 
    if(tag == 'others') 
    { 
     this.tag.others = true 
    } 
}, 
<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('investor')">Investor</button> 
<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('research')">Research</button> 
<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('company')">Company</button> 

これは、次の要素を示しています10子要素が選択されたら、btn-warningのクラスを持ち、btn-primaryを削除します。どのようにこれを実装するためのアイデア、私は個々のデータ要素を持って、クラスを切り替えるしたくないです。

答えて

4

私はあなたのVueにデータ駆動型アプローチを提案したいと思います。

const tags = { 
    Investor:[ 
    {display:"Mutual Fund", value:"Investor - Mutual Funds"}, 
    {display:"Insurance", value:"Investor - Insurance"}, 
    {display:"FII", value:"Investor - FII"}, 
    ], 
    Research:[ 
    {display:"Research - Tier I", value:"Research - Tier I"}, 
    {display:"Research - Tier II", value:"Research - Tier II"}, 
    ] 
} 

これを使用すると、テンプレートをかなりクリーンアップし、データ構造に追加するタグをすべて処理できます。

<div id="app"> 
    <button v-for="(obj, key) in tags" 
      :key="key" 
      @click="currentTag = key" 
      class="btn btn-xs btn-primary"> 
    {{key}} 
    </button> 

    <div> 
    <button v-for="tag in tags[currentTag]" 
      :key="tag" 
      class="btn btn-xs" 
      :class="tagClass(tag)" 
      @click="selectTags(tag.value)"> 
     {{tag.display}} 
    </button> 
    </div> 
</div> 

あなたのVueもきれいに見えます。

new Vue({ 
    el:"#app", 
    data:{ 
    currentTag: null, 
    tags, 
    selectedTags:[] 
    }, 
    methods:{ 
    selectTags(tag){ 
     const index = this.selectedTags.findIndex(t => t == tag) 

     if (index >= 0) 
     this.selectedTags.splice(index, 1) 
     else 
     this.selectedTags.push(tag) 
    }, 
    tagClass(tag){ 
     const isSelected = this.selectedTags.includes(tag.value) 

     return { 
     'btn-warning': isSelected, 
     'btn-primary': !isSelected 
     } 
    } 
    } 
}) 

ここにはexampleがあります。

+0

答えに感謝しています。 1つの最終要素を選択したい場合はどうしたらいいですか?ちょうど知識のために。 –

+0

@NitishKumar "one end element"とは何を意味しますか? 1つのタグを選択することを意味しますか?カテゴリごとに1つのタグ? – Bert

+0

任意のカテゴリのタグは1つだけです。つまり、finalまたはselectedTagを複数の値の配列ではなく1つの値にしたいということです。 –

1

v-bind:classディレクティブを使用できます。

<button 
    class="btn btn-xs" 
    v-bind:class="{ 'btn-warning': tag.research, 'btn-primary': !tag.research }" 
    v-on:click.prevent="selectTags('Research - Tier I')" 
> 
    Research - Tier I 
</button> 
+0

私は 'v-bind:class'を使っていることは知っていますが、すべての要素クラスがトグルされるので、管理するのが繰り返しやすく、非常に難しい要素がたくさんあります。同様に 'Research Tier II'要素に何が起こるのか、' Research Tier III、IV'要素がある場合はどうなるでしょうか –

関連する問題