Vue Bootstrap - lazy content load on Collapse - vue.js

I am using multiple Collapse in the page and want the content to appear only when it is active as it is in Tabs (Lazy loading tab content).
<b-button v-b-toggle.collapse-1 variant="primary">Toggle Collapse</b-button>
<b-collapse id="collapse-1" class="mt-2">
<b-card>
<p class="card-text">Collapse contents Here</p>
</b-card>
</b-collapse>
<b-button v-b-toggle.collapse-2 variant="primary">Toggle Collapse 2</b-button>
<b-collapse id="collapse-2" class="mt-2">
<b-card>
<p class="card-text">Collapse contents Here</p>
</b-card>
</b-collapse>

You can use the #show and #hidden events to toggle a boolean property, which you can then bind to a v-if on your collapse's content.
This way the content will only be rendered in the DOM when the collapse is open.
new Vue({
el: '#app',
data() {
return {
isCollapseOpenOne: false,
isCollapseOpenTwo: false
}
}
})
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue#2.21.2/dist/bootstrap-vue.min.css" />
<script src="https://unpkg.com/vue#2.6.2/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue#2.21.2/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-button v-b-toggle.collapse-1 variant="primary">Toggle Collapse</b-button>
<b-button v-b-toggle.collapse-2 variant="primary">Toggle Collapse 2</b-button>
<b-collapse id="collapse-1" class="mt-2" #show="isCollapseOpenOne = true" #hidden="isCollapseOpenOne = false">
<b-card v-if="isCollapseOpenOne">
<p class="card-text">Collapse contents Here</p>
</b-card>
</b-collapse>
<b-collapse id="collapse-2" class="mt-2" #show="isCollapseOpenTwo = true" #hidden="isCollapseOpenTwo = false">
<b-card v-if="isCollapseOpenTwo">
<p class="card-text">Collapse contents Here</p>
</b-card>
</b-collapse>
</div>

Related

vue js transition between two buttons

I am trying to swap between two buttons using vue js transition. I need to exchange the position of two accordions like this-- adode xd example
I have done this part. But how do I swap the buttons using vue transitions?
<html>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0-beta1/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0-
beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-
pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous">
</script>
<div id="app">
<div id="t_accordion">
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header " id="headingOne">
<button class="d-flex bg-white mx-auto">
<a #click="leaveScreen = true" class="click-me">User</a>
<a href="#" class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-
bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>
</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<button class="d-flex bg-white mx-auto">
<a #click="toggleTheme">View</a>
<a href="" class="accordion-button " type="button" data-bs-toggle="collapse" data-bs-
target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo"></a>
</button>
</h2>
</div>
<div>
<div id="collapseOne" class="accordion-collapse collapse " aria-labelledby="headingOne" data-
bs-parent="#accordionExample">
<div class="accordion-body">
<strong>User Content</strong>
</div>
</div>
<div id="collapseTwo" class="accordion-collapse collapse show" aria-labelledby="headingTwo"
data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>View Content</strong>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Don't forget to include Vue from CDN! -->
<script src="https://unpkg.com/vue#2"></script>
<script>
new Vue({
el: '#app', //Tells Vue to render in HTML element with id "app"
data() {
return {
message: 'Hello World!'
}
},
methods:{
toggleTheme(){
// alert('clicked')
this.isActive = !this.isActive;
}
}
});
</script>
</html>
I am new to vue js. If anyone knows about vue transition animations plase help me to solve the problem

bootstrap-vue navbar doesn't uncollapse in mobile when a button is clicked on the navbar

I've used bootstrap-vue to implement a navbar on my vue.js app. When the elements are clicked the navbar uncollapses as expected. I've replaced one of the navitem with a so I can make it stand out and give it it's own CSS, but when the button is clicked the navigation works correctly but menu stays collapsed. Is there a way to force it to uncollapse?
<template>
<b-navbar toggleable="lg" type="light">
<b-navbar-brand to="/"> <img src="static/img/logo.svg" width="220px"></b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item to="/about-us">About Us</b-nav-item>
<b-nav-item to="/landlords">Landlords</b-nav-item>
<b-nav-item to="#">Tenants</b-nav-item>
<b-button class=" " to="/search">Valuation</b-button>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</template>
Styling the native b-nav-item
The easiest solution would be to stick with b-nav-item and apply your classes to the item using either the class attribute, to add a class to the li, or the link-classes prop to add it to the rendered a tag.
This way you let Bootstrap-Vue handle the collapse close, making it more future proof in case something changes in the future.
new Vue({
el: '#app'
})
<link href="https://unpkg.com/bootstrap#4.5.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue#2.15.0/dist/bootstrap-vue.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue#2.15.0/dist/bootstrap-vue.js"></script>
<div id="app">
<b-navbar toggleable="lg" type="light">
<b-navbar-brand to="#">
LOGO
</b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item to="#">About Us</b-nav-item>
<b-nav-item to="#">Landlords</b-nav-item>
<b-nav-item to="#">Tenants</b-nav-item>
<b-nav-item to="#" link-classes="btn btn-primary text-white">
Valuation
</b-nav-item>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</div>
Closing the collapse manually
Another option is to close the collapse by adding a click handler to your custom element in the navbar, that closes the collapse if open.
new Vue({
el: '#app',
data() {
return {
isCollapseOpen: false
}
},
methods: {
closeNavCollapse() {
if(this.isCollapseOpen) {
this.$root.$emit('bv::toggle::collapse', 'nav-collapse')
}
}
}
})
<link href="https://unpkg.com/bootstrap#4.5.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue#2.15.0/dist/bootstrap-vue.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue#2.15.0/dist/bootstrap-vue.js"></script>
<div id="app">
<b-navbar toggleable="lg" type="light">
<b-navbar-brand to="/">LOGO</b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse v-model="isCollapseOpen" id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item to="#">About Us</b-nav-item>
<b-nav-item to="#">Landlords</b-nav-item>
<b-nav-item to="#">Tenants</b-nav-item>
<b-button to="#" variant="primary" #click="closeNavCollapse">
Valuation
</b-button>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</div>
Using a watcher to automatically detect route changes.
Alternatively you can use a watcher and detect when the route changes and manually close the collapse.

Vue bootstrap-vue accordion wont open

I am new to vue and really struggle to get my dynamically created accordion to expand when clicked on.
I think its something basic I am missing but I have been stuck for a while.
This codepen show my troubles, I cant figure it out?
<template>
<div role="tablist">
<b-card v-for="(item, index) in feature_info" no-body class="mb-1">
<b-card-header header-tag="header" class="p-1" role="tab">
<b-button block href="#" v-bind:v-b-toggle="'accordion-' + index" variant="info">[[ item.layerName ]]</b-button>
</b-card-header>
<b-collapse v-bind:id="'accordion-' + index" accordion="my-accordion" role="tabpanel">
<b-card-body>
<b-card-text>Mock text</b-card-text>
</b-card-body>
</b-collapse>
</b-card>
</div>
</template>
Link to codepen
Your problem is the v-bind:v-b-toggle="'accordion-' + index".
v-b-toggle is a directive and you therefor don't need to use v-bind on it. So if you just remove v-bind: from it, it should work.
new Vue({
el: '#app',
delimiters: ['[[',']]'],
data: {
message:"Hello",
feature_info:[
{layerName: 'Hello Vue!'},
{layerName: 'Second'}
],
}
})
<link href="https://unpkg.com/bootstrap#4.4.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/bootstrap-vue#2.5.0/dist/bootstrap-vue.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue#2.5.0/dist/bootstrap-vue.js"></script>
<div id="app">
<div role="tablist">
<b-card v-for="(item, index) in feature_info" no-body class="mb-1">
<b-card-header header-tag="header" class="p-1" role="tab">
<b-button block href="#" v-b-toggle="'accordion-' + index" variant="info">[[ item.layerName ]]</b-button>
</b-card-header>
<b-collapse v-bind:id="'accordion-' + index" accordion="my-accordion" role="tabpanel">
<b-card-body>
<b-card-text>Mock text</b-card-text>
</b-card-body>
</b-collapse>
</b-card>
</div>
</div>

scroll between Nav and components Vue

I have created a vue single page this way:
My app.vue
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<div id="app">
<Navegacion/>
<Cabecera/>
<Contenido/>
<Aside/>
<Footer/>
</div>
</template>
My navigation.vue
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<div class="flex flex-row blanco">
<div class="item-uno">
<nav class="flex flex-row items-center">
<a #click="desplazar" href="#">Quienes somos</a>
<a #click="desplazar" href="#">Cómo funciona</a>
<a #click="desplazar" href="#">Servicios a empresas</a>
<a #click="desplazar" href="#">Compromiso</a>
</nav>
</div>
<div class="item-dos flex justify-center">
<img id="logo" src="../assets/logo.svg" alt="logo apeteat">
</div>
<div class="item-uno flex justify-center items-center">
<a class="btn blue">Nuevo pedido</a>
<div class="flex flex-row items-center">
<i class="fas fa-shopping-bag"></i>
<p>0,00 €</p>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Navegacion',
props: {
},
methods: {
desplazar: function() {
window.scrollTo(0,200);
}
}
}
</script>
I want to make scroll to the correspondent component when clicking on a navigation link.
I have installed npm scroll-to, and I have tried to add a function methd desplazar but it's not working.
Thanks in advance for your help.
window.scrollTo() is javascript window method, you don't need to install anything to use it.
It works in your code but href="#" always throw you at the top of the page so it's cancelling window.scrollTo().
You need to remove href="#" from your links.
To scroll to component first you need to add ref to it like this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<div id="app">
<Navegacion ref="Navegacion" />
<Cabecera ref="Cabecera" />
<Contenido ref="Contenido" />
<Aside ref="Aside" />
<Footer ref="Footer" />
</div>
</template>
and your function should looks like this
scrollToNavegacion() {
window.scrollTo(0, this.$refs.Navegacion.$el.offsetTop);
}

The modal window is not displayed

I am trying to show a modal window as in this example. https://codepen.io/anon/pen/dgRKEP?editors=1010
But for some reason, the modal window is not shown.
<div id="app6">
<div class="has-text-centered">
<button class="button is-success" #click="showModal = true">
show modal!
</button>
</div>
<modal title="Title of the modal" v-show="showModal" #close="showModal = false">
<div class="content">
is modal!
</div>
</modal>
</div>
Script:
Vue.component('modal', {
template: `
<div class="modal is-active">
<div class="modal-background"></div>
<div class="modal-content">
<div class="box"><slot></slot></div>
</div>
<button class="modal-close" #click="$emit('close')"></button>
</div>
`
});
new Vue({
el: '#app6',
data:{
...
showModal: true
...
},
I'm assuming you're using Bulma. So you need to include their stylesheet for things to work correctly. The simplest way is to just use a CDN. So you need to include this line in your head tag or somewhere before your app div.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
Like this for example ..
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
<div id="app">
<div id="app" class="container">
<div class="has-text-centered">
<button class="button is-success" #click="showModal = true">
Покажите мне всплывашечку!
</button>
</div>
<modal title="Title of the modal" v-show="showModal" #close="showModal = false">
<div class="content">
</div>
</modal>
</div>
</div>
See this JSFiddle
You need to add the css. you can add your own css or use the library that used in the code pen example: https://cdnjs.cloudflare.com/ajax/libs/bulma/0.2.3/css/bulma.min.css. Clicking the css setting button you will find the imported library.