You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jekyll循环关联一对多数据:构建层级表格的可行性问询

Solution: Build Hierarchical Tables with Nested Loops

Absolutely! This is totally achievable with nested loops that walk through your one-to-many relationships exactly as you described. Let’s break this down with a practical example using sample data, so you can see how it works step by step.

Step 1: Define Your Data Structures

First, let’s set up sample data matching your Brands/Models/Colors/Items relationships (adjust this to match your actual data format):

# Sample relational data
brands = [
    {"id": 1, "name": "TechPro"},
    {"id": 2, "name": "HomeGear"}
]

models = [
    {"id": 1, "brand_id": 1, "name": "Laptop X15"},
    {"id": 2, "brand_id": 1, "name": "Phone Pro Max"},
    {"id": 3, "brand_id": 2, "name": "Smart Oven 3000"}
]

colors = [
    {"id": 1, "model_id": 1, "name": "Space Gray"},
    {"id": 2, "model_id": 1, "name": "Silver"},
    {"id": 3, "model_id": 2, "name": "Midnight Blue"}
]

items = [
    {"id": 1, "color_id": 1, "name": "TechPro X15 256GB", "sku": "TP-X15-SG-256"},
    {"id": 2, "color_id": 1, "name": "TechPro X15 512GB", "sku": "TP-X15-SG-512"},
    {"id": 3, "color_id": 2, "name": "TechPro X15 1TB", "sku": "TP-X15-SV-1TB"},
    {"id": 4, "color_id": 3, "name": "TechPro Pro Max 128GB", "sku": "TP-PM-MB-128"}
]

Step 2: Nested Logic to Filter & Build the Table

The core idea is to traverse the relationships sequentially:

  • Grab the first brand from your list
  • Filter to find its first associated model
  • Filter again to find that model’s first associated color
  • Pull all items linked to that color

Here’s how to implement this in Python, generating an HTML table (which supports the hierarchical header spanning you need):

# Get the first brand in your list
first_brand = brands[0]

# Find the first model belonging to this brand
first_model = next(model for model in models if model["brand_id"] == first_brand["id"])

# Find the first color belonging to this model
first_color = next(color for color in colors if color["model_id"] == first_model["id"])

# Get all items matching this color
matching_items = [item for item in items if item["color_id"] == first_color["id"]]

# Generate the hierarchical table
table_output = f"""
<table style="border-collapse: collapse; width: 80%; margin: 20px 0;">
    <!-- Brand header (spans all columns) -->
    <tr>
        <th colspan="2" style="border: 1px solid #ddd; padding: 8px; background: #f5f5f5;">{first_brand['name']}</th>
    </tr>
    <!-- Model sub-header (spans all columns) -->
    <tr>
        <th colspan="2" style="border: 1px solid #ddd; padding: 8px; background: #fafafa;">{first_model['name']}</th>
    </tr>
    <!-- Color & Item Detail Headers -->
    <tr>
        <th style="border: 1px solid #ddd; padding: 8px; background: #fafafa;">{first_color['name']} Item Name</th>
        <th style="border: 1px solid #ddd; padding: 8px; background: #fafafa;">SKU</th>
    </tr>
    <!-- List all matching items -->
    {''.join([f'<tr><td style="border: 1px solid #ddd; padding: 8px;">{item["name"]}</td><td style="border: 1px solid #ddd; padding: 8px;">{item["sku"]}</td></tr>' for item in matching_items])}
</table>
"""

# Print or render the table
print(table_output)

Step 3: Extending to All Brands/Models/Colors

If you later want to generate tables for every brand → model → color combination, just wrap the logic in additional loops:

  • Outer loop: Iterate over all brands
  • Middle loop: Iterate over all models for the current brand
  • Inner loop: Iterate over all colors for the current model

Alternative: Database Query Approach

If your data is stored in a database, you can use a JOIN query to fetch the exact dataset you need upfront, avoiding manual filtering in code. Here’s an example SQL query:

SELECT 
    b.name AS brand_name,
    m.name AS model_name,
    c.name AS color_name,
    i.name AS item_name,
    i.sku AS item_sku
FROM Brands b
JOIN Models m ON b.id = m.brand_id
JOIN Colors c ON m.id = c.model_id
JOIN Items i ON c.id = i.color_id
-- Filter for the first brand, first model, first color
WHERE 
    b.id = (SELECT MIN(id) FROM Brands)
    AND m.id = (SELECT MIN(id) FROM Models WHERE brand_id = b.id)
    AND c.id = (SELECT MIN(id) FROM Colors WHERE model_id = m.id);

This query returns exactly the data you need to build your table, which you can then render with your preferred templating language.

内容的提问来源于stack exchange,提问作者Jon Griffith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:25:50