index.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 id="my_alert_success" class="alert alert-success hide">这是一个成功提示!</div>-->
  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. <form>
  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.name"></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.author"></div>
  84. </div>
  85. <div class="form-group row">
  86. <label class="col-sm-2 col-form-label">价格</label>
  87. <div class="col-sm-10"><input type="text" class="form-control" v-model="book.price"></div>
  88. </div>
  89. </form>
  90. </div>
  91. <div class="modal-footer ">
  92. <div class="col-sm-3 offset-9">
  93. <button class="btn btn-primary btn-block" v-on:click="updatedBook()" v-if="isUpdate">修改</button>
  94. <button class="btn btn-primary btn-block" v-on:click="addBook()" v-else>添加</button>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. <script>
  102. const vm = new Vue({
  103. el: "#app",
  104. // 计算函数 动态变化的数据
  105. data: {
  106. initBook: {
  107. id: 0,
  108. author: '',
  109. name: '',
  110. price: ''
  111. },
  112. book: deepClone(this.initBook),
  113. books: [{
  114. id: 1,
  115. author: '曹雪芹',
  116. name: '红楼梦',
  117. price: 36.00
  118. }, {
  119. id: 2,
  120. author: '曹雪芹',
  121. name: '三国演义',
  122. price: 37.00
  123. }],
  124. search: "",
  125. page: 1,
  126. pageSize: 10,
  127. totalPage: 1
  128. },
  129. watch: {},
  130. // 计算属性(过滤) 查询功能
  131. computed: {
  132. isUpdate() {
  133. return this.book.id !== undefined && this.book.id !== 0;
  134. }
  135. },
  136. mounted: function () {
  137. // this.queryList();
  138. },
  139. methods: {
  140. refresh_pagination: function () {
  141. $('#pageDiv').pagination({
  142. pages: this.totalPage, // 页数
  143. edges: 2, // 边缘页数,不用修改
  144. cssStyle: 'pagination',
  145. displayedPages: 5, // 展示相连页数,不用修改
  146. onPageClick: function () {
  147. //点击时调用
  148. this.queryList();
  149. }
  150. });
  151. },
  152. queryList: function () {
  153. // 获取数据,刷新页数
  154. axios.post("/api/list", this.book).then(res => {
  155. if (res.success) {
  156. this.totalPage = 10;
  157. this.books = res.data;
  158. this.refresh_pagination();
  159. }
  160. })
  161. },
  162. addBook: function () {
  163. axios.post("/api/add", this.book).then(res => {
  164. if (res.success) {
  165. this.queryList();
  166. }
  167. })
  168. },
  169. delBook: function (book) {
  170. axios.post("/api/delete", book).then(res => {
  171. if (res.success) {
  172. this.queryList();
  173. }
  174. })
  175. },
  176. // 修改按钮
  177. editBook: (book) => {
  178. this.book = deepClone(book);
  179. $('#exampleModalCenter').modal('show');
  180. },
  181. // 修改完成后的 确认按钮点击事件
  182. updatedBook: function () {
  183. // 异步请求
  184. axios.post("/api/update", this.book).then(res => {
  185. if (res.success) {
  186. this.queryList();
  187. }
  188. })
  189. }
  190. },
  191. });
  192. </script>
  193. </body>
  194. </html>