Vue、layui实现简单的增删查改

淩亂°似流年 2021-12-12 13:11 424阅读 0赞

今天公司里不是很忙,忙里偷闲学了会Vue,做了个小demo,使用Vue和layui框架。全部代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Vue+layui Demo</title>
  6. <!-- 对应好js文件的路径哟 -->
  7. <link rel="stylesheet" href="layui/css/layui.css" />
  8. <script src="layui/layui.js"></script>
  9. <script src="vue.js"></script>
  10. </head>
  11. <body>
  12. <div id="app">
  13. <form class="layui-form" action="">
  14. <fieldset>
  15. <legend>
  16. 添加新用户
  17. </legend>
  18. <div style="width: 30%">
  19. <div class="layui-form-item">
  20. <label class="layui-form-label">名字:</label>
  21. <div class="layui-input-block">
  22. <input type="text" placeholder="请输入名字" class="layui-input" v-model="newPerson.name">
  23. </div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label class="layui-form-label">年龄:</label>
  27. <div class="layui-input-block">
  28. <input type="number" placeholder="请输入年龄" class="layui-input" v-model="newPerson.age">
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">性别:</label>
  33. <div class="layui-input-block">
  34. <select v-model="newPerson.sex">
  35. <option value=""></option>
  36. <option value="Male"></option>
  37. <option value="Female"></option>
  38. </select>
  39. </div>
  40. </div>
  41. </div>
  42. <button @click="add" type="button" class="layui-btn layui-btn-normal">添加</button>
  43. <button @click="find" type="button" class="layui-btn layui-btn-normal">查询</button>
  44. <button @click="ref" type="button" class="layui-btn layui-btn-normal">重置</button>
  45. </fieldset>
  46. </form>
  47. <table class="layui-table" style="text-align: center">
  48. <thead>
  49. <tr>
  50. <th style="text-align: center">姓名</th>
  51. <th style="text-align: center">年龄</th>
  52. <th style="text-align: center">性别</th>
  53. <th style="text-align: center">操作</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <tr v-for="(person,index) in people">
  58. <td>{
  59. { person.name }}</td>
  60. <td>{
  61. { person.age }}</td>
  62. <td>
  63. <span v-if="person.sex=='Male'"></span>
  64. <span v-else-if="person.sex=='Female'"></span>
  65. <span v-else></span>
  66. </td>
  67. <td :class="'text-center'"><button @click="del(index)"
  68. class="layui-btn layui-btn-sm layui-btn-danger">删除</button></td>
  69. </tr>
  70. </tbody>
  71. </table>
  72. </div>
  73. </body>
  74. <script>
  75. var vm = new Vue({
  76. el: '#app',
  77. data: {
  78. newPerson: {
  79. name: '',
  80. age: 0,
  81. sex: 'Male'
  82. },
  83. datas: [{
  84. name: 'Z',
  85. age: 22,
  86. sex: 'Male'
  87. }, {
  88. name: 'W',
  89. age: 22,
  90. sex: 'Male'
  91. }, {
  92. name: 'Q',
  93. age: 19,
  94. sex: 'Female'
  95. }, {
  96. name: 'R',
  97. age: 99,
  98. sex: 'Male'
  99. }],
  100. people: [],
  101. onepeople:{}
  102. },
  103. created: function () {
  104. this.people = this.datas.slice(0);//数组赋值 问题一
  105. },
  106. methods: {
  107. ref: function () {
  108. //绑定数据问题
  109. this.newPerson = {
  110. name: '',
  111. age: 0,
  112. sex: 'Male'
  113. }
  114. this.people.splice(0, this.people.length);
  115. this.people = this.datas.slice(0);
  116. },
  117. add: function () {
  118. if (this.newPerson.name == "" || this.newPerson.name == null) {
  119. alert("名字不能为空");
  120. } else if (this.newPerson.age == "" || this.newPerson.age == null) {
  121. alert("年龄不能为空");
  122. } else if (this.newPerson.sex == "" || this.newPerson.sex == null) {
  123. alert("性别不能为空");
  124. } else {
  125. this.people.push(this.newPerson);
  126. this.newPerson = {
  127. name: '',
  128. age: 0,
  129. sex: 'Male'
  130. }//问题二
  131. }
  132. },
  133. del: function (index) {
  134. this.people.splice(index, 1);//删除数组中的数据
  135. },
  136. find: function () {
  137. for (let p of this.people) {
  138. if (p.name == this.newPerson.name) {
  139. this.onepeople = p;
  140. break;
  141. }
  142. }
  143. if (this.onepeople.name != null) {
  144. this.people.length = 0;
  145. this.people.push(this.onepeople);
  146. } else {
  147. alert("无数据");
  148. }
  149. }
  150. }
  151. });
  152. </script>
  153. <script>
  154. layui.use('form', function () {
  155. var form = layui.form;
  156. form.render();//表单渲染 这里是针对select 问题三
  157. })
  158. </script>
  159. </html>

问题总结

1、数组间赋值,使用slice方法,避免地址复用
2、添加后改变输入框里的值,添加后的数据也会跟着变化,最直接的方法就是重新创建一个对象
3、layui表单中有几个标签必须要使用form模块渲染才能使用

发表评论

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

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

相关阅读