MyItem.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <li>
  3. <label>
  4. <input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)" />
  5. <span v-show="!todo.isEdit">{{ todo.title }}</span>
  6. <input
  7. type="text"
  8. v-show="todo.isEdit"
  9. :value="todo.title"
  10. @blur="handleBlur(todo,$event)"
  11. ref="inputTitle"
  12. />
  13. </label>
  14. <button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
  15. <button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>
  16. </li>
  17. </template>
  18. <script>
  19. import pubsub from 'pubsub-js'
  20. export default {
  21. name: "MyItem",
  22. // 声明接收todo对象
  23. props: ['todo'],
  24. methods: {
  25. //勾选或取消勾选
  26. handleCheck(id){
  27. //console.log(id)
  28. //通知App组件将对应的todo对象的done值取反
  29. // this.checkTodo(id)
  30. this.$bus.$emit('checkTodo',id)
  31. },
  32. //删除
  33. handleDelete(id){
  34. //console.log(id)
  35. if(confirm('确定删除吗?')){
  36. // console.log(id)
  37. // this.deleteTodo(id)
  38. // this.$bus.$emit('deleteTodo',id)
  39. pubsub.publish('deleteTodo',id)
  40. }
  41. },
  42. // 编辑
  43. handleEdit(todo){
  44. if(todo.hasOwnProperty('isEdit')){
  45. todo.isEdit = true
  46. }else{
  47. console.log('@')
  48. this.$set(todo,'isEdit',true)
  49. }
  50. //nextTick指定的回调会在DOM更新完毕之后再执行
  51. this.$nextTick(function(){
  52. this.$refs.inputTitle.focus()
  53. })
  54. },
  55. // 失去焦点回调(真正执行修改逻辑)
  56. handleBlur(todo,e){
  57. todo.isEdit = false
  58. if(!e.target.value.trim()) return alert('输入不能为空')
  59. this.$bus.$emit('updateTodo',todo.id,e.target.value)
  60. }
  61. },
  62. };
  63. </script>
  64. <style scoped>
  65. /*item*/
  66. li {
  67. list-style: none;
  68. height: 36px;
  69. line-height: 36px;
  70. padding: 0 5px;
  71. border-bottom: 1px solid #ddd;
  72. }
  73. li label {
  74. float: left;
  75. cursor: pointer;
  76. }
  77. li label li input {
  78. vertical-align: middle;
  79. margin-right: 6px;
  80. position: relative;
  81. top: -1px;
  82. }
  83. li button {
  84. float: right;
  85. display: none;
  86. margin-top: 3px;
  87. }
  88. li:before {
  89. content: initial;
  90. }
  91. li:last-child {
  92. border-bottom: none;
  93. }
  94. li:hover{
  95. background-color: #ddd;
  96. }
  97. li:hover button{
  98. display: block;
  99. }
  100. </style>