index.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Vue Demo</title>
  6. <style>
  7. </style>
  8. <link rel="stylesheet" href="../tmp/bootstrap.min.css">
  9. <script src="../tmp/vue.min.js"></script>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <div class="col-md-6 col-md-offset-3">
  14. <h1>Vue demo</h1>
  15. <div id="app">
  16. <table class="table table-hover ">
  17. <br/>
  18. <thead>
  19. <tr>
  20. <th>序号</th>
  21. <th>书名</th>
  22. <th>作者</th>
  23. <th>价格</th>
  24. <th>操作</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <tr v-for='book in filterBooks'>
  29. <td>{{book.id}}</td>
  30. <td>{{book.author}}</td>
  31. <td>{{book.name}}</td>
  32. <td>{{book.price}}</td>
  33. <td class="text-left">
  34. <button type="button" class="btn btn-success" @click="delBook(book)">删除</button>
  35. <button type="button" class="btn btn-success" @click="updateBook(book)">修改</button>
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. <!-- 添加书籍 -->
  41. <div id="add-book">
  42. <!--查询输入框-->
  43. <div class="row" style="margin-bottom: 30px;">
  44. <div class="col-md-4" style="text-align: left;font-size: 16px;line-height: 30px;">
  45. 请输入查询书名:
  46. </div>
  47. <div class="col-md-5" style="text-align: left;">
  48. <input type="text" class="form-control" v-model="search"></input>
  49. </div>
  50. </div>
  51. <h3 v-if="book.id">修改书籍</h3>
  52. <h3 v-else>新增书籍</h3>
  53. <hr/>
  54. <div class="form-group">
  55. <label>书名
  56. <input type="text" class="form-control" id="group" v-model="book.name">
  57. </label>
  58. </div>
  59. <div class="form-group">
  60. <label>作者<input type="text" class="form-control" id="author" v-model="book.author"></label>
  61. </div>
  62. <div class="form-group">
  63. <label>价格<input type="text" class="form-control" id="price" v-model="book.price"></label>
  64. </div>
  65. <button class="btn btn-primary btn-block" v-on:click="addBook()" v-if="book.id===0">添加</button>
  66. <button class="btn btn-primary btn-block" v-on:click="updatedBook()" v-else>修改</button>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. <script>
  72. var id = 0;
  73. var vm = new Vue({
  74. el: "#app",
  75. // 计算函数 动态变化的数据
  76. data: {
  77. book: {
  78. id: 0,
  79. author: '',
  80. name: '',
  81. price: ''
  82. },
  83. books: [{
  84. id: 1,
  85. author: '曹雪芹',
  86. name: '红楼梦',
  87. price: 36.00
  88. }, {
  89. id: 2,
  90. author: '曹雪芹',
  91. name: '三国演义',
  92. price: 37.00
  93. }, {
  94. id: 3,
  95. author: '曹雪芹',
  96. name: 'Good boy',
  97. price: 85.00
  98. }, {
  99. id: 4,
  100. author: '曹雪芹',
  101. name: '红楼梦',
  102. price: 39.00
  103. }],
  104. search: "", // 查询功能,""中不能加空格,否则默认查询空格
  105. isUpdate: false,
  106. },
  107. watch: {
  108. "book.id": (newValue) => {
  109. this.isUpdate = newValue !== 0;
  110. }
  111. },
  112. methods: { // 写函数
  113. addBook: function () {
  114. this.book.id = this.books.length + 1;
  115. this.books.push(this.book);
  116. this.book = {};
  117. },
  118. delBook: function (book) {
  119. const blength = this.books.length;
  120. this.books.splice(book.id - 1, 1);
  121. for (let i = 0; i < blength; i++) {
  122. if (book.id < this.books[i].id) {
  123. this.books[i].id -= 1;
  124. }
  125. }
  126. },
  127. // 修改按钮
  128. updateBook: function (book) {
  129. this.book = book
  130. },
  131. // 修改完成后的 确认按钮点击事件
  132. updatedBook: function () {
  133. this.book = {
  134. id: 0,
  135. author: '',
  136. name: '',
  137. price: ''
  138. }
  139. }
  140. },
  141. // 计算属性(过滤) 查询功能
  142. computed: {
  143. filterBooks: function () {
  144. const books = this.books;
  145. const search = this.search;
  146. return books.filter(function (book) {
  147. return book.name.toLowerCase().indexOf(search.toLocaleLowerCase()) !== -1
  148. });
  149. }
  150. },
  151. })
  152. </script>
  153. </body>
  154. </html>