index.html 7.4 KB

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