index.html 6.7 KB

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