vue(13)增删改查

╰+哭是因爲堅強的太久メ 2022-01-11 15:19 312阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>vue</title>
  6. <style type="text/css">
  7. table tr td{
  8. border:1px solid gray;
  9. }
  10. table thead tr{
  11. background-color: lightgray;
  12. }
  13. table{
  14. border-collapse: collapse;
  15. }
  16. div#app{
  17. margin:10px;
  18. width:400px;
  19. padding:20px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <script src="jquery.min.js"></script>
  25. <script src="vue.min.js"></script>
  26. <div id="app">
  27. <table id="heroListTable">
  28. <thead>
  29. <tr>
  30. <th>英雄名称</th>
  31. <th>血量</th>
  32. <th>操作</th>
  33. </tr>
  34. </thead>
  35. <tbody>
  36. <tr v-for="hero in heros">
  37. <td>{ { hero.name}}</td>
  38. <td>{ { hero.hp}}</td>
  39. <td>
  40. <a href="#" @click="edit(hero)">编辑</a>
  41. </td>
  42. <td>
  43. <a href="#" @click="deleteHero(hero.id)">删除</a>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td colspan="4">
  48. 英雄名称:<input type="text" v-model="hero4Add.name">
  49. <br>
  50. 血量:<input type="number" v-model="hero4Add.hp">
  51. <br>
  52. <button v-on:click="add">增加</button>
  53. </td>
  54. </tr>
  55. </tbody>
  56. </table>
  57. <div id="div4Update">
  58. 英雄名称:<input type="text" v-model="hero4Update.name"><!--双向绑定-->
  59. <br>
  60. 血量:<input type="number" v-model="hero4Update.hp">
  61. <br>
  62. <input type="hidden" v-model="hero4Update.id">
  63. <button v-on:click="update">修改</button>
  64. <button type="button" v-on:click="cancel">取消</button>
  65. </div>
  66. </div>
  67. <script type="text/javascript">
  68. $("#div4Update").hide();
  69. //model
  70. var data = {
  71. heros:[
  72. { id:1,name:"盖伦",hp:318},
  73. { id:2,name:"提莫",hp:320},
  74. { id:3,name:"安妮",hp:410}
  75. ],
  76. hero4Add:{ id:0,name:'',hp:'0'},
  77. hero4Update:{ id:0,name:'',hp:'0'}
  78. };
  79. var maxId = 3;
  80. for(var i=0;i<data.heros.length;i++){
  81. if(data.heros[i].id > maxId){
  82. maxId = this.heros[i].id;
  83. }
  84. }
  85. //view
  86. var vue = new Vue({
  87. el:"#app",
  88. data:data,
  89. methods:{
  90. add:function(event){
  91. //获取最大id
  92. maxId++;
  93. //计算最大值
  94. this.hero4Add.id = maxId;
  95. if(this.hero4Add.name.length == 0){
  96. this.hero4Add.name = "hero#"+this.hero4Add.id
  97. }
  98. //把对象加入数组
  99. this.heros.push(this.hero4Add);
  100. //只想新对象
  101. this.hero4Add = { id:0,name:'',hp:'0'}
  102. },
  103. deleteHero:function(id){
  104. console.log("id"+id);
  105. for(var i = 0; i<this.heros.length;i++){
  106. if(this.heros[i].id ==id){
  107. this.heros.splice(i,1);
  108. break;
  109. }
  110. }
  111. },
  112. edit:function(hero){
  113. $("#heroListTable").hide();
  114. $("#div4Update").show();
  115. this.hero4Update = hero;
  116. },
  117. update:function(){
  118. $("#heroListTable").show();
  119. $("#div4Update").hide();
  120. },
  121. cancel:function(){
  122. $("#heroListTable").show();
  123. $("#div4Update").hide();
  124. }
  125. }
  126. });
  127. </script>
  128. </body>
  129. </html>

在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,312人围观)

还没有评论,来说两句吧...

相关阅读