index.html 7.0 KB

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