index.html 6.0 KB

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