| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>商品列表</title>
- <script src="../js/jquery-2.1.4.js"></script>
- <style>
- /* 全局样式重置 */
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- font-family: "Microsoft Yahei", sans-serif;
- }
- /* 页面背景与布局 */
- body {
- background-color: #f5f5f5;
- padding: 50px;
- }
- /* 标题样式 */
- h1 {
- color: #333;
- margin-bottom: 30px;
- font-size: 26px;
- font-weight: 600;
- }
- /* 表格容器 */
- .table-container {
- background-color: #fff;
- border-radius: 8px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- overflow: hidden;
- }
- /* 表格样式 */
- table {
- width: 100%;
- border-collapse: collapse;
- }
- th, td {
- padding: 14px 20px;
- text-align: left;
- border-bottom: 1px solid #eee;
- }
- th {
- background-color: #f8f9fa;
- color: #333;
- font-size: 16px;
- font-weight: 600;
- }
- td {
- color: #666;
- font-size: 15px;
- }
- /* 按钮样式 */
- .btn {
- padding: 6px 12px;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- font-size: 14px;
- transition: background-color 0.3s;
- margin-right: 8px;
- }
- .edit-btn {
- background-color: #409eff;
- color: #fff;
- }
- .edit-btn:hover {
- background-color: #337ecc;
- }
- .delete-btn {
- background-color: #f56c6c;
- color: #fff;
- }
- .delete-btn:hover {
- background-color: #e45656;
- }
- /* 无数据提示 */
- .no-data {
- padding: 50px;
- text-align: center;
- color: #999;
- font-size: 16px;
- }
- </style>
- </head>
- <body>
- <h1>商品列表</h1>
- <button onclick="add()">新增</button>
- <div class="table-container">
- <table id="productTable">
- <thead>
- <tr>
- <th>ID</th>
- <th>商品名称</th>
- <th>库存数量</th>
- <th>商品标题</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody>
- <!-- 数据将通过JS动态填充 -->
- <tr class="no-data">
- <td colspan="5">加载中...</td>
- </tr>
- </tbody>
- </table>
- </div>
- <script>
- function add(){
- location.href = "/js-demo-day02/js-demo-day02/demo/add.html"
- }
- // 编辑按钮点击事件
- // 删除按钮点击事件(含异步请求)
- function handleDelete(productId) {
- var flag = confirm("确定要删除吗");
- if(flag){
- $.get("http://c46a5489.natappfree.cc/product/delete?id="+productId,function (result){
- if(result.success){
- location.reload()
- }else{
- alert("删除失败")
- }
- })
- }
- // 弹出确认框,确认后执行删除逻辑
- // if (confirm(`确定要删除商品ID:${productId}吗?`)) {
- // // 使用jQuery发送删除异步请求
- // $.ajax({
- // url: `http://c46a5489.natappfree.cc/product/delete?id=${productId}`,
- // type: "get", // 若后端要求POST,可改为post并调整参数传递方式
- // dataType: "json",
- // success: function(result) {
- // // 判断后端返回的success状态
- // if (result.success) {
- // alert(result.msg || "删除成功");
- // // 刷新当前页面,重新加载商品列表
- // location.reload();
- // } else {
- // alert(result.msg || "删除失败,请重试");
- // }
- // },
- // error: function() {
- // alert("网络异常,删除请求失败,请检查网络或重试");
- // }
- // });
- // }
- }
- function handleEdit(id){
- location.href = "/js-demo-day02/js-demo-day02/demo/edit.html?id="+id
- }
- // 页面加载完成后请求数据
- $(function() {
- // 调用后端接口获取商品数据
- $.ajax({
- url: "http://c46a5489.natappfree.cc/product/list",
- type: "get",
- dataType: "json",
- success: function(result) {
- if (result.success && result.data.length > 0) {
- // 清空加载提示
- $("#productTable tbody").empty();
- // 遍历data数组,填充表格
- $.each(result.data, function(index, product) {
- const tr = `<tr>
- <td>${product.id}</td>
- <td>${product.productName}</td>
- <td>${product.stocket}</td>
- <td>${product.title}</td>
- <td>
- <button class="btn edit-btn" onclick="handleEdit(${product.id})">编辑</button>
- <button class="btn delete-btn" onclick="handleDelete(${product.id})">删除</button>
- </td>
- </tr>`;
- $("#productTable tbody").append(tr);
- });
- } else {
- // 无数据提示
- $("#productTable tbody").html('<tr class="no-data"><td colspan="5">暂无商品数据</td></tr>');
- }
- },
- error: function() {
- // 请求失败提示
- $("#productTable tbody").html('<tr class="no-data"><td colspan="5">数据加载失败,请刷新重试</td></tr>');
- }
- });
- });
- </script>
- </body>
- </html>
|