当前位置:   article > 正文

vue.对列表增删操作_vue中list的添加和删除

vue中list的添加和删除

 先看效果图,实现增删操作,还有对输入框的数据非空,查重,添加后清空文本框内容

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
  6. <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
  7. <title></title>
  8. </head>
  9. <body class="container">
  10. <style>
  11. .a1 {
  12. background: white;
  13. }
  14. .red {
  15. background: greenyellow;
  16. }
  17. </style>
  18. <div id="app">
  19. 姓名:<input class="form-control" v-model="name" /><br /> 性别:
  20. <input class="form-control" v-model="sex" /><br />
  21. <button class="btn btn-primary" @click="adda(1)">添加</button>
  22. <table class="table table-bordered">
  23. <tr>
  24. <td>序号</td>
  25. <td>姓名</td>
  26. <td>性别</td>
  27. <td>操作</td>
  28. </tr>
  29. <tr v-for="(a,index) in tables" :class="a.xx">
  30. <td>{{index}}</td>
  31. <td>{{a.name}}</td>
  32. <td>{{a.sex}}</td>
  33. <td>
  34. <button class="btn btn-primary" @click="adda(index) ">添加</button>
  35. <button class="btn btn-danger" @click="pop(index)">删除</button>
  36. </td>
  37. </tr>
  38. </table>
  39. </div>
  40. <script type="text/javascript">
  41. var vm = new Vue({
  42. xx: "",
  43. el: "#app",
  44. data: {
  45. isshow: false,
  46. none: false,
  47. name: '',
  48. sex: '',
  49. xx: '',
  50. show: true,
  51. tables: [{
  52. name: '常丕丽1',
  53. sex: '男',
  54. xx: ""
  55. },
  56. {
  57. name: '常丕丽2',
  58. sex: '女',
  59. xx: ""
  60. },
  61. {
  62. name: '常丕丽3',
  63. sex: '男',
  64. xx: ""
  65. }
  66. ]
  67. },
  68. methods: {
  69. adda: function(index) {
  70. var iii = true
  71. for(var i = 0; i < this.tables.length; i++) {
  72. if(this.name == this.tables[i].name) {
  73. iii = false;
  74. break;
  75. }
  76. };
  77. if(iii == false) {
  78. alert("内容重复")
  79. } else {
  80. if(this.name == "" || this.sex == "") {
  81. alert("姓名或为空")
  82. } else {
  83. if(index == 1) {
  84. this.tables.push({
  85. name: this.name,
  86. sex: this.sex,
  87. xx: 'red'
  88. });
  89. this.name = "";
  90. this.sex = '';
  91. } else {
  92. this.tables.splice(index + 1, 0, {
  93. name: this.name,
  94. sex: this.sex,
  95. xx: 'red'
  96. })
  97. }
  98. }
  99. }
  100. },
  101. pop: function(index) {
  102. this.tables.splice(index, 1)
  103. }
  104. }
  105. })
  106. </script>
  107. </body>
  108. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/224051
推荐阅读
相关标签
  

闽ICP备14008679号