I would like to manage a case in js view. I have a list of employees who arrive in a department and also leave the department. My list returns me 3 objects (entries and exits) for the current week, the next week and the week after. By default, I display the entries and exits of the current week and I would like with two buttons previous and next to display the other weeks and return to the previous week with the previous button. It's possible to call a watch in vue JS
<template>
<div >
<ul>
<li v-for="employee in employees" :key="employee.id">
<div >
<p >{{ employee.name }}</p>
<p >{{ employee.dept }}</p>
</div>
</li>
</ul>
</div>
<div>
<button type="button" @click="selectemployee(employee)" >Previous</button>
<button type="button" @click="selectemployee(employee)" >Next</button>
</div>
</template>
<script>
export default {
props: {
employees: [
{
"inputs": [
{
"id": "38",
"name": "Marcus",
"dept": "Informatic Technology",
},
{
"id": "48",
"name": "Dimitri",
"dept": "Marketing",
}
],
"outputs": [
{
"id": "36",
"name": "John",
"dept": "Businnes",
},
{
"id": "82",
"name": "Greg",
"dept": "Marketing",
}
]
},
{
"inputs": [
{
"id": "50",
"name": "Emilie",
"dept": "Informatic Technology",
},
{
"id": "15",
"name": "Julian",
"dept": "Marketing",
}
],
"outputs": [
{
"id": "60",
"name": "Alan",
"dept": "Informatic Technology",
},
{
"id": "19",
"name": "Mireille",
"dept": "Marketing",
}
]
},
{
"inputs": [
{
"id": "71",
"name": "Dupond",
"dept": "Informatic Technology",
},
{
"id": "16",
"name": "Dufourd",
"dept": "Marketing",
}
],
"outputs": [
{
"id": "386",
"name": "Tata",
"dept": "Informatic Technology",
},
{
"id": "198",
"name": "Toto",
"dept": "Marketing",
}
]
}
]
},
methods: {
selectemployeePreviousOrNext(employee) {
}
}
}
</script>
<style lang="scss" scoped>
.employees-list {
flex: 2;
max-height: 100%;
height: 600px;
overflow: scroll;
border-left: 1px solid #a6a6a6;
ul {
list-style-type: none;
padding-left: 0;
li {
display: flex;
padding: 2px;
border-bottom: 1px solid #aaaaaa;
height: 80px;
position: relative;
cursor: pointer;
&.selected {
background: #dfdfdf;
}
span.unread {
background: #82e0a8;
color: #fff;
position: absolute;
right: 11px;
top: 20px;
display: flex;
font-weight: 700;
min-width: 20px;
justify-content: center;
align-items: center;
line-height: 20px;
font-size: 12px;
padding: 0 4px;
border-radius: 3px;
}
.employee {
flex: 3;
font-size: 10px;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: center;
p {
margin: 0;
&.name {
font-weight: bold;
}
}
}
}
}
}
How can I do it please?
CodePudding user response:
Just store the current week index and increase / decrease that index to change the week:
<template>
<div>
<div v-for="employee of weekEmployees">
{{ employee.name}}
</div>
<button @click="prevWeek">Prev</button>
<button @click="nextWeek">Next</button>
</div>
</template>
<script>
export default {
data () {
return {
week: 0,
employees: [], // your array of 3 weeks of employees
}
},
computed: {
weekEmployees() {
return this.employees[this.week]
}
},
methods: {
nextWeek() {
if (this.week >= this.employees.length - 1) { return }
this.week
},
prevWeek() {
if (this.week <= 0 ) { return }
this.week--
}
}
}
</script>
CodePudding user response:
If I understood you correctly, try like following snippet
new Vue({
el: "#demo",
props: {
employees: {
type:Array,
default: () => [
{"inputs": [{"id": "38", "name": "Marcus", "dept": "Informatic Technology",}, {"id": "48", "name": "Dimitri", "dept": "Marketing",}],
"outputs": [{"id": "36", "name": "John", "dept": "Businnes",}, {"id": "82", "name": "Greg", "dept": "Marketing",}]},
{"inputs": [{"id": "50", "name": "Emilie", "dept": "Informatic Technology",}, {"id": "15", "name": "Julian", "dept": "Marketing",}],
"outputs": [{"id": "60", "name": "Alan", "dept": "Informatic Technology",}, {"id": "19", "name": "Mireille", "dept": "Marketing",}]},
{"inputs": [{"id": "71", "name": "Dupond", "dept": "Informatic Technology",}, {"id": "16", "name": "Dufourd", "dept": "Marketing",}],
"outputs": [{"id": "386", "name": "Tata", "dept": "Informatic Technology",}, {"id": "198", "name": "Toto", "dept": "Marketing",}]}
]
}
},
data() {
return {
week: {id: 0, title: "current week"}
}
},
methods: {
employeesFilter(w) {
return this.employees[w]
},
selectemployee(w) {
this.week.id = w
this.week.id === 0 ? this.week.title = "current week" : this.week.id === 1 ? this.week.title = "next week" : this.week.title = "week after"
}
}
})
.employees-list {
flex: 2;
max-height: 100%;
height: 600px;
overflow: scroll;
border-left: 1px solid #a6a6a6;
}
.employees-list ul {
list-style-type: none;
padding-left: 0;
}
.employees-list ul li {
display: flex;
padding: 2px;
border-bottom: 1px solid #aaaaaa;
height: 80px;
position: relative;
cursor: pointer;
}
.employees-list ul li selected {
background: #dfdfdf;
}
.employee {
flex: 3;
font-size: 10px;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: center;
}
.employee p {
margin: 0;
}
.employee .name {
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div >
{{ week.title }}
<ul>
<li v-for="(employee, i) in employeesFilter(week.id)" :key="i">
{{ i }}
<ul>
<li v-for="empl in employee" :key="empl.id">
<div >
<p >{{ empl.name }}</p>
<p >{{ empl.dept }}</p>
</div>
</li>
</ul>
</li>
</ul>
</div>
<div>
<button type="button" v-show="week.id > 0" @click="selectemployee(-1)" >Previous</button>
<button type="button" v-show="week.id < 2" @click="selectemployee(1)" >Next</button>
</div>
</div>