iT邦幫忙

2026 iThome 鐵人賽

DAY 9
0
自我挑戰組

Laravel 讀碼源系列 第 9

產品管理列表的前端code

  • 分享至 

  • xImage
  •  

這個是永康社大的作品,也歡迎大家來上課或購買
https://laihao.vaserver.com/admin/good_design
這個也有另一個列表樣式,
因為怕大家動到CRUD所以先反灰,
不過大家還是可以看到原始的按鈕的樣子
https://laihao.vaserver.com/admin/products
程式碼的內容:

<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>產品管理(卡片式)- 我的商店</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft JhengHei', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding-top: 80px;
        }

        .container {
            max-width: 1600px;
            margin: 0 auto;
            padding: 30px 20px;
        }

        .hero {
            text-align: center;
            color: white;
            padding: 40px 20px 20px;
        }

        .hero h2 {
            font-size: 2.5em;
            margin-bottom: 15px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }

        .product-count {
            display: inline-block;
            background: rgba(255, 255, 255, 0.2);
            padding: 10px 25px;
            border-radius: 25px;
            color: white;
            font-size: 1.1em;
            font-weight: 600;
            margin-bottom: 20px;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }

        .btn-action {
            padding: 12px 30px;
            background: white;
            color: #667eea;
            border: none;
            border-radius: 25px;
            font-weight: 600;
            font-size: 1em;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 15px rgba(255, 255, 255, 0.3);
            text-decoration: none;
            display: inline-block;
        }

        .btn-action:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(255, 255, 255, 0.4);
            color: #667eea;
            text-decoration: none;
        }

        .btn-switch {
            background: rgba(255, 255, 255, 0.95);
            border: 2px solid white;
        }

        /* 產品卡片網格 */
        .products-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }

        /* 產品卡片 */
        .product-card {
            background: white;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
        }

        .product-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 12px 30px rgba(0,0,0,0.25);
        }

        /* 圖片區域 (3:4 比例) */
        .product-image {
            width: 100%;
            padding-top: 133.33%; /* 4/3 = 1.333... = 133.33% */
            position: relative;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            overflow: hidden;
        }

        .product-image img {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .product-image .no-image {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 4em;
            color: rgba(0,0,0,0.1);
        }

        .product-image .image-count {
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(0,0,0,0.7);
            color: white;
            padding: 5px 12px;
            border-radius: 15px;
            font-size: 0.85em;
            font-weight: 600;
        }

        /* 產品資訊區 */
        .product-info {
            padding: 20px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .product-id {
            font-size: 0.85em;
            color: #95a5a6;
            margin-bottom: 8px;
            font-weight: 600;
        }

        .product-title {
            font-size: 1.15em;
            font-weight: 700;
            color: #2c3e50;
            margin-bottom: 8px;
            line-height: 1.4;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        .product-content {
            font-size: 0.9em;
            color: #7f8c8d;
            margin-bottom: 12px;
            line-height: 1.5;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            flex: 1;
        }

        .product-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-top: 12px;
            border-top: 1px solid #ecf0f1;
        }

        .product-price {
            font-size: 1.4em;
            font-weight: 700;
            color: #e74c3c;
        }

        .product-quantity {
            font-size: 0.9em;
            color: #95a5a6;
        }

        .product-quantity.low-stock {
            color: #e74c3c;
            font-weight: 600;
        }

        .product-actions {
            display: flex;
            gap: 8px;
        }

        .btn-upload,
        .btn-edit {
            flex: 1;
            padding: 10px 15px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.9em;
            font-weight: 600;
            transition: all 0.3s;
            text-align: center;
        }

        .btn-upload {
            background: #3498db;
            color: white;
        }

        .btn-upload:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }

        .btn-edit {
            background: #2ecc71;
            color: white;
        }

        .btn-edit:hover {
            background: #27ae60;
            transform: translateY(-2px);
        }

        /* 分頁 */
        .pagination-wrapper {
            display: flex;
            justify-content: center;
            margin-top: 40px;
        }

        .pagination {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
            justify-content: center;
        }

        .pagination a,
        .pagination span {
            padding: 10px 16px;
            border-radius: 8px;
            text-decoration: none;
            color: white;
            background: rgba(255, 255, 255, 0.2);
            border: 2px solid rgba(255, 255, 255, 0.3);
            transition: all 0.3s;
            font-weight: 600;
        }

        .pagination a:hover {
            background: white;
            color: #667eea;
            transform: translateY(-2px);
        }

        .pagination .active span {
            background: white;
            color: #667eea;
        }

        .pagination .disabled span {
            opacity: 0.5;
            cursor: not-allowed;
        }

        /* Modal 樣式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 2000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.7);
            backdrop-filter: blur(5px);
        }

        .modal.show {
            display: block;
        }

        .modal-dialog {
            max-width: 500px;
            margin: 5% auto;
        }

        .modal-content {
            background-color: white;
            padding: 0;
            border-radius: 15px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
            position: relative;
        }

        .modal-header {
            padding: 20px 30px;
            border-bottom: 2px solid #ecf0f1;
            position: relative;
        }

        .modal-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px 15px 0 0;
        }

        .modal-title {
            color: #2c3e50;
            font-size: 1.5em;
            font-weight: 600;
            margin-top: 5px;
        }

        .close {
            position: absolute;
            right: 20px;
            top: 20px;
            color: #95a5a6;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            border: none;
            background: none;
            line-height: 20px;
        }

        .close:hover {
            color: #2c3e50;
        }

        .modal-body {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 8px;
        }

        .form-control {
            width: 100%;
            padding: 10px 15px;
            border: 2px solid #ecf0f1;
            border-radius: 8px;
            font-size: 1em;
            cursor: pointer;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn-primary {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-primary:disabled {
            background: #95a5a6;
            cursor: not-allowed;
            transform: none;
        }

        /* 空狀態 */
        .empty-state {
            grid-column: 1 / -1;
            text-align: center;
            padding: 80px 20px;
            color: white;
        }

        .empty-state-icon {
            font-size: 5em;
            margin-bottom: 20px;
            opacity: 0.7;
        }

        .empty-state h3 {
            font-size: 1.8em;
            margin-bottom: 10px;
        }

        /* 響應式設計 */
        @media (max-width: 768px) {
            .hero h2 {
                font-size: 2em;
            }

            .products-grid {
                grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
                gap: 15px;
            }

            .product-info {
                padding: 15px;
            }

            .product-title {
                font-size: 1em;
            }

            .product-price {
                font-size: 1.2em;
            }
        }

        @media (min-width: 1400px) {
            .products-grid {
                grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            }
        }
    </style>
</head>
<body>
    @if(!empty($readonly))
<div style="
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    text-align: center;
    padding: 18px 20px;
    font-size: 1.3em;
    font-weight: 700;
    letter-spacing: 2px;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 9999;
    box-shadow: 0 4px 15px rgba(245, 87, 108, 0.4);
">
    🛒 如預購買,請每周一晚上到永康社區大學詢問
</div>
@endif
@if(!empty($readonly))
<div style="
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    text-align: center;
    padding: 18px 20px;
    font-size: 1.3em;
    font-weight: 700;
    letter-spacing: 2px;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 9999;
    box-shadow: 0 4px 15px rgba(245, 87, 108, 0.4);
">
    🛒 如預購買,請每周一晚上到永康社區大學詢問
</div>
@endif



    <div class="hero">
        <h2>🛍️ 產品管理(卡片式)</h2>
        <span class="product-count">產品總數: {{ $productCount }}</span>
    </div>

    <div class="container">
        @if(empty($readonly))
    <div class="action-buttons">
        <a href="{{ route('admin.products.index') }}" class="btn-action btn-switch">
            📋 切換至列表模式
        </a>
        <a href="{{ route('admin.products.create') }}" class="btn-action">
            ➕ 新增卡片
        </a>
        <button type="button" class="btn-action import">📊 匯入EXCEL</button>
    </div>
@endif

        <!-- 產品卡片網格 -->
        <div class="products-grid">
            @forelse($products as $product)
                <div class="product-card">
                    <!-- 圖片區域 (3:4 比例) -->
                    <div class="product-image">
                        @if($product->images && $product->images->count() > 0)
                            <img src="/show-image.php?path={{ urlencode($product->images->first()->path) }}"
                                 alt="{{ $product->title }}">
                            @if($product->images->count() > 1)
                                <span class="image-count">📷 {{ $product->images->count() }}</span>
                            @endif
                        @else
                            <div class="no-image">📦</div>
                        @endif
                    </div>

                    <!-- 產品資訊 -->
                    <div class="product-info">
                        <div class="product-id">#{{ $product->id }}</div>
                        <h3 class="product-title">{{ $product->title }}</h3>
                        <p class="product-content">{{ $product->content ?: '無說明' }}</p>

                        <div class="product-meta">
                            <div class="product-price">NT$ {{ number_format($product->price) }}</div>
                            <div class="product-quantity {{ $product->quantity < 10 ? 'low-stock' : '' }}">
                                庫存: {{ $product->quantity }}
                            </div>
                        </div>

                       @if(empty($readonly))
    <div class="product-actions">
        <button class="btn-upload upload_image" data-id="{{ $product->id }}">
            📸 上傳圖片
        </button>
        <a href="{{ route('admin.products.edit', $product->id) }}"
           class="btn-edit"
           style="text-decoration: none; display: flex; align-items: center; justify-content: center;">
            ✏️ 編輯
        </a>
    </div>
@endif
                    </div>
                </div>
            @empty
                <div class="empty-state">
                    <div class="empty-state-icon">📭</div>
                    <h3>尚無產品資料</h3>
                    <p>點擊上方「匯入EXCEL」按鈕開始建立產品</p>
                </div>
            @endforelse
        </div>

        <!-- 分頁 -->
        <!-- 分頁 -->
@if($products->hasPages())
    <div class="pagination-wrapper">
        {{ $products->links() }}
    </div>
@endif

<!-- 製作廠商 -->
<div style="
    text-align: center;
    margin-top: 30px;
    margin-bottom: 20px;
    color: rgba(255,255,255,0.8);
    font-size: 0.95em;
">
    Powered by 
    <a href="https://laihao2.com/Home/About" 
       target="_blank"
       style="
           color: white;
           font-weight: 700;
           text-decoration: none;
           border-bottom: 2px solid rgba(255,255,255,0.5);
           padding-bottom: 2px;
           transition: all 0.3s;
       "
       onmouseover="this.style.borderBottomColor='white'"
       onmouseout="this.style.borderBottomColor='rgba(255,255,255,0.5)'"
    >
        🏢 來好資訊
    </a>
</div>
    </div>

    <!-- Upload Image Modal -->
    <div class="modal fade" id="upload_image_modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">上傳圖片</h5>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="upload_form" method="POST" action="/admin/products/upload-image" enctype="multipart/form-data">
                        @csrf
                        <input type="hidden" id="product_id" name="product_id">
                        <div class="form-group">
                            <label for="product_image">選擇圖片</label>
                            <input type="file" class="form-control" id="product_image" name="product_image" accept="image/*" required>
                        </div>
                        <button type="submit" class="btn-primary">上傳</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Import Excel Modal -->
    <div class="modal fade" id="import" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">📊 匯入 Excel 檔案</h5>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="import_form" method="POST" action="/admin/products/excel/import" enctype="multipart/form-data">
                        @csrf
                        <div class="form-group">
                            <label for="excel">選擇 Excel 檔案</label>
                            <input type="file" class="form-control" id="excel" name="excel" accept=".xlsx,.xls,.csv" required>
                            <small style="color: #95a5a6; margin-top: 5px; display: block;">
                                支援格式:.xlsx, .xls, .csv
                            </small>

                            <div style="margin-top: 15px; padding: 12px; background: #e8f4f8; border-left: 4px solid #667eea; border-radius: 5px;">
                                <div style="color: #2c3e50; margin-bottom: 8px;">
                                    <strong>第一次使用?</strong>
                                    <a href="/download-sample.php"
                                       style="color: #667eea; font-weight: 600; text-decoration: none; margin-left: 5px;"
                                       download>
                                        📥 下載範例檔案
                                    </a>
                                </div>
                                <small style="color: #7f8c8d; display: block;">
                                    範例包含:標題、內容、價格、數量
                                </small>
                            </div>
                        </div>
                        <button type="submit" class="btn-primary">開始匯入</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
    $(document).ready(function() {
        // 上傳圖片按鈕
        $('.upload_image').click(function() {
            $('#product_id').val($(this).data('id'));
            $('#upload_image_modal').addClass('show');
        });

        // 匯入按鈕
        $('.import').click(function() {
            $('#import').addClass('show');
        });

        // 關閉所有 modal
        $('.close, [data-dismiss="modal"]').click(function() {
            $('.modal').removeClass('show');
        });

        // 點擊外部關閉 modal
        $(window).click(function(event) {
            if ($(event.target).hasClass('modal')) {
                $('.modal').removeClass('show');
            }
        });

        // 表單提交後顯示處理中狀態
        $('#import_form').submit(function() {
            $(this).find('.btn-primary').prop('disabled', true).text('匯入中...');
        });

        $('#upload_form').submit(function() {
            $(this).find('.btn-primary').prop('disabled', true).text('上傳中...');
        });
    });
    </script>
</body>
</html>


眼尖的人應該會發現有地方怪怪的~
這是人打的依據QQ
明天再來解釋


上一篇
產品管理列表的內容
系列文
Laravel 讀碼源9
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言