我是靠谱客的博主 勤劳斑马,这篇文章主要介绍Vue.js实现可编辑的表格,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Vue.js实现可编辑的表格的具体代码,供大家参考,具体内容如下

复制代码
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
202
<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="bootstrap.min.css" > <style type="text/css"> table tr td{ text-align: center; } .btn-info{ margin-left: 5px; } .add,.addBox{ margin: 10px 0px 10px 10px; } .submit{ margin-left: 172px; } /*全删*/ .delAll{ margin-left: 10px; } /*新增*/ fieldset{ margin-left: 10px; } </style> </head> <body> <div id="app"> <button class="btn btn-primary btn-sm addBox" @click="addBox">添加</button> <button class="btn btn-sm btn-danger delAll" @click="delAll">批量删除</button> <table class="table table-bordered" > <thead> <tr> <td><input type="checkbox" @click="allSelect" v-model="checked"></td> <td>学号</td> <td>姓名</td> <td>年纪</td> <td>操作</td> </tr> </thead> <tbody> <tr v-for="person,index in people"> <td><input type="checkbox" v-model="selected" v-bind:value="person.sid"></td> <td @click="edit(index)" contenteditable="true">{{person.sid}}</td> <td @click="edit(index)" contenteditable="true">{{person.sname}}</td> <td @click="edit(index)" contenteditable="true">{{person.sage}}</td> <td><button @click="del(index)" class="btn btn-danger btn-sm">删除</button><button @click="update(index)" class="btn btn-info btn-sm">编辑</button></td> </tr> </tbody> </table> <fieldset v-show="seen" > <legend>新增用户</legend> <div class=""> <p> <label>学号:</label> <input type="text" v-model="newPeople.sid"> </p> <p> <label>姓名:</label> <input type="text" v-model="newPeople.sname"> </p> <p> <label>年龄:</label> <input type="number" v-model="newPeople.sage"> </p> <p> <button class="btn btn-success btn-sm submit" @click="add">提交</button> </p> </div> </fieldset> <!-- 编辑界面 --> <fieldset v-show="editSeen"> <legend>编辑用户</legend> <div class=""> <p> <label>学号:</label> <input type="text" v-model="editPeople.sid" value="{{sid}}"> </p> <p> <label>姓名:</label> <input type="text" v-model="editPeople.sname" value="{{sname}}"> </p> <p> <label>年龄:</label> <input type="number" v-model="editPeople.sage" value="{{sage}}"> </p> <p> <button class="btn btn-success btn-sm submit" @click="editSubmit">提交</button> </p> </div> </fieldset> </div> <script type="text/javascript" src="vue.min.js"></script> <script type="text/javascript"> var data ={ people:[ {'sid':'1043','sname':'张三','sage':18}, {'sid':'2434','sname':'赵六','sage':43}, {'sid':'3424','sname':'李四','sage':42}, {'sid':'1231','sname':'王五','sage':35} ], newPeople:{ 'sid':'','sname':'','sage':'' }, seen:false, editSeen:false, checked:false, selected:[], editPeople:{ 'sid':'','sname':'','sage':'' } } ; var app = new Vue({ 'el':'#app', data:data, methods:{ // 添加 addBox:function(){ this.seen = this.seen == false ? true : false; }, //删除 del:function(index){ console.log(11); this.people.splice(index,1); }, //提交 add:function(){ //插入到people中 this.people.push(this.newPeople); this.newPeople = {}; this.seen = false }, //全选 allSelect:function(){ if(this.selected.length != this.people.length){ this.selected = []; for(var i = 0; i<this.people.length;i++){ this.selected.push(this.people[i].sid); console.log(this.people[i].sid); } }else{ this.selected = []; } }, //批量删除 delAll:function(){ for(var j = 0; j< this.selected.length;j++){ for(var i = 0; i< this.people.length; i++){ if(this.selected[j] == this.people[i].sid){ this.people.splice(i,1); } } } }, //编辑 update:function(index){ this.editSeen = true; this.editPeople = this.people[index]; }, //编辑后提交 editSubmit:function(){ this.editSeen = false; } }, watch:{ "selected":function(){ if(this.selected.length == this.people.length){ this.checked = true; }else{ this.checked = false; } } } }) </script> </body> </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是勤劳斑马最近收集整理的关于Vue.js实现可编辑的表格的全部内容,更多相关Vue.js实现可编辑内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(259)

评论列表共有 0 条评论

立即
投稿
返回
顶部