【问题标题】:How can I activate child category in treeview on the vue component?如何在 vue 组件的树视图中激活子类别?
【发布时间】:2018-09-02 19:10:06
【问题描述】:

我有两个 vue 组件。

我的第一个组件(父组件)是这样的:

<template>
    ...
        <ul class="filter-category" v-for="list in categories">
            <list-category :data="list" :category-id="categoryId"></list-category>
        </ul>
    ...
</template>
<script>
    ...
    export default {
        ...
        data() {
            return {
                categories: [
                    {
                        id: 1,
                        name: 'England',
                        children: [
                            {
                                id: 3,
                                name: 'Chelsea',
                                children: [
                                    {id: 7, name: 'Hazard'},
                                    {id: 8, name: 'Morata'}
                                ]
                            },
                            {
                                id: 4,
                                name: 'Manchester United',
                                children: [
                                    {id: 9, name: 'Pogba'},
                                    {id: 10, name: 'Lukaku'}
                                ]
                            }
                        ]
                    },
                    {
                        id: 2,
                        name: 'Spain',
                        children: [
                            {
                                id: 5,
                                name: 'Real Madrid',
                                children: [
                                    {id: 11, name: 'Ronaldo'},
                                    {id: 12, name: 'Bale'}
                                ]
                            },
                            {
                                id: 6,
                                name: 'Barcelona',
                                children: [
                                    {id: 13, name: 'Messi'},
                                    {id: 14, name: 'Suarez'}
                                ]
                            },
                        ]
                    }
                ],
                categoryId: 7
            }
        }
    }
</script>

我的第二个组件(子组件)是这样的:

<template>
    <li>
        <!--parent-->
        <a v-if="isFolder" href="javascript:" @click="toggle">
            <span class="fa fa-fw" :class="icon"></span> {{data.name}}
        </a>
        <!--if not folding, we do not need an binding event-->
        <a v-else href="javascript:" :title="data.name"><span class="fa fa-fw fa-circle-o"></span> {{data.name}}</a>
        <!--children-->
        <ul v-if="isFolder" :class="isShow">
            <list-category v-for="(data, index) in data.children" :key="index" :data="data" :search="search"></list-category>
        </ul>
    </li>
</template>
<script>
    export default {
        props: ['data', 'categoryId'],
        data() {
            return {
                open: false
            }
        },
        computed: {
            icon() {
                return {
                    'fa-plus': !this.open,
                    'fa-minus': this.open,
                }
            },
            isFolder() {
                return this.data.children && this.data.children.length
            },
            isShow() {
                return this.open ? 'show': 'hide'
            }
        },
        methods: {
            toggle() {
                this.open = !this.open
            }
        }
    }
</script>

如果数据 categories 中的 prop categoryId = id 类别,那么我希望该类别处于活动状态。

在我的代码中,如果代码被执行,我想像这样激活类别危险:

================================================ =============================

================================================ =============================

我该怎么做?

【问题讨论】:

    标签: javascript vue.js treeview vuejs2 vue-component


    【解决方案1】:

    实际上缺少两件事:

    • 您必须使用对象语法添加conditional class attribute (:class="{active: data.id === categoryId}"):

      <a v-else href="javascript:" :title="data.name" :class="{active: data.id === categoryId}"><span class="fa fa-fw fa-circle-o"></span> {{data.name}}</a>
      
    • 您必须将 categoryId 向下传递到组件树 (:category-id="categoryId"):

      <ul v-if="isFolder" :class="isShow">
          <list-category v-for="(data, index) in data.children" :key="index" :data="data" :search="search" :category-id="categoryId"></list-category>
      </ul>
      

    下面的演示。

    Vue.component('list-category', {
      template: "#lc",
      props: ['data', 'categoryId', 'search'],
      data() {
        return {
          open: false
        }
      },
      computed: {
        icon() {
          return {
            'fa-plus': !this.open,
            'fa-minus': this.open,
          }
        },
        isFolder() {
          return this.data.children && this.data.children.length
        },
        isShow() {
          return this.open ? 'show' : 'hide'
        }
      },
      methods: {
        toggle() {
          this.open = !this.open
        }
      }
    })
    
    new Vue({
      el: '#app',
      data() {
        return {
          categories: [{
              id: 1,
              name: 'England',
              children: [{
                  id: 3,
                  name: 'Chelsea',
                  children: [{
                      id: 7,
                      name: 'Hazard'
                    },
                    {
                      id: 8,
                      name: 'Morata'
                    }
                  ]
                },
                {
                  id: 4,
                  name: 'Manchester United',
                  children: [{
                      id: 9,
                      name: 'Pogba'
                    },
                    {
                      id: 10,
                      name: 'Lukaku'
                    }
                  ]
                }
              ]
            },
            {
              id: 2,
              name: 'Spain',
              children: [{
                  id: 5,
                  name: 'Real Madrid',
                  children: [{
                      id: 11,
                      name: 'Ronaldo'
                    },
                    {
                      id: 12,
                      name: 'Bale'
                    }
                  ]
                },
                {
                  id: 6,
                  name: 'Barcelona',
                  children: [{
                      id: 13,
                      name: 'Messi'
                    },
                    {
                      id: 14,
                      name: 'Suarez'
                    }
                  ]
                },
              ]
            }
          ],
          categoryId: 7
        }
      }
    })
    .active {
      background: yellow;
    }
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <div>
        <ul class="filter-category" v-for="list in categories">
          <list-category :data="list" :category-id="categoryId"></list-category>
        </ul>
      </div>
    </div>
    
    <template id="lc">
        <li>
            <!--parent-->
            <a v-if="isFolder" href="javascript:" @click="toggle">
                <span class="fa fa-fw" :class="icon"></span> {{data.name}}
            </a>
            <!--if not folding, we do not need an binding event-->
            <a v-else href="javascript:" :title="data.name" :class="{active: data.id === categoryId}"><span class="fa fa-fw fa-circle-o"></span> {{data.name}}</a>
            <!--children-->
            <ul v-if="isFolder" :class="isShow">
                <list-category v-for="(data, index) in data.children" :key="index" :data="data" :search="search" :category-id="categoryId"></list-category>
            </ul>
        </li>
    </template>

    【讨论】:

    • 如果我在我的情况下实施,它不起作用。未显示类别危险。结果如下:postimg.org/image/4yu2rbjvx。结果应该像我的问题中的图像
    • 为您提供这种风格(灰色背景)的是 CSS 类。您必须找到它的名称并将active 替换为其在:class="{active: data.id === categoryId}" 中的名称。例如。如果 CSS 类名是 selectedcategory,你应该使用 :class="{selectedcategory: data.id === categoryId}"
    • 类别危险激活。但是没有显示父类别。在我的问题中,父类别是否应该像我的图片一样显示
    • 因此,如果我在我的案例中实施,则类别危险处于活动状态。但是在执行脚本时,父类别(类别 england 和类别 chelsea)并没有直接打开。我必须先单击父类别。如果危险类别处于活动状态,则父类别直接打开
    • 在您发布的代码中,与打开/关闭类别无关。每个类别始终是开放的......看我发布的演示,与打开/关闭无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多