简体   繁体   English

当我在 VueJS 中单击下一个或上一个按钮时显示一个列表

[英]Display a list when i click next or previous button in VueJS

I would like to manage a case in js view.我想在 js 视图中管理一个案例。 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.我的列表为我返回了本周、下周和下周的 3 个对象(入口和出口)。 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可以在Vue JS中调用手表

<template>
    <div class="employee-list">
        <ul>
            <li v-for="employee in employees" :key="employee.id">
                <div class="employee">
                    <p class="name">{{ employee.name }}</p>
                    <p class="dept">{{ employee.dept }}</p>
                </div>
            </li>
        </ul>
    </div>
    <div>
        <button type="button" @click="selectemployee(employee)" class="btn btn-primary btn-lg">Previous</button>
        <button type="button" @click="selectemployee(employee)" class="btn btn-secondary btn-lg">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?请问我该怎么做?

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; } p, ul { 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 class="employee-list"> <p>{{ week.title }}</p> <ul> <li v-for="(employee, i) in employeesFilter(week.id)" :key="i"> <p>{{ i }}-{{ employee.length }}</p> <ul v-if="employee.length"> <li v-for="empl in employee" :key="empl.id"> <div class="employee"> <p class="name">{{ empl.name }}</p> <p class="dept">{{ empl.dept }}</p> </div> </li> </ul> <p v-else>no people this week</p> </li> </ul> </div> <div> <button type="button" v-show="week.id > 0" @click="selectemployee(-1)" class="btn btn-primary btn-lg">Previous</button> <button type="button" v-show="week.id < 2" @click="selectemployee(1)" class="btn btn-secondary btn-lg">Next</button> </div> </div>

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击卡片以在带有下一个和上一个按钮的弹出窗口上显示其各自的详细信息 - Click on the card to display their respective details on a popup with next and previous button 单击下一个/上一个按钮时,如何将活动类更改为LI元素? - How do I change the active class to the LI element when I click next/previous button? 当我单击下一个或上一个按钮时,数据表显示旧值。 - datatable is displaying old value when i click next or previous button. Vuejs单击打开按钮时如何显示选择列表 - Vuejs How can I show select-list when I click on open button 当我点击下一个按钮时,上一个问题必须用反向 animation 删除,下一个问题必须显示输入效果 animation - When i click on next button, the previous question must be remove with reverse animation & next question must be show with typing effect animation 单击 slider 下一个和上一个按钮我得到 $curr[action] is not a function - On click slider next and previous button i get $curr[action] is not a function 当我单击按钮时加载下一行 - Load next row when i click the button 当使用 javascript 同时单击下一个按钮或上一个按钮时,激活下一个 li 或前一个 li 元素 - when click on next button or previous button at the same time active next li or provious li element by using javascript 当我将鼠标悬停在div的左侧/右侧时,如何显示上一个/下一个按钮(使用CSS / jQuery) - When I hover left/right side of a div how do I display previous/next button (using CSS/jQuery) 当我单击下一个/上一个按钮时,jQuery DatePicker消失 - jQuery DatePicker disappears when i click Next/Previous buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM