Задать вопрос
Ответы пользователя по тегу Vue.js
  • Как сделать часть пунктов списка скрытыми и разворачивать при клике?

    drop9dead
    @drop9dead
    Примерно так:
    <div id="app">
            <ul>
                <li v-for="item in list">
                    {{item}}
                </li>
            </ul>
            <button @click="changeCount" v-if="showButton">{{ buttonText }}</button> 
        </div>
    
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    
        <script>
            new Vue({
                el: '#app',
                data: {
                    showItemCount: 4,
                    isOpen: false,
                    array: [
                        'Первый', 'Второй', 'Третий', 'Четвертый', 'Пятый', 'Шестой', 'Седьмой'
                    ]
                },
                computed: {
                    list() {
                        return this.array.slice(0, this.showItemCount)
                    },
                    showButton() {
                        return this.array.length > 4
                    },
                    buttonText() {
                        return this.isOpen ? 'Скрыть' : 'Подробнее'
                    }
                },
                methods: {
                    changeCount() {
                        if(this.isOpen) {
                            this.showItemCount = 4
                        } else {
                            this.showItemCount = this.array.length
                        }
                        this.isOpen = !this.isOpen
                    }
                }
            })
        </script>
    Ответ написан
    Комментировать