-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
184 lines (168 loc) · 5.96 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<title>Document</title>
<style>
#app {
width: 600px;
margin: 10px auto;
}
.tb {
border-collapse: collapse;
width: 100%;
}
.tb th {
background-color: #0094ff;
color: white;
}
.tb td,
.tb th {
padding: 5px;
border: 1px solid black;
text-align: center;
}
.add {
padding: 5px;
border: 1px solid black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="app">
<brand-manager></brand-manager>
</div>
</body>
</html>
<script type="text/html" id="templateId">
<!-- 组件模板应该有根目录的存在 -->
<div>
<!-- //组件模板 -->
<div class="add">
编号:
<input type="text" v-model="id">
品牌名称:
<input type="text" @keyup.enter="add" v-model="name">
<input type="button" @click="add"value="添加" >
</div>
<div class="add">
品牌名称:
<input type="text" @keyup.enter="search" v-model="keyword" placeholder="请输入搜索条件">
</div>
<table class="tb">
<tr>
<th>编号</th>
<th>品牌名称</th>
<th>创立时间</th>
<th>操作</th>
</tr>
<!---->
<tr v-if="brandlist.length === 0">
<!--没有内容的时候显示没有数据-->
<td colspan="4">没有数据!</td>
</tr>
<tr v-for="(item,index) in brandlist" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.time | formateDate}}</td>
<td><a href="javascript:void(0)" @click = "delList(index)" >删除</a></td>
</tr>
</table>
</div>
</script>
<script>
//自定义注册组件
Vue.component("brand-manager", {
template: `#templateId`,
//data必须使一个函数,因为ia一个项目中会有多个组件,
data() {
return {
brandlist: [{
id: 1,
name: "LV",
time: new Date()
},
{
id: 2,
name: "GUCCI",
time: new Date()
}
],
oldBrandList: [],
id: "",
name: "",
keyword: ""
}
},
//组件创建的同时其中的方法也会被执行
created() {
this.oldBrandList = this.brandlist;
},
filters: {
formateDate(input, operator = '-') {
var date = new Date(input)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minus = date.getMinutes();
var second = date.getSeconds();
return `${year}${month} ${day} ${hour}:${minus}:${second}`
}
},
methods: {
add() {
this.brandlist.push({
id: this.id,
name: this.name,
time: new Date()
})
//新增完成以后将新数组保存起来;
this.oldBrandList = this.brandlist;
},
delList(index) {
//删除对应的列表值;
this.brandlist.splice(index, 1);
//删除数据以后,将删除数组重新保存,
this.oldBrandList = this.brandlist;
},
search() {
//如果输入框为空,则显示全部的数据;
if (this.keyword.length === 0) {
this.brandlist = this.oldBrandList;
return
}
//array.filter(function(currentValue,index,arr), thisValue)
//brand 为遍历的每一个数组的value的值,在这里为么一个数据对象;arr为原数组对象
var newBrandList = this.brandlist.filter(function (currentValue, index, arr) {
return currentValue.name.includes(this.keyword)
},this);
console.log(this.keyword);
this.brandlist = newBrandList;
}
},
watch:{
keyword(newValue,oldValue){
//如果输入框为空,则显示全部的数据;
if (this.keyword.length === 0) {
this.brandlist = this.oldBrandList;
return
}
//array.filter(function(currentValue,index,arr), thisValue)
//brand 为遍历的每一个数组的value的值,在这里为么一个数据对象;arr为原数组对象
var newBrandList = this.brandlist.filter(function (currentValue, index, arr) {
return currentValue.name.includes(this.keyword)
},this);//在作为函数回调的时候应添加this,
this.brandlist = newBrandList;
}
}
//搜索数组,中的数据;
});
var app = new Vue({
el: "#app",
});
</script>