product.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>商品列表</title>
  6. <script src="../js/jquery-2.1.4.js"></script>
  7. <style>
  8. /* 全局样式重置 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. font-family: "Microsoft Yahei", sans-serif;
  14. }
  15. /* 页面背景与布局 */
  16. body {
  17. background-color: #f5f5f5;
  18. padding: 50px;
  19. }
  20. /* 标题样式 */
  21. h1 {
  22. color: #333;
  23. margin-bottom: 30px;
  24. font-size: 26px;
  25. font-weight: 600;
  26. }
  27. /* 表格容器 */
  28. .table-container {
  29. background-color: #fff;
  30. border-radius: 8px;
  31. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  32. overflow: hidden;
  33. }
  34. /* 表格样式 */
  35. table {
  36. width: 100%;
  37. border-collapse: collapse;
  38. }
  39. th, td {
  40. padding: 14px 20px;
  41. text-align: left;
  42. border-bottom: 1px solid #eee;
  43. }
  44. th {
  45. background-color: #f8f9fa;
  46. color: #333;
  47. font-size: 16px;
  48. font-weight: 600;
  49. }
  50. td {
  51. color: #666;
  52. font-size: 15px;
  53. }
  54. /* 按钮样式 */
  55. .btn {
  56. padding: 6px 12px;
  57. border: none;
  58. border-radius: 4px;
  59. cursor: pointer;
  60. font-size: 14px;
  61. transition: background-color 0.3s;
  62. margin-right: 8px;
  63. }
  64. .edit-btn {
  65. background-color: #409eff;
  66. color: #fff;
  67. }
  68. .edit-btn:hover {
  69. background-color: #337ecc;
  70. }
  71. .delete-btn {
  72. background-color: #f56c6c;
  73. color: #fff;
  74. }
  75. .delete-btn:hover {
  76. background-color: #e45656;
  77. }
  78. /* 无数据提示 */
  79. .no-data {
  80. padding: 50px;
  81. text-align: center;
  82. color: #999;
  83. font-size: 16px;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <h1>商品列表</h1>
  89. <button onclick="add()">新增</button>
  90. <div class="table-container">
  91. <table id="productTable">
  92. <thead>
  93. <tr>
  94. <th>ID</th>
  95. <th>商品名称</th>
  96. <th>库存数量</th>
  97. <th>商品标题</th>
  98. <th>操作</th>
  99. </tr>
  100. </thead>
  101. <tbody>
  102. <!-- 数据将通过JS动态填充 -->
  103. <tr class="no-data">
  104. <td colspan="5">加载中...</td>
  105. </tr>
  106. </tbody>
  107. </table>
  108. </div>
  109. <script>
  110. function add(){
  111. location.href = "/js-demo-day02/js-demo-day02/demo/add.html"
  112. }
  113. // 编辑按钮点击事件
  114. // 删除按钮点击事件(含异步请求)
  115. function handleDelete(productId) {
  116. var flag = confirm("确定要删除吗");
  117. if(flag){
  118. $.get("http://c46a5489.natappfree.cc/product/delete?id="+productId,function (result){
  119. if(result.success){
  120. location.reload()
  121. }else{
  122. alert("删除失败")
  123. }
  124. })
  125. }
  126. // 弹出确认框,确认后执行删除逻辑
  127. // if (confirm(`确定要删除商品ID:${productId}吗?`)) {
  128. // // 使用jQuery发送删除异步请求
  129. // $.ajax({
  130. // url: `http://c46a5489.natappfree.cc/product/delete?id=${productId}`,
  131. // type: "get", // 若后端要求POST,可改为post并调整参数传递方式
  132. // dataType: "json",
  133. // success: function(result) {
  134. // // 判断后端返回的success状态
  135. // if (result.success) {
  136. // alert(result.msg || "删除成功");
  137. // // 刷新当前页面,重新加载商品列表
  138. // location.reload();
  139. // } else {
  140. // alert(result.msg || "删除失败,请重试");
  141. // }
  142. // },
  143. // error: function() {
  144. // alert("网络异常,删除请求失败,请检查网络或重试");
  145. // }
  146. // });
  147. // }
  148. }
  149. function handleEdit(id){
  150. location.href = "/js-demo-day02/js-demo-day02/demo/edit.html?id="+id
  151. }
  152. // 页面加载完成后请求数据
  153. $(function() {
  154. // 调用后端接口获取商品数据
  155. $.ajax({
  156. url: "http://c46a5489.natappfree.cc/product/list",
  157. type: "get",
  158. dataType: "json",
  159. success: function(result) {
  160. if (result.success && result.data.length > 0) {
  161. // 清空加载提示
  162. $("#productTable tbody").empty();
  163. // 遍历data数组,填充表格
  164. $.each(result.data, function(index, product) {
  165. const tr = `<tr>
  166. <td>${product.id}</td>
  167. <td>${product.productName}</td>
  168. <td>${product.stocket}</td>
  169. <td>${product.title}</td>
  170. <td>
  171. <button class="btn edit-btn" onclick="handleEdit(${product.id})">编辑</button>
  172. <button class="btn delete-btn" onclick="handleDelete(${product.id})">删除</button>
  173. </td>
  174. </tr>`;
  175. $("#productTable tbody").append(tr);
  176. });
  177. } else {
  178. // 无数据提示
  179. $("#productTable tbody").html('<tr class="no-data"><td colspan="5">暂无商品数据</td></tr>');
  180. }
  181. },
  182. error: function() {
  183. // 请求失败提示
  184. $("#productTable tbody").html('<tr class="no-data"><td colspan="5">数据加载失败,请刷新重试</td></tr>');
  185. }
  186. });
  187. });
  188. </script>
  189. </body>
  190. </html>