vuejs button click to next date - vuejs2

I want to print the date on the input according to the number of days of the button when the buttons are clicked with vue js.
For example: Clicking the 7 Day Button
I want "30.08.2021" to be written.
<template>
<div>
<div class="btn-group">
<button #click="getDate(1)" class="btn">TODAY</button>
<button #click="getDate(7)" class="btn">7 DAY</button>
<button #click="getDate(14)" class="btn">14 DAY</button>
<button #click="getDate(30)" class="btn">30 DAY</button>
<button #click="getDate(45)" class="btn">45 DAY</button>
<button #click="getDate(60)" class="btn">60 DAY</button>
</div>
<div>
<input v-model="data.date">
</div>
</div>
</template>
<script>
export default {
data() {
return {
data: {
date: "",
}
}
},
methods: {
getDate: function (day) {
var timeFrom = (day) => {
var dates = [];
for (let I = 0; I < Math.abs(day); I++) {
dates.push(new Date(new Date().getTime() - ((day >= 0 ? I : (I - I - I)) * 24 * 60 * 60 * 1000)).toLocaleString());
}
return dates;
}
return console.log(timeFrom(day));
},
}
}
</script>

Right now you are just creating an array of dates, you need to create one date and then use that value in your template.
<template>
<div>
<div class="btn-group">
<button #click="getDate(1)" class="btn">TODAY</button>
<button #click="getDate(7)" class="btn">7 DAY</button>
<button #click="getDate(14)" class="btn">14 DAY</button>
<button #click="getDate(30)" class="btn">30 DAY</button>
<button #click="getDate(45)" class="btn">45 DAY</button>
<button #click="getDate(60)" class="btn">60 DAY</button>
</div>
<div>
<input v-model="data.date" />
</div>
</div>
</template>
<script>
export default {
data() {
return {
data: {
date: "",
},
};
},
methods: {
getDate(days) {
const now = new Date();
const tmpDate = now.setDate(now.getDate() + days);
const newDate = new Date(tmpDate);
this.data.date = newDate.toDateString()
},
},
};
</script>

Related

vue js button next date vuetify date-picker

how to add button to change the date to next date with vuetify v-date-picker? i try to make button that would change the content to next date. the calendar
this doesn't work
`
<template>
<div>
<div class="btn-group">
<button #click="getDate(1)" class="btn">TODAY</button>
<button #click="getDate(7)" class="btn">7 DAY</button>
<button #click="getDate(14)" class="btn">14 DAY</button>
<button #click="getDate(30)" class="btn">30 DAY</button>
<button #click="getDate(45)" class="btn">45 DAY</button>
<button #click="getDate(60)" class="btn">60 DAY</button>
</div>
<div>
<input v-model="data.date">
</div>
</div>
</template>
`
`
<script>
export default {
data() {
return {
data: {
date: "",
}
}
},
methods: {
getDate: function (day) {
var timeFrom = (day) => {
var dates = [];
for (let I = 0; I < Math.abs(day); I++) {
dates.push(new Date(new Date().getTime() - ((day >= 0 ? I : (I - I - I)) * 24 * 60 * 60 * 1000)).toLocaleString());
}
return dates;
}
return console.log(timeFrom(day));
},
}
}
</script>
`

How to restrict user to enter only 30 characters in Vuejs?

<script>
export default {
name: "Register",
props: {
msg: String,
},
};
</script>
-------------main.js---------------
new Vue({
data:{
max:30,
text:''
},
render:h => h(App),
}).$mount('#app'
<template>
<div class="pop-up-mask">
{{ msg }}
<div class="pop-up">
<input type="text" class="input-section"
placeholder="Enter your Name" :maxlength="max" v-model="text" />
</div>
</template>
If the user tries to enter more than 30 characters, user should get an error message: you can only enter 30 characters. Try with above logic like maxlength="max" v-model="text"
I had done something similar in the past, so I built on that component (plus some research) to build this component that solves the problem.
<template>
<div class="input-max">
<div class="form-row">
<div class="col-md-8">
<input class="form-control" type="text" placeholder="Address"
v-model="address" #keyup="updateAddress">
</div>
<div class="col-md-4">
<span v-if="displayWarning" class="error-msg">* You can only enter {{ maxLength }} characters</span>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
address: '',
previousAddress: '',
maxLength: 30,
displayWarning: false
}
},
methods: {
updateAddress(event) {
let newValue = event.target.value;
if (newValue.length > this.maxLength) {
event.preventDefault()
this.address = this.previousAddress;
this.displayWarning = true;
}
else {
this.address = newValue;
this.previousAddress = newValue;
this.displayWarning = false;
}
}
}
}
</script>
<style scoped>
.error-msg {
color: red;
}
</style>

Dynamic v-model problem on nuxt application

I am trying to create a dynamic v-model input. All seems well except for the following.
The on click event that triggers the checkAnswers method only works if you click out of the input then click back into the input and then press the button again. It should trigger when the button is pressed the first time.
Does anyone have any ideas? Thanks in advance.
<template>
<div class="addition container">
<article class="tile is-child box">
<div class="questions">
<ul v-for="n in 5">
<li>
<p>{{ randomNumberA[n] }} + {{ randomNumberB[n] }} = </p>
<input class="input" type="text" maxlength="8" v-model.number="userAnswer[n]">
<p>{{ outcome[n] }}</p>
</li>
</ul>
</div>
<div class="button-container">
<button #click="checkAnswers" class="button">Submit Answer</button>
</div>
</article>
</div>
</template>
<script>
export default {
data() {
return {
randomNumberA: [] = Array.from({length: 40}, () => Math.floor(Math.random() * 10)),
randomNumberB: [] = Array.from({length: 40}, () => Math.floor(Math.random() * 10)),
userAnswer: [],
outcome: [],
}
},
methods: {
checkAnswers() {
for (var i = 0; i < 6; i++) {
if (this.userAnswer[i] === (this.randomNumberA[i] + this.randomNumberB[i])) {
this.outcome[i] = 'Correct';
} else {
this.outcome[i] = 'Incorrect';
}
}
}
}
}
</script>
You have some basic issues with your use of the template syntax here. According to the vue docs:
One restriction is that each binding can only contain one single
expression, so the following will NOT work: {{ var a = 1 }}
If you want to populate your arrays with random numbers you would be better calling a function on page mount. Something like this:
mounted() {
this.fillArrays()
},
methods: {
fillArrays() {
for (let i = 0; i < 5; i++) {
this.randomNumberA.push(Math.floor(Math.random() * 10))
this.randomNumberB.push(Math.floor(Math.random() * 10))
this.answer.push(this.randomNumberA[i] + this.randomNumberB[i])
}
}
}
Then you can use template syntax to display your arrays.
It looks like you are setting up a challenge for the user to compare answers so I think you'd be better to have a function called on input: Something like:
<input type="whatever" v-model="givenAnswer[n-1]"> <button #click="processAnswer(givenAnswer[n-1])>Submit</button>
Then have a function to compare answers.
Edit
I have basically rewritten your whole page. Basically you should be using array.push() to insert elements into an array. If you look at this you'll see the randomNumber and answer arrays are populated on page mount, the userAnswer array as it is entered and then the outcome on button click.
<template>
<div>
<div >
<ul v-for="n in 5">
<li>
<p>{{ randomNumberA[n-1] }} + {{ randomNumberB[n-1] }} = </p>
<input class="input" type="text" maxlength="8" v-model.number="userAnswer[n-1]">
<p>{{ outcome[n-1] }}</p>
</li>
</ul>
</div>
<div class="button-container">
<button #click="checkAnswers" class="button">Submit Answers</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
randomNumberA: [],
randomNumberB: [],
answer: [],
userAnswer: [],
outcome: [],
}
},
mounted() {
this.fillArrays()
},
methods: {
checkAnswers() {
this.outcome.length = 0
for (var i = 0; i < 6; i++) {
if (this.userAnswer[i] === this.answer[i]) {
this.outcome.push('Correct');
} else {
this.outcome.push('Incorrect');
}
}
},
fillArrays() {
for (let i = 0; i < 5; i++) {
this.randomNumberA.push(Math.floor(Math.random() * 10))
this.randomNumberB.push(Math.floor(Math.random() * 10))
this.answer.push(this.randomNumberA[i] + this.randomNumberB[i])
}
}
}
}
</script>

Pagination. How to make moving between pages by clicking on numerals

Tell me how to make it so that when you click on a button from a cycle with page numbers, this particular page opens. Switching along the arrows works for me, but I cannot understand how to switch between pages. I take data from Api. Total posts 98. It is possible to add your posts. On one page only 10 posts are shown.
My html:
<div id="app">
<div class="smallfon">
<div class="blocktwitter"><img src="src/assets/twitter.png" class="twitter"/></div>
<div class="addTextPost">Add a post</div>
<input type="text" v-model="createTitle" class="created"/>
<input type="text" v-model="createBody" class="created"/>
<div><button #click="addPost()" class="addPost">AddPost</button></div>
<div class="post1">
<div class="yourPosts">Your Posts</div>
<ul>
<li v-for="(post, index) of paginatedData" class="post">
<p><span class="boldText">Title:</span> {{ post.title }}</p>
<p><span class="boldText">Content:</span> {{ post.body }}</p>
<button #click="deleteData(index, post.id)" class="buttonDelete">Delete</button>
<button #click="visiblePostID = post.id" class="buttonChange">Change</button>
<div v-if="visiblePostID === post.id" class="modalWindow">
<div><input v-model="post.title" class="changePost"><input v-model="post.body" class="changePost"></div>
<button type="button" #click="changePost(post.id, post.title, post.body)" class="apply">To apply</button>
</div>
</li>
</ul>
<button type="button" #click="page -=1" v-if="page > 0" class="prev"><<</button>
<button class="item"
v-for="n in evenPosts"
:key="n.id"
v-bind:class="{'selected': current === n.id}">{{ n }} </button>
<button type="button" #click="page +=1" class="next" v-if="page < evenPosts-1">>></button>
</div>
</div>
</div>
My js:
export default {
el: "#app",
data () {
return {
current: null,
page: 0,
posts: [],
createTitle: '',
createBody: '',
visiblePostID: '',
}
},
watch: {
counter: function(newValue, oldValue) {
this.getData()
}
},
created(){
this.getData()
},
computed: {
evenPosts: function(posts){
return Math.ceil(this.posts.length/10);
},
paginatedData() {
const start = this.page * 10;
const end = start + 10;
return this.posts.slice(start, end);
}
},
methods: {
setCurrent: function(id) {
this.current = id;
},
getData() {
axios.get(`https://jsonplaceholder.typicode.com/posts`).then(response => {
this.posts = response.data
})
},
deleteData(index, id) {
axios.delete('http://jsonplaceholder.typicode.com/posts/' + id)
.then(response => {
console.log('delete')
this.posts.splice(index, 1);
})
.catch(function(error) {
console.log(error)
})
},
addPost() {
axios.post('http://jsonplaceholder.typicode.com/posts/', {
title: this.createTitle,
body: this.createBody
}).then((response) => {
this.posts.unshift(response.data)
})
},
changePost(id, title, body) {
axios.put('http://jsonplaceholder.typicode.com/posts/' + id, {
title: title,
body: body
})
},
}
}
Screenshot of application
add click event #click="page=n" in button
<button #click="page=n" class="item"
v-for="n in evenPosts"
:key="n.id"
v-bind:class="{'selected': current === n.id}">{{ n }} </button>
Codepen : https://codepen.io/anon/pen/bZOROO

incrementing and rendering text with vue.js

I am trying to do something pretty simple. Increment a number:
<div>
<button v-on:click="increase">Add 1</button>
<p>{{ sayHello() }} and {{ counter }}</p>
</div>
which will change an event here:
<b-form-input v-bind="renderText(counter)"
type="text"
placeholder="Type Alex"></b-form-input>
where this should just render whatever is in the text box.
<b-form-input v-bind="foo"
type="text"
placeholder="Enter your name"></b-form-input>
<p>Value: {{ foo }} and {{counter}}</p>
full code: but the only this that works in the sayHello() method.
<template>
<div>
<form>
<div>
<button v-on:click="increase">Add 1</button>
<p>{{ sayHello() }} and {{ counter }}</p>
</div>
<div>
<b-form-input v-bind="foo"
type="text"
placeholder="Enter your name"></b-form-input>
<b-form-input v-bind="renderText(counter)"
type="text" placeholder="Type Alex"></b-form-input>
<p>Value: {{ foo }} and {{counter}}</p>
</div>
</form>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
name: 'Hobbies',
methods: {
data:{
foo: '',
counter: 0
},
sayHello: function () {
return 'Alex'
},
increase: function () {
this.counter++;
},
renderText: function (event) {
if (event == 5){
return 10
} else{
return 16
}
}
}
});
</script>
<style scoped>
</style>
when putting data outside of methods I get:
You put data inside methods, it should be at the root level, since we're in a component it must also come as a function :
export default {
name: 'Hobbies',
data: function(){
return { foo: '', counter: 0 };
},
methods: {
sayHello: function () { return 'Alex' },
increase: function () { this.counter++; },
renderText: function (event) {
if (event == 5){
return 10
} else{
return 16
}
}
}
}