1: 永远不要眼高手低,看起来很简单,但是你从来没有去动手试一下,就不知道其中真正需要注意的许多细节,

2:完整code如下:

  1 <!DOCTYPE html>
2 <html lang="en">
3
4 <head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <title>Document</title>
8 <script src="../node_modules/vue/dist/vue.js"></script>
9
10 </head>
11
12 <body>
13 <div id="app">
14 <div>
15 <p><label for="inputid"><input type="text" v-model="inputid" v-bind:disabled="disableflag!=0"></label></p>
16 <p><label for="inputname"><input type="text" v-model="inputname"></label></p>
17 <p><button v-on:click="submitbtn">操作按钮</button></p>
18 </div>
19 <table border="1">
20 <thead>
21 <tr>
22 <th>编号</th>
23 <th>姓名</th>
24 <th>时间</th>
25 <th>操作</th>
26 </tr>
27 </thead>
28 <tbody>
29 <tr v-for="(item,index) in historys" :key="item.id">
30 <td>{{item.id}}</td>
31 <td>{{item.name}}</td>
32 <td>{{item.cdate}}</td>
33 <td><a href="javascript:;" v-on:click.prevent="edit(item.id)">编辑{{item.id}}</a> | <a href="javascript:;" v-on:click.prevent="deleteSoft(item.id,index)">删除</a></td>
34 </tr>
35 </tbody>
36 </table>
37 </div>
38
39 <script>
40 var app = new Vue({
41 el: "#app",
42 data: {
43 inputid: "",
44 inputname: "",
45 disableflag: 0,
46 addorEdit: false,
47 historys: [{
48 id: 1,
49 name: "三国演义",
50 cdate: "1881-01-10"
51 }, {
52 id: 2,
53 name: "水浒传",
54 cdate: "1891-11-21"
55 }, {
56 id: 3,
57 name: "聊斋异志",
58 cdate: "1895-2-13"
59 }, {
60 id: 4,
61 name: "大宋提刑官",
62 cdate: "1899-01-18"
63 }]
64 },
65 methods: {
66 edit: function(id) { //点击编辑获取该行的数据
67 var getone = this.historys.filter(function(item) {
68 return item.id == id;
69 })[0];
70 console.log(getone.id + ",name=" + getone.name);
71 this.inputid = getone.id;
72 this.inputname = getone.name;
73 this.disableflag = 1;
74 this.addorEdit = true;
75 },
76 deleteSoft: (id, index) => { //splice删除操作
77 console.log(this); //windows对象
78 console.log("index=" + index);
79 var getone = this.app.historys.filter(function(item) {
80 return item.id == id;
81 });
82 var getfindIndex = this.app.historys.findIndex(function(item) { //也可以这样来找到索引
83 return item.id == id;
84 });
85 console.log("getfindIndex=" + getfindIndex); //
86 console.log(getone[0].id + ",name=" + getone[0].name);
87 this.app.historys.splice(index, 1); //在数据中的第几个索引开始删除几个
88 console.log("删除成功");
89 },
90 submitbtn: function() {
91 console.log(this);
92 var getinputid = this.inputid;
93 var getinputname = this.inputname;
94
95 console.log(getinputid + "," + getinputname);
96 this.disableflag = 0;
97 if (this.addorEdit) { //编辑
98 if (this.isEmptyorNull(getinputid) || this.isEmptyorNull(getinputname)) {
99 alert("id和名称不可以为空");
100 return;
101 }
102 var geteditobj = this.historys.filter(function(item) {
103 return item.id == getinputid;
104 });
105 geteditobj[0].name = getinputname; //这里需要加上下标
106 console.log("编辑成功");
107 this.inputid = "";
108 this.inputname = "";
109 } else {
110 if (this.isEmptyorNull(getinputname)) {
111 alert("名称不可以为空");
112 return;
113 }
114 var listid = [];
115 this.historys.forEach(function(item) {
116 listid.push(item.id);
117 })
118 var getMaxid = Math.max(...listid) + 1; //查找出最大的id
119 console.log("getMaxid:" + getMaxid);
120 this.historys.push({
121 id: getMaxid,
122 name: getinputname,
123 cdate: "1998-10-12"
124 });
125 console.log("新增成功");
126 }
127 this.addorEdit = false;
128 },
129 isEmptyorNull: function(str) {
130 if (str == "" || str == "undefined" || str == null) {
131 return true;
132 }
133 return false;
134 }
135 }
136
137 })
138 </script>
139 </body>
140
141 </html>

3:测试效果如下

4:总结:

当你认真去做了,才会明白其中存在的细节,当你完成一个小Demo后也会感觉有小成就感的,再回头看时:嗯还行,挺简单的!

不去动手做就越堆越多,到时候会然人感觉啥都会一点,又啥都不精通,处于恶性循环的境地。其实个人觉得还是需要落地多多实操 即可,大不了多来几次!需要先打好扎实的基础!

最新文章

  1. 重建索引提高SQL Server性能
  2. oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)
  3. 【poj3017】 Cut the Sequence
  4. 解决Hibernate向MySQL数据库插入中文乱码问题
  5. uva729
  6. recordcount
  7. strstr strchr strrchr
  8. Ubuntu安装取色软件
  9. PHP Version之PHP5.2.x到5.3.x
  10. 非线性规划问题的matlab求解
  11. 在Apache上架设SVN使得可以通过http来使用SVN
  12. 非常棒的Visual Studo调试插件:OzCode 2.0 下载地址
  13. PLSQL Developer下报错信息显示乱码问题
  14. Serv-U FTP版本控制服务器 - 目录规范
  15. 解决SVN提交和更新代码冲突?
  16. Python笔记 【无序】 【三】
  17. JavaScript 空间分析库——JSTS和Turf【转】
  18. [UE4]Lock Always
  19. ACM__容器之vector
  20. 基于SQL和PYTHON的数据库数据查询select语句

热门文章

  1. docker(4)解决pull镜像速度缓慢
  2. CF-1328 E. Tree Queries
  3. Codeforces Round #656 (Div. 3) D. a-Good String
  4. UVA 10480 Sabotage (最大流最小割)
  5. 悬线法——有套路的DP
  6. hdu2546 饭卡
  7. Codeforces Gym-102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)
  8. Linux系统编程【2】——编写who命令
  9. Windows Terminal 更换主题
  10. 1.nginx安装和基本配置