<
meta
http-equiv
=
"
content-type
"
content
=
"
text/html; charset=UTF-8
"
>
<
link
rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
https://unpkg.com/todomvc-app-css@2.2.0/index.css
"
>
<
script
type
=
"
text/javascript
"
src
=
"
https://unpkg.com/vue@latest/dist/vue.js
"
>
</
script
>
<
title
>
Title
</
title
>
<
style
>
[v-cloak]
{
display
:
none
;
</
style
>
</
head
>
<
section
class
=
"
todoapp
"
>
<
header
class
=
"
header
"
>
<
h1
>
todos
</
h1
>
<
input
autofocus
autocomplete
=
"
off
"
placeholder
=
"
What needs to be done?
"
class
=
"
new-todo
"
v-model
=
"
newTodo
"
@keyup.enter
=
"
addTodo
"
>
</
header
>
<
section
class
=
"
main
"
v-show
=
"
todos.length
"
v-cloak
>
<
input
id
=
"
toggle-all
"
type
=
"
checkbox
"
class
=
"
toggle-all
"
>
<
ul
class
=
"
todo-list
"
>
<
li
v-for
=
"
todo in filteredTodos
"
class
=
"
todo
"
:key
=
"
todo.id
"
:class
=
"
{completed: todo.completed,editing: todo == editedTodo}
"
>
<
div
class
=
"
view
"
>
<
input
class
=
"
toggle
"
type
=
"
checkbox
"
v-model
=
"
todo.completed
"
>
<
label
@dblclick
=
"
editTodo(todo)
"
>
{
{todo.title}}
</
label
>
<
button
class
=
"
destroy
"
@click
=
"
removeTodo(todo)
"
>
</
button
>
</
div
>
<
input
class
=
"
edit
"
type
=
"
text
"
v-model
=
"
todo.title
"
v-todo-focus
=
"
todo == editedTodo
"
@blur
=
"
doneEdit(todo)
"
@keyup.enter
=
"
doneEdit(todo)
"
@key.esc
=
"
cancelEdit(todo)
"
>
</
section
>
<
footer
class
=
"
footer
"
v-show
=
"
todos.length
"
v-cloak
>
<
span
class
=
"
todo-count
"
>
<
strong
>
{
{remaining}}
</
strong
>
{
{remaining | pluralize}} left
</
span
>
<
ul
class
=
"
filters
"
>
<
li
>
<
a
href
=
"
#/all
"
:class
=
"
{selected: visibility ==
'
all
'
}
"
>
All
</
a
>
</
li
>
<
li
>
<
a
href
=
"
#/active
"
:class
=
"
{selected: visibility ==
'
active
'
}
"
>
Active
</
a
>
</
li
>
<
li
>
<
a
href
=
"
#/completed
"
class
=
"
{selected: visibility ==
'
completed
'
}
"
>
Completed
</
a
>
</
li
>
<
button
class
=
"
clear-completed
"
@click
=
"
removeCompleted
"
v-show
=
"
todos.length > remaining
"
>
Clear completed
</
button
>
</
footer
>
</
section
>
<
script
>
let
STORAGE_KEY
=
"todos-vuejs-2.0"
;
let
todoStorage
=
{
fetch
:
function
(
)
{
let
todos
=
JSON
.
parse
(
localStorage
.
getItem
(
STORAGE_KEY
)
||
"[]"
)
;
todos
.
forEach
(
function
(
todo
,
index
)
{
todo
.
id
=
index
;
}
)
;
todoStorage
.
uid
=
todos
.
length
;
return
todos
;
save
:
function
(
todos
)
{
localStorage
.
setItem
(
STORAGE_KEY
,
JSON
.
stringify
(
todos
)
)
;
let
filters
=
{
all
:
function
(
todos
)
{
return
todos
;
active
:
function
(
todos
)
{
return
todos
.
filter
(
function
(
todo
)
{
return
!
todo
.
completed
;
}
)
;
completed
:
function
(
todos
)
{
return
todos
.
filter
(
function
(
todo
)
{
return
todo
.
completed
;
}
)
;
let
app
=
new
Vue
(
{
data
:
{
todos
:
todoStorage
.
fetch
(
)
,
newTodo
:
""
,
editedTodo
:
null
,
visibility
:
"all"
watch
:
{
todos
:
{
handler
:
function
(
todos
)
{
todoStorage
.
save
(
todos
)
;
deep
:
true
computed
:
{
filteredTodos
:
function
(
)
{
return
filters
[
this
.
visibility
]
(
this
.
todos
)
;
remaining
:
function
(
)
{
return
filters
.
active
(
this
.
todos
)
.
length
;
allDone
:
{
get
:
function
(
)
{
return
this
.
remaining
===
0
;
set
:
function
(
value
)
{
this
.
todos
.
forEach
(
function
(
todo
)
{
todo
.
completed
=
value
;
}
)
;
filters
:
{
pluralize
:
function
(
n
)
{
return
n
===
1
?
"item"
:
"items"
;
methods
:
{
addTodo
:
function
(
)
{
let
value
=
this
.
newTodo
&&
this
.
newTodo
.
trim
(
)
;
if
(
!
value
)
{
return
;
this
.
todos
.
push
(
{
id
:
todoStorage
.
uid
++
,
title
:
value
,
completed
:
false
}
)
;
this
.
newTodo
=
""
;
removeTodo
:
function
(
todo
)
{
this
.
todos
.
splice
(
this
.
todos
.
indexOf
(
todo
)
,
1
)
;
editTodo
:
function
(
todo
)
{
this
.
beforeEditCache
=
todo
.
title
;
this
.
editedTodo
=
todo
;
doneEdit
:
function
(
todo
)
{
if
(
!
this
.
editedTodo
)
{
return
;
this
.
editedTodo
=
null
;
todo
.
title
=
todo
.
title
.
trim
(
)
;
if
(
!
todo
.
title
)
{
this
.
removeTodo
(
todo
)
;
cancelEdit
:
function
(
todo
)
{
this
.
editedTodo
=
null
;
todo
.
title
=
this
.
beforeEditCache
;
removeCompleted
:
function
(
)
{
this
.
todos
=
filters
.
active
(
this
.
todos
)
;
directives
:
{
"todo-focus"
:
function
(
el
,
binding
)
{
if
(
binding
.
value
)
{
el
.
focus
(
)
;
}
)
;
function
onHashChange
(
)
{
let
visibility
=
window
.
location
.
hash
.
replace
(
/#\/?/
,
""
)
;
if
(
filters
[
visibility
]
)
{
app
.
visibility
=
visibility
;
}
else
{
window
.
location
.
hash
=
""
;
app
.
visibility
=
"all"
;
window
.
addEventListener
(
"hashchange"
,
onHashChange
)
;
onHashChange
(
)
;
app
.
$mount
(
".todoapp"
)
;
</
script
>
<
script
crossorigin
=
"
"
type
=
"
text/javascript
"
src
=
"
https://codesandbox.io/static/js/watermark-button.76f1e5b66.js
"
>
</
script
>
</
body
>
</
html
>
运行效果:
在文本框中输入内容及回车:
点击左侧圆点选项:
下面三个标签all、active、completed功能也比较清楚,active只显示没有被标记的项,completed只显示标记的项,即完成项,all则显示所有项。clear completed删除掉所有的完成项。
并且,页面支持浏览器缓存功能,使用同一种浏览器添加的数据,下次重新打开数据依然在,如果切换浏览器,如先使用firefox添加数据,再通过Google打开界面,是没有数据显示的。
2.单击
待办
项的前面选择框可
实现
未完成或已完成切换
3.双击
待办
内容可以修改里面的内容,修改完成后点击空白处或enter键都可
实现
修改,如果不想修改按Esc键可恢复初始内容。
4.单击
待办
1、在输入框中输入
待办
事项
并按回车键可以把内容添加到下面列表中,并且
待办
事项
总数会随之增加
2.、点击
待办
事项
列表中"√"可以将事件添加到已完成事件中, 点击“×”按钮,该按钮可以删除该
事项
3.、已完成
事项
中可以删除事件
整个项目的结构:
实现
的效果图:
身为互联网农民工的我们,提起 Todo List 大家肯定一点都不陌生,因为Todo List是一个圈内比较知名的案例,可以通过 Java, Python 等后端语言再以少量的前端
Html
语言辅助
实现
整个 Todo List 的增、删、改、查功能。
今天分享的 Todo List 案例与常见的
实现
方式不太一样,因为今天分享的案例是由纯前端代码
Vue
组件化来
实现
的,完全没有后端语言的支撑,也能
实现
Todo List功能的动态效果。
Vue.js
是什么
这是
Vue
官网对
Vue
的介绍
每个人都需要做一个
待办
事项
清单
,对吧? 这是一个处理多个列表的单页应用程序,并且(在 mColor 的帮助下)使用漂亮的渐变显示。
当前数据使用
HTML
5 本地存储进行持久化,但可以轻松扩展以将数据保存到后端数据库或您网站上更永久的内容。
matt[at]mattlag[dot]com
To change this template use File | Settings | File Templates.
<%@ page contentType="text/
html
;charset=UTF-8"
文章目录0 使用技术1 项目搭建1.1 创建项目1.2 搭建项目结构1.2.1 准备数据1.2.2 组件化开发1.2.3 引入element-ui2 开发模块2.1 Header模块2.2 Input模块2.3 List模块2.4 Item模块3 项目文件4 优化4.1 条件渲染4.1.1 旧版渲染4.1.2 新版渲染4.2 按需更新4.2.1 利用生命周期4.2.2 利用纯组件5 用户体验
0 使用技术
axios 请求数据
json-server 模拟数据
react 操作dom
element-ui