TodoList製作

實現基本結構

  • 若下載好資料後可至 basic > todo.html 開始進行修改

  • 建立 資料陣列(todos) 與 暫時資料變數(newtodo)

<script>
    var app = new Vue({
        el: '#app',
        data: {
            newTodo: '',
            todos: [],
        },
        methods:{},
        computed:{}
    });
</script>
  • 在 input 使用 v-model 得到使用者輸入字串

  • 在 列表li 建立 v-for循環結構

實現增加資料

  • 增加 methods addTodo 用來增加資料

  • 將 addTodo 放入 button(@click="addTodo") 與 input( @keyup.enter) 方便加入列表資料

  • 將 放入label標籤內顯示資料名稱

  • 將 :for="item.id" 與 :id="item.id" 放入label 與 input 方便辨識各個資料

實現刪除資料

  • 在下面 button(x鍵) 加入 removeTodo 方法並傳入 item 指定刪除此資料

  • 建立 removeTodo 方法 來刪除列表資料

    • 傳入 item 並找出在todos對應的正確id

    • 利用找出的 id 刪除 item

實現刪除線

  • 增加 css

  • 於input checkbox內增加 v-model="item.completed"(當按下時變為true)

  • 於label 增加 :class="{'completed':item.completed}" 來進行切換

實現過濾資料

  • 增加預設全部顯示 : 在 data 內新增 visibility='all'

  • 於連結增加 :class="{'active': visibility =='對應的參數'}" @click="visibility='對應的參數'"

  • 增加 computed 計算函數 : filteredTodos來回傳顯示的陣列資料

  • 將 v-for 的 todos 改成 filteredTodos 來顯示過濾後資料 即可進行過濾

實現雙擊修改標題功能

  • 新增預設變數 cacheTodo,cacheTile 存取更改資料

  • 新增修改函數 editTodo 存取使用者欲更改資料

  • 在 li 新增雙擊事件 @dblclick="editTodo(item)

  • 新增 列表資料 與 input輸入框 互相切換顯示 : v-if="item.id !== cacheTodo.id" 與 v-if="item.id === cacheTodo.id"

  • 在 input 內增加 v-model="cacheTitle" 來讀取使用者修改資料

  • 在 input 內增加 esc離開編輯,enter完成編輯 事件

實現自動計算未完成筆數

  • 在 computed 新增 totall 函數

  • {{totall}} 放入 <span> 之中即可

完成後程式碼

Last updated

Was this helpful?