index.html 7.2 KB

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