You need to enable JavaScript to run this app. 基于Firebase实时数据库提取的JSON生成嵌套表格与预期不符,求助实现
优惠活动
大模型
产品
解决方案
定价
更多

基于Firebase实时数据库提取的JSON生成嵌套表格与预期不符,求助实现

基于Firebase嵌套库存JSON生成带合并单元格的分层表格

现有代码的核心问题点

  • 行创建逻辑错位:函数初始化时就主动创建了第一行空row,后续循环动态插入新行时,超市名称的合并单元格被绑定在初始空行上,和后续批次行完全错位,导致rowSpan合并属性完全失效
  • 缺失表头构建逻辑:没有提前聚合所有跨批次的SKU品类(如green200、red200这类键值)作为横向列头,仅渲染了超市、批次ID两列,完全丢失了各品类的在售状态数据
  • 单元格内容缺失:循环提取了每个批次下的品类键值productKeys,但从未将对应yes/no的状态值写入表格单元格
  • 缺少基础表格样式:原生无样式的DOM表格默认会折叠单元格边界,视觉上会进一步放大结构错位的观感

修正后的可运行实现

代码包含动态样式注入、全量SKU聚合、合并单元格逻辑、状态标识全流程,无外部依赖:

// 基础表格样式动态注入,避免额外外部CSS引用
const tableStyle = `
  table { border-collapse: collapse; width: 100%; margin: 1rem 0; }
  th, td { border: 1px solid #e2e8f0; padding: 8px 12px; text-align: left; }
  th { background: #f8fafc; font-weight: 600; }
  .stock-yes { color: #16a34a; }
  .stock-no { color: #dc2626; }
`;
const styleEl = document.createElement('style');
styleEl.textContent = tableStyle;
document.head.appendChild(styleEl);

function jsonTabulizer(jsonData, containerSelector) {
  // 逐层定位数据根节点
  const availabilityRoot = jsonData.availability;
  const storeList = Object.keys(availabilityRoot);
  
  // 提前聚合所有出现过的SKU品类,生成统一横向表头
  const allSkuSet = new Set();
  storeList.forEach(storeId => {
    const batchList = Object.keys(availabilityRoot[storeId]);
    batchList.forEach(batchId => {
      const skuMap = availabilityRoot[storeId][batchId];
      Object.keys(skuMap).forEach(sku => allSkuSet.add(sku));
    });
  });
  const allSkuList = Array.from(allSkuSet);

  // 创建表格本体
  const table = document.createElement('table');
  // 构建表头行
  const headerRow = table.insertRow();
  ['超市名称', '库存批次ID', ...allSkuList].forEach(headerText => {
    const th = document.createElement('th');
    th.textContent = headerText;
    headerRow.appendChild(th);
  });

  // 逐行渲染数据,处理单元格合并
  storeList.forEach(storeId => {
    const batchList = Object.keys(availabilityRoot[storeId]);
    batchList.forEach((batchId, batchIndex) => {
      const currentRow = table.insertRow();
      // 每个超市的第一个批次行,插入带rowSpan的合并单元格
      if (batchIndex === 0) {
        const storeCell = currentRow.insertCell();
        storeCell.textContent = storeId;
        storeCell.rowSpan = batchList.length;
      }
      // 插入批次ID单元格
      const batchCell = currentRow.insertCell();
      batchCell.textContent = batchId;
      // 逐SKU匹配渲染库存状态
      const skuMap = availabilityRoot[storeId][batchId];
      allSkuList.forEach(sku => {
        const skuCell = currentRow.insertCell();
        const status = skuMap[sku];
        // 无对应SKU的批次显示占位符
        if (status === undefined) {
          skuCell.textContent = '-';
          return;
        }
        skuCell.textContent = status;
        // 状态值做颜色区分
        skuCell.classList.add(status === 'yes' ? 'stock-yes' : 'stock-no');
      });
    });
  });

  // 挂载到目标DOM容器
  document.querySelector(containerSelector).appendChild(table);
}

// 调用方法,和原有DOM结构匹配
jsonTabulizer(jsonData, '#test_table');

最终表格效果说明

  • 第一列:超市名称,通过rowSpan自动合并对应超市下所有批次的行,无重复渲染
  • 第二列:每个超市关联的独立库存批次ID,逐行展示
  • 后续列:聚合后的全量SKU品类,某批次无对应SKU时自动显示占位符-
  • 库存状态yes/no通过颜色做高亮区分,无需额外调整样式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.13 01:15:02