Integrating strike-through the list on its click or on selecting checkbox in to-do-list app? - vuejs2

I am creating a demo of todolist app and I want to strike-through the list on click of list or selecting particular checkbox. Here is my code:
<div v-for="(item, index) in toDoList" :key="index">
<v-checkbox v-model="item.status"></v-checkbox>
<textfield>{{item.title}}</textfield>
</div>
where status is having a boolean value.

If you want your <textfield> to be responding to click events, a small change to your markup is required:
<v-checkbox
:label="item.title"
v-model="item.status"
></v-checkbox>
And then you can add this additional style to your stylesheet to target the <label> element of a checked checkbox:
.input-group--active label {
text-decoration: line-through;
}
new Vue({
el: '#app',
data: {
toDoList: [
{ title: 'Lorem', status: null },
{ title: 'ipsum', status: null },
{ title: 'dolor', status: null },
{ title: 'sit', status: null },
{ title: 'amet', status: null }
]
}
});
.input-group--active label {
text-decoration: line-through;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<!-- Vuetify dependencies -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.0.14/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.0.14/vuetify.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<div id="app">
<div
v-for="(item, index) in toDoList"
:key="index">
<v-checkbox
:label="item.title"
v-model="item.status"
></v-checkbox>
</div>
</div>
An alternative solution is simply adding a custom class to your checked elements using :class binding, e.g.:
<div
v-for="(item, index) in toDoList"
:key="index"
:bind="{ 'is-selected': item.status' }">
And for your CSS:
.is-selected label {
text-decoration: line-through;
}
Note that since text-decoration is not inheritable, using .is-selected { text-decoration: line-through; } will not work. You will have to select for the descendant <label> element.
new Vue({
el: '#app',
data: {
toDoList: [
{ title: 'Lorem', status: null },
{ title: 'ipsum', status: null },
{ title: 'dolor', status: null },
{ title: 'sit', status: null },
{ title: 'amet', status: null }
]
}
});
.is-selected label {
text-decoration: line-through;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<!-- Vuetify dependencies -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.0.14/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.0.14/vuetify.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<div id="app">
<div
v-for="(item, index) in toDoList"
:key="index"
:class="{ 'is-selected': item.status }">
<v-checkbox
:label="item.title"
v-model="item.status"
></v-checkbox>
</div>
</div>

Related

nuxtjs add and remove class on click on elements

I am new in vue and nuxt and here is my code I need to update
<template>
<div class="dashContent">
<div class="dashContent_item dashContent_item--active">
<p class="dashContent_text">123</p>
</div>
<div class="dashContent_item">
<p class="dashContent_text">456</p>
</div>
<div class="dashContent_item">
<p class="dashContent_text">789</p>
</div>
</div>
</template>
<style lang="scss">
.dashContent {
&_item {
display: flex;
align-items: center;
}
&_text {
color: #8e8f93;
font-size: 14px;
}
}
.dashContent_item--active {
.dashContent_text{
color:#fff;
font-size: 14px;
}
}
</style>
I tried something like this:
<div #click="onClick">
methods: {
onClick () {
document.body.classList.toggle('dashContent_item--active');
},
},
but it changed all elements and I need style change only on element I clicked and remove when click on another
also this code add active class to body not to element I clicked
This is how to get a togglable list of fruits, with a specific class tied to each one of them.
<template>
<section>
<div v-for="(fruit, index) in fruits" :key="fruit.id" #click="toggleEat(index)">
<span :class="{ 'was-eaten': fruit.eaten }">{{ fruit.name }}</span>
</div>
</section>
</template>
<script>
export default {
name: 'ToggleFruits',
data() {
return {
fruits: [
{ id: 1, name: 'banana', eaten: false },
{ id: 2, name: 'apple', eaten: true },
{ id: 3, name: 'watermelon', eaten: false },
],
}
},
methods: {
toggleEat(clickedFruitIndex) {
this.fruits = this.fruits.map((fruit) => ({
...fruit,
eaten: false,
}))
return this.$set(this.fruits, clickedFruitIndex, {
...this.fruits[clickedFruitIndex],
eaten: true,
})
},
},
}
</script>
<style scoped>
.was-eaten {
color: hsl(24, 81.7%, 49.2%);
}
</style>
In Vue2, we need to use this.$set otherwise, the changed element in a specific position of the array will not be detected. More info available in the official documentation.

How remove active class from router-link when :to property get false value?

I use router-link in vue for menu.
I want make the link be enabled when the routeName is specificName1 or specificName2.
I use a if clause in :to property and make links diabled and enabeld.
But when :to property is false, however is disabled, but has the active class.
How can I make it be disabled but with disabled class?
Vue.component('customIcon', {
template: `<svg xmlns="http://www.w3.org/2000/svg" width="15.352" height="15.355" viewBox="0 0 15.352 15.355">
<path id="Union_19" data-name="Union 19" d="M-19.5-15958.5l-7.5,7.5,7.5-7.5-7.5-7.5,7.5,7.5,7.5-7.5-7.5,7.5,7.5,7.5Z" transform="translate(27.176 15966.178)" fill="none" stroke="#bbb" stroke-width="0.5"/>
</svg>`
})
new Vue({
el: "#app",
data() {
return {
routeName: 'specificName1',
menu: [
{to: 'link1', name: 'name1'},
{to: 'link2', name: 'name2'},
{to: 'link3', name: 'name3'}
]
}
}
});
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap#4.5.3/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue#2.21.2/dist/bootstrap-vue.css" />
<script src="https://unpkg.com/vue#2.6.12/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue#2.21.2/dist/bootstrap-vue.min.js"></script>
<div id="app">
<div>
<ul>
<router-link
v-for="{ to, name } in menu"
v-slot="{ href, navigate, isActive, isExactActive }"
:key="name"
:to="(routeName === 'specificName1' || routeName === 'specificName2') && to"
>
<li :class="['nav-item', isActive && 'active', isExactActive && 'exact-active']">
<a
v-t="name"
:href="href"
#click="navigate"
/>
</li>
</router-link>
</ul>
</div>
</div>
when you are in the same URL of router-link to the property, the element class will be active.
for example if you are in search page, following element class will be active:
<router-link to="/search"></router-link>
I came up with a solution. Based on this issue, vue-router doesn't provide the disabled attribute, so you can use CSS to do the trick.
I tried to simulate your situation because I couldn't see the result of your snippet. If it needs any update please let me know.
Vue.use(VueRouter)
const Name1 = {
template: `<h1>Name1</h1>`,
}
const Name2 = {
template: `<h1>Name2</h1>`,
}
const Name3 = {
template: `<h1>Name3</h1>`,
}
const specificName1 = {
template: `<h1>specificName1</h1>`,
}
const routes = [{
path: '/name1',
name: 'name1',
component: Name1
},
{
path: '/name2',
name: 'name2',
component: Name2
},
{
path: '/name3',
name: 'name3',
component: Name3
},
{
path: '/specific',
name: 'specificName1',
component: specificName1
},
]
const router = new VueRouter({
routes,
})
var app = new Vue({
el: '#app',
router,
data() {
return {
routeName: 'specificName1',
menu: [{
to: 'link1',
name: 'name1'
},
{
to: 'link2',
name: 'name2'
},
{
to: 'link3',
name: 'name3'
},
{
to: 'specific',
name: 'specificName1'
}
]
}
},
})
a {
color: black;
text-decoration: none;
padding: 1rem;
}
a.router-link-active {
font-weight: bold;
color: green;
text-decoration: underline;
}
.disabled {
pointer-events: none;
}
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-router"></script>
<!-- App -->
<div id="app">
<nav>
<router-link v-for="{ to, name } in menu" :key="name" :class="{disabled: (name === 'specificName1' || name === 'specificName2')}" :to="{name: name}">{{name}}</router-link>
</nav>
<router-view></router-view>
</div>

How to use v-model for both form and populate the dropdown

I want to do two tasks using a v-select which is having dynamic items to load.
1. want to bind item-text with form to insert.
2. want to get item-id while select the option.
My code :
<v-select
:items="titles"
item-text="ocmtitle"
item-value="id"
v-model="ocmform.title"
label="Ocm Title"
#change="getOcmsubtitles()"
outline
></v-select>
You should use multi-select for v-model the element. Here is an example.
new Vue({
components: {
Multiselect: window.VueMultiselect.default
},
data: {
value: '',
options: [{
name: 'Vue.js',
language: 'JavaScript'
},
{
name: 'Rails',
language: 'Ruby'
},
{
name: 'Sinatra',
language: 'Ruby'
},
{
name: 'Laravel',
language: 'PHP',
$isDisabled: true
}
]
},
methods: {}
}).$mount('#app')
* {
font-family: 'Lato', 'Avenir', sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<script src="https://unpkg.com/vue-multiselect#2.1.0"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-multiselect#2.1.0/dist/vue-multiselect.min.css">
<div id="app">
<multiselect v-model="value" :options="options" placeholder="Pick some" label="name" track-by="name" id="example">
</multiselect>
<pre class="language-json"><code>{{ value }}</code></pre>
</div>

How to display multiple labels in vue-select field using Vue Select Library

I am using vue-select component from Vue Select Library in my html form as shown below and I want to display three value in the label but don't know how to achieve that. i couldn't found any solution in the documentation.
I want to display three value in the label as shown below.
<v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" label="item_name+'::'+item_code+'::'+item_created_at" v-model="item.selected_item" #input="getSelectedItem" style="width: 100%; height:56px;" />
HTML:
<script src="{{ asset('assets/requiredjs/vue-select.js') }}"></script>
<link href="{{ asset('assets/requiredcss/vue-select.css') }}" rel="stylesheet">
<v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" label="item_name" v-model="item.selected_item" #input="getSelectedItem" style="width: 100%; height:56px;" />
JS:
Vue.component('v-select', VueSelect.VueSelect);
var app = new Vue({
el: '#app',
data: {
formfieldsdata: {
items: [],
},
item: {
selected_item: 0,
},
}
});
Ref to vue select library documentation: https://vue-select.org/guide/values.html#transforming-selections
Just use template literals, what allow embed expressions in JavaScript strings. And make the label binded :label
<v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" :label="`${item_name} ${item_code} ${item_created_at}" v-model="item.selected_item`" #input="getSelectedItem" style="width: 100%; height:56px;" />
Update
label can use only for one object property. But you can use scopes for options and selected values. Example on codepen
<v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" v-model="item.selected_item" #input="getSelectedItem" style="width: 100%; height:56px;" >
<template slot="option" slot-scope="option">
<span :class="option.icon"></span>
{{ option.item_name }} {{option.item_code}} {{option.created_at}}
</template>
<template slot="selected-option" slot-scope="option">
<span :class="option.icon"></span>
{{ option.item_name }} {{option.item_code}} {{option.created_at}}
</template>
</v-select>
Update 2
Multi properties search vue-select
vue-component
<div id="app">
<h1>Vue Select - Multiple properties</h1>
<v-select :options="options" label="item_data"
v-model="selected">
</v-select>
</div>
vue-code
Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
options: [
{
title: 'Read the Docs',
icon: 'fa-book',
url: 'https://codeclimate.com/github/sagalbot/vue-select'
},
{
title: 'View on GitHub',
icon: 'fa-github',
url: 'https://codeclimate.com/github/sagalbot/vue-select'
},
{
title: 'View on NPM',
icon: 'fa-database',
url: 'https://codeclimate.com/github/sagalbot/vue-select'
},
{
title: 'View Codepen Examples',
icon: 'fa-pencil',
url: 'https://codeclimate.com/github/sagalbot/vue-select'
}
]
}
})

How can I inject css class in Vuejs component with javascript?

Sorry for my question. I actually want to inject new css class and its hover attributes which I got from a web service. I'm trying to put it into #style#
#/style# tags.
The official docs for Class and Style Bindings/Binding HTML Classes show several ways. Below are the most common and some useful demos.
Object Syntax
We can pass an object to v-bind:class to dynamically toggle classes:
<div v-bind:class="{ active: isActive }"></div>
The above syntax means the presence of the active class will be
determined by the truthiness of the data property isActive.
See demo below:
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
isActive: true
}
})
.active { background: yellow; }
<script src="https://unpkg.com/vue"></script>
<div id="app">
<p>{{ message }}</p>
<div v-bind:class="{ active: isActive }">My DIV</div>
<button #click="isActive = !isActive">activate/deactivate</button>
</div>
Multiple classes
You can have multiple classes toggled by having more fields in the
object. In addition, the v-bind:class directive can also co-exist with
the plain class attribute. So given the following template:
<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>
Demo:
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
isActive: true,
hasError: false
}
})
.active { background: yellow }
.text-danger { color: red; font-weight: bold; }
<script src="https://unpkg.com/vue"></script>
<div id="app">
<p>{{ message }}</p>
<div v-bind:class="{ active: isActive, 'text-danger': hasError }">My DIV</div>
<button #click="isActive = !isActive">toggle isActive</button>
<button #click="hasError= !hasError">toggle hasError</button>
</div>
Array Syntax
We can pass an array to v-bind:class to apply a list of classes:
<div v-bind:class="[activeClass, errorClass]"></div>
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
And, of course, you can change that data and the HTML will react. Another demo:
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
activeClass: 'active',
errorClass: 'text-danger'
}
})
.active { background: green }
.inactive { background: yellow }
.text-danger { color: red; font-weight: bold; }
<script src="https://unpkg.com/vue"></script>
<div id="app">
<p>{{ message }}</p>
<div v-bind:class="[activeClass, errorClass]">My DIV</div>
<button #click="activeClass = 'inactive'">activeClass to inactive</button>
<button #click="activeClass = 'active'">activeClass to active</button>
</div>