MyFooter.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div class="todo-footer" v-show="total">
  3. <label>
  4. <!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
  5. <input type="checkbox" v-model="isAll"/>
  6. </label>
  7. <span> <span>已完成{{doneTotal}}</span> / 全部{{ total }} </span>
  8. <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: "MyFooter",
  14. props: ['todos'],
  15. computed:{
  16. //任务总数
  17. total(){
  18. return this.todos.length
  19. },
  20. //已完成任务数
  21. doneTotal(){
  22. // const x = this.todos.reduce((pre,current)=>{
  23. // console.log('@@',pre)
  24. // return pre + (current.done ? 1 : 0)
  25. // },0)
  26. // console.log('###',x)
  27. // 代码简化
  28. return this.todos.reduce((pre,todo)=>pre + (todo.done ? 1 : 0),0)
  29. },
  30. // reduce里面的函数会调用三次,第一次调用的时候,pre的初始值是reduce的第二个参数(本例中为0)
  31. //从第二次开始,pre的值都是上一次调用函数的返回值
  32. //current是每一个元素对象
  33. // isAll(){
  34. // return this.doneTotal === this.total && this.total > 0
  35. // }
  36. //是否全选
  37. isAll:{
  38. get(){
  39. return this.doneTotal === this.total && this.total > 0
  40. },
  41. set(value){
  42. // this.checkAllTodo(value)
  43. this.$emit('checkAllTodo',value)
  44. }
  45. }
  46. },
  47. methods:{
  48. // checkAll(e){
  49. // // console.log(e.target.checked)
  50. // this.checkAllTodo(e.target.checked)
  51. // },
  52. //清除已完成的任务
  53. clearAll(){
  54. this.$emit('cleatAllTodo')
  55. }
  56. }
  57. };
  58. </script>
  59. <style scoped>
  60. /*footer*/
  61. .todo-footer {
  62. height: 40px;
  63. line-height: 40px;
  64. padding-left: 6px;
  65. margin-top: 5px;
  66. }
  67. .todo-footer label {
  68. display: inline-block;
  69. margin-right: 20px;
  70. cursor: pointer;
  71. }
  72. .todo-footer label input {
  73. position: relative;
  74. top: -1px;
  75. vertical-align: middle;
  76. margin-right: 5px;
  77. }
  78. .todo-footer button {
  79. float: right;
  80. margin-top: 5px;
  81. }
  82. </style>