q

const buildColumnsForTableDateRange = () => {
  const totalDate = getMonthsInRange(selectDate); // ['2026-01','2026-02','2026-03']
  const currentMonth = getCurrentMonth();         // '2026-03'

  const columns: any[] = [];
  // 月份 -> 分组列,保证同一个月份只创建一次
  const monthGroupMap = new Map<string, any>();

  // 懒创建月份分组列
  const getMonthGroup = (month: string) => {
    if (!monthGroupMap.has(month)) {
      const group = {
        title: month,
        key: `group-${month}`,
        align: 'center',
        children: [],
      };
      monthGroupMap.set(month, group);
      columns.push(group); // 放到主 columns 里
    }
    return monthGroupMap.get(month);
  };

  PROPOSAL_LIST.forEach((item) => {
    // 1. 非动态列:直接 push
    if (!item.dynamic) {
      columns.push({
        title: item.itemName,
        dataIndex: item.itemVar,
        key: item.itemVar,
        width: 130,
      });
      return;
    }

    // 2. 动态列:决定它该出现在哪些月份
    const visibleMonths = totalDate.filter((month) => {
      // 不支持「只当月」→ 每月都显示
      if (!item.currentMonthOnly) return true;
      // 支持,但用户没勾选 → 不启用限制 → 每月都显示
      if (!currentMonthOnlyChecked.has(item.itemVar)) return true;
      // 勾了 → 只在当前月显示
      return month === currentMonth;
    });

    // 3. 把这一列塞进对应月份分组的 children
    visibleMonths.forEach((month) => {
      const group = getMonthGroup(month);
      group.children.push({
        title: item.itemName,
        dataIndex: `${item.itemVar}_${month}`,
        key: `${item.itemVar}_${month}`,
        width: 100,
      });
    });
  });

  return columns;
};
posted @ 2026-09-29 14:42  小兔子09  阅读(3)  评论(0)    收藏  举报