本文实例为大家分享了vue实现图书管理系统的具体代码,供大家参考,具体内容如下
组件代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
< template >
< div id = "app" >
< div class = "grid" >
< div >
< h1 >图书管理</ h1 >
< div class = "book" >
< div >
< label for = "id" v-focus>
编号:
</ label >
< input type = "text" id = "id" v-model = "id" :disabled = "flag" >
< label for = "name" >
名称:
</ label >
< input type = "text" id = "name" v-model = "name" >
< button @ click = "add(addOrUpdate)" :disabled = "subFlag" >提交</ button >
</ div >
</ div >
</ div >
< div class = "total" >
< span >图书总数:</ span >
< span >{{ totalNum }}</ span >
</ div >
< table >
< thead >
< tr >
< th >编号</ th >
< th >名称</ th >
< th >时间</ th >
< th >操作</ th >
</ tr >
</ thead >
< tbody >
< tr v-for = "book in books" >
< td > {{ book.id }} </ td >
< td > {{ book.name }} </ td >
< td > {{ book.date | date-format }} </ td >
< td >
< a href = "" @ click.prevent = " rel=" external nofollow" updateBook(book.id)">修改</ a >
< span >|</ span >
< a href = "" @ click.prevent = 'deleteBook(book.id)' >删除</ a >
</ td >
</ tr >
</ tbody >
</ table >
</ div >
</ div >
</ template >
< script >
export default {
data(){
return{
books:[
{
id: 1,
name: '三国演义',
date: 2525609975000
},
{
id: 2,
name: '水浒传',
date: 2525609975000
},
{
id: 3,
name: '红楼梦',
date: 2525609975000
},
{
id: 4,
name: '西游记',
date: 2525609975000
}
],
id:'',
name:'',
flag:false, // id输入框是否可修改标识
addOrUpdate: 0, // 0代表添加 1代表修改
subFlag:false, // 提交按钮是否禁用(图书存在时禁用)
}
},
methods:{
// 添加图书的方法
add() {
if(this.addOrUpdate===0){
// 添加图书
this.books.push({
id: this.id,
name: this.name,
date: new Date()
});
}else{
const book = this.books.filter((book)=>{
return book.id === this.id;
});
book[0].name = this.name
}
// 添加之后清空input框
this.id = '';
this.name = '';
this.flag = false
},
// 更新图书的方法
updateBook(id){
this.addOrUpdate = 1;
// 需要修改的当前图书信息
const book = this.books.filter((book)=>{
return book.id === id;
});
// 让input框显示相应内容
this.id = book[0].id;
this.name = book[0].name;
this.flag = true;
},
deleteBook(id){
// 获取当前图书的索引
const index = this.books.findIndex((book)=>{
return book.id === id
});
this.books.splice(index, 1)
}
},
computed:{
totalNum(){
return this.books.length
}
},
// 自定义局部指令
directives:{
focus:{
inserted(el){
// 自动聚焦
el.focus()
}
}
},
// 监视图书是否存在
watch:{
name:{
deep:true,
handler(val){
const book = this.books.find((book)=>{
return book.name === val
});
if(book){
this.subFlag = true
}else{
this.subFlag = false
}
}
}
}
}
</ script >
< style type = "text/css" >
.grid {
margin: auto;
width: 530px;
text-align: center;
}
.grid table {
border-top: 1px solid #C2D89A;
width: 100%;
border-collapse: collapse;
}
.grid th,td {
padding: 10px;
border: 1px dashed #F3DCAB;
height: 35px;
line-height: 35px;
}
.grid th {
background-color: #F3DCAB;
}
.grid .book {
padding-bottom: 10px;
padding-top: 5px;
background-color: #F3DCAB;
}
.grid .total {
height: 30px;
line-height: 30px;
background-color: #F3DCAB;
border-top: 1px solid #C2D89A;
}
</ style >
|
过滤器文件index.js
1
2
3
4
5
6
7
|
import Vue from 'vue'
import format from 'date-fns/format'
// 自定义过滤器
Vue.filter( 'date-format' , function (value, formatStr= 'yyyy-MM-dd HH:mm:ss' ) {
return format(value, formatStr)
});
|
main.js引入
1
|
import './filters'
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/gklcsdn/article/details/111132301