I want a function to be called when a button is clicked to change the selected text that is in a v-container, how can I do that?
I will be glad to any information
<template>
<div class="text">
<v-btn
#click="editText"
>
</v-btn>
<v-textarea
v-model="text"
></v-textarea>
</div>
</template>
<script>
export default {
name: "ArticleForm",
data() {
return {
text: 'some text',
}
},
methods: {
editText() {
... how?
}
}
}
</script>
<style scoped>
</style>
You can use window.getSelection for the selected text. Here is the demo
SFC:
<template>
<v-app>
<v-content>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore facere
aspernatur explicabo voluptatibus? Non temporibus, dicta illum
perferendis error voluptatum. Rerum officiis debitis voluptas earum
nulla consequatur necessitatibus tenetur eius.
</div>
<v-btn class="my-5" #click="editText">Edit</v-btn>
<v-textarea v-model="text"></v-textarea>
</v-content>
</v-app>
</template>
<script>
export default {
name: "App",
data() {
return {
text: "",
};
},
methods: {
editText() {
if (window.getSelection) this.text = window.getSelection().toString();
},
},
};
</script>
Related
I am doing an npm timeline Vue component. But I am facing problems.
parent component A:
<script>
export default Vue.extend({
name: "aComponent",
})
</script>
<template>
<div class="events-content">
<ol>
<slot name="eventsContent"></slot>
</ol>
</div>
</template>
children component B:
<aComponent>
<template v-slot:eventsContent>
<li class="selected" ref="event-1">
<p>
orem ipsum dolor sit amet, consectetur adipisicing elit. Illum
praesentium officia, fugit recusandae ipsa, quia velit nulla
adipisci? Con
</p>
</li>
<li>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illum
praesentium officia, fugit recusandae ipsa, quia velit nulla
adipisci? Con
</p>
</li>
</template>
</aComponent>
Now I want to modify the style of li of Children component B in the method of parent component A. That is why I tried to use ref="event-1" to access in parent component A, but it doesn't work. I don't want to add too much code in children component B, because it is going to be npm package. So how can I modify each li style from children component B in parent component A
The parent component can style the slotted children with its own <style> block:
<!-- ParentComponent.vue -->
<template>
<aComponent>
<template v-slot:eventsContent>
<li class="selected">
⋮
</li>
<li>
⋮
</li>
</template>
</aComponent>
</template>
<style>
li {
color: #444;
}
.selected {
font-weight: bold;
color: blue;
}
</style>
demo
I'm trying to add a button that changes color when clicked and changes isCompleted between true and false, what am I doing wrong? I have tried to change 'checkCompleted' to 'checkCompleted(task)' but it still gives the same error
(extra text so stackoverflow allows me post the question, they keep saying too much code and not enough text)
<template>
<div class="container tasks">
<div class="card" v-for="(task, index) in tasks" :key="index">
<div class="card-content">
<div class="card-title">
<h4>{{ task.title }}</h4>
</div>
<p>{{ task.description }}</p>
<button
class="btn-small green"
#click="checkCompleted"
v-if="!isCompleted"
>
Completed
</button>
<div class="btn- red" v-else>Incomplete</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
tasks: [
{
title: "Read Books",
description:
"Lorem ipsum dolor sit amet consectetur, adipisicing elit. Alias accusamus iste asperiores excepturi tempore unde.",
isCompleted: false,
},
{
title: "Wash Plates",
description:
"Lorem ipsum dolor sit amet consectetur, adipisicing elit. Alias accusamus iste asperiores excepturi tempore unde.",
isCompleted: false,
},
{
title: "Play Fifa",
description:
"Lorem ipsum dolor sit amet consectetur, adipisicing elit. Alias accusamus iste asperiores excepturi tempore unde.",
isCompleted: false,
},
{
title: "Go Gym",
description:
"Lorem ipsum dolor sit amet consectetur, adipisicing elit. Alias accusamus iste asperiores excepturi tempore unde.",
isCompleted: false,
},
],
};
},
methods: {
checkCompleted() {
this.isCompleted = this.isCompleted ? true : false;
},
},
};
</script>
You're looping and want to reference the isCompleted from each task so
v-if="!isCompleted"
should be
v-if="!task.isCompleted"
I am still very new to the Vue.js and Vuetify frameworks. I am currently trying to create a front page element with a css property of height:100% in Vuetify, but for some reason I am unable to fill the page with the first row element.
As seen below, I am using the "fullscreen" Vuetify property in the v-row element.
Thanks!
<template>
<div class="home">
<v-row class="grey fullscreen" align="center" justify="space-around">
<v-col xs=12 md=5>
<h1 class="main-title">Clever and<br> Practical Designs<span class="redDot">.</span></h1>
<h3 class="sub-title">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quo exercitationem repellat, officia consequuntur obcaecati voluptate cum voluptas placeat dolores temporibus vel ex saepe reprehenderit porro asperiores cumque distinctio accusantium optio.</h3>
<v-row>
<v-btn href="" class="button white--text pa-4">
View our Products
</v-btn>
</v-row>
</v-col>
<v-col xs=12 md=5>
<v-img contain class="homeimg" max-width="500px" src="../assets/Large_product.png"></v-img>
</v-col>
</v-row>
</div>
</template>
I found a solution:
Adding a style element in the App.vue file to get the fullscreen class to function properly :
<template>
<v-app>
<Navbar />
<v-content>
<router-view></router-view>
</v-content>
</v-app>
</template>
<script>
import Navbar from '#/components/Navbar'
export default {
name: 'App',
components: { Navbar },
data: () => ({
//
}),
};
</script>
<style lang="scss">
.fullscreen {
width: 100%;
height: 100vh;
}
</style>
I get some code like in the below from coreui vue template,
<b-modal title="Modal title" class="modal-success" v-model="successModal" #ok="successModal = false" ok-variant="success">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</b-modal>`
And the result is like in the below:
https://i.stack.imgur.com/8qPLJ.png
the question:
How I can edit the two buttons (cancel and OK) in the footer of that modal?
I know.
this is to use a slot,
you can put the slot for footer like in the below code
<div slot="modal-footer" class="w-100">
<p class="float-left">Modal Footer Content</p>
<b-btn size="sm" class="float-right" variant="primary" #click="show=false">
Close
</b-btn>
</div>
For anyone that's stumbling upon this question, here's is another answer.
<CModal title="Delete scraper?" :show.sync="dangerModal" color="danger">
By deleting this scraper, you will delete all the jobs and results that
are related to this scraper.<br><br>
<span class="font-weight-bold">Are you sure you would want to delete this scraper?</span>
<div slot="footer" class="w-100">
<CButton style="border-radius: .2rem; color: white;" color="danger" class="ml-1 mr-1 float-right" #click="dangerModal = true">
<i class="fas fa-trash"></i>
</CButton>
<CButton style="border-radius: .2rem; color: white;" color="danger" class="ml-1 mr-1 float-right" #click="dangerModal = true">
<i class="fas fa-trash"></i>
</CButton>
</div>
</CModal>
By adding a div element with the slot name.
OR
You can do this, which is way cleaner and easier:
<CModal title="Delete scraper?" :show.sync="dangerModal" color="danger">
By deleting this scraper, you will delete all the jobs and results that
are related to this scraper.<br><br>
<span class="font-weight-bold">Are you sure you would want to delete this scraper?</span>
<template #footer>
<CButton #click="dangerModal = false" color="danger">Discard</CButton>
<CButton #click="dangerModal = false" color="success">Accept</CButton>
</template>
</CModal>
Just use template element with the hashtag of the slot you want to use. In this case, I did #footer, to change the footer of the modal.
The slot names are listed in the official CoreUI Vue documentation here.
Remove button through hide-footer, add the button the way you want it.
on the button we use the float-right class to direct the buttons to the right.
Example:
<template>
<div>
<b-button #click="showModal">
Open Modal
</b-button>
<b-modal ref="myModalRef" hide-footer title="Using Component Methods">
<div class="d-block text-center">
<h3>Alteration</h3>
</div>
<b-btn class="float-right" #click="hideModal">Test</b-btn>
</b-modal>
</div>
</template>
<script>
export default {
methods: {
showModal () {
this.$refs.myModalRef.show()
},
hideModal () {
this.$refs.myModalRef.hide()
}
}
}
</script>
I'm trying to create a sidebar which can slide-in and -out by clicking on a trigger element. The trigger-element should be sticked to the sidebar.
However, in my current fiddle, the triggler-element only changes position after the sidebar completed its transition.
How can I accomplish the expected behavior?
new Vue({
el: "#app",
data: {
exposed: true
}
})
.sidebar {
width: 200px;
height: 200px;
}
.toggler {
height: 30px;
}
/**********************
* Transition classes *
**********************/
.slide-leave-active,
.slide-enter-active {
transition: 1s;
}
.slide-enter {
transform: translate(-200px, 0);
}
.slide-leave-to {
transform: translate(-200px, 0);
}
<script src="https://vuejs.org/js/vue.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="app" class="px-0 py-4">
<div class="d-flex">
<!-- Sidebar content -->
<transition name="slide">
<div v-if="exposed" class="sidebar bg-dark text-white p-1">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>
</transition>
<!-- Sidebar toggler -->
<div class="toggler px-2 bg-secondary">
<a href="#" v-on:click="exposed = !exposed" class="text-white">
<span v-if="exposed">[X]</span>
<span v-else>(?)</span>
</a>
</div>
</div>
</div>
jsfiddle
I made minor css change as below
.slide-enter {
margin-left:-200px;
}
.slide-enter-to {
margin-left:0px;
}
.slide-leave-to {
margin-left:-200px;
}
Please check if it works.
https://jsfiddle.net/z43xug9n/3/
If you want to slide both the sidebar and the toggle at the same time, it is better to slide their parent (container).
https://jsfiddle.net/4mju0rzt/31/
<div id="app">
<div v-bind:class="{'minimized': hide}" class="sidebar-container">
<div class="sidebar bg-dark text-white p-1">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>
<!-- Sidebar toggler -->
<div class="toggler px-2 bg-secondary">
<a href="#" v-on:click="hide = !hide" class="text-white">
<span v-if="!hide">[X]</span>
<span v-else>(?)</span>
</a>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.17/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
hide: false
}
})
</script>
Found a solution with Velocity.js. The trick is to move the sidebar away instead of removing it from the DOM.
new Vue({
el: "#app",
data: {
exposed: true
},
methods: {
slideIn (el, done) {
Velocity(el, {
marginLeft: 0
}, {
duration: 500
}, {
complete: done
});
},
slideOut (el, done) {
Velocity(el, {
marginLeft: -200
}, {
duration: 500
}, {
complete: done
});
}
}
})
.sidebar {
width: 200px;
height: 200px;
}
.toggler {
height: 30px;
width: 36px;
}
<script src="https://vuejs.org/js/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/2.0.3/velocity.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="app" class="px-0 py-4">
<div class="d-flex">
<!-- Sidebar content -->
<transition v-on:enter="slideIn" v-on:leave="slideOut">
<div v-show="exposed" id="sidebar" class="sidebar bg-dark text-white p-1">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>
</transition>
<!-- Sidebar toggler -->
<div class="toggler px-2 bg-secondary">
<a href="#" v-on:click="exposed = !exposed" class="text-white">
<span v-if="exposed">[X]</span>
<span v-else>(?)</span>
</a>
</div>
</div>
</div>