• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
丶菜鸟也会飞
博客园    首页    新随笔    联系   管理    订阅  订阅
handsontable问题总结篇

表单滚动条滚动不到最底下

描述:表单有58行,但是滚动条只能滚动到33行。
原因:行高不一致,导致计算不准确
解决办法:rowHeights: 25, // 统一行高,确保计算准确,初始化定义即可,在其他地方不要再赋值

对单元格数据进行格式化,但是会无限循环导致页面卡死

解决办法:// 这里必须写后面的"format",不然会不停循环更新单元格
this.hot.setDataAtCell(
row,
col,
Number(newValue).toFixed(2),
"format"
);

点击查看代码
afterChange(changes, source) {
      changes?.forEach(([row, col, oldValue, newValue]) => {
        // if (
        //   typeof this.currentCellVal === "string" &&
        //   this.currentCellVal.match(/^[<>!=]+\s*\(.*\)$/)
        // ) {
        // 改成右键菜单定义配置
        console.log('source', source);

        if (source === "edit") {
          if (this.customizeCheckData &&
            this.customizeCheckData[row + "-" + col] && typeof this.customizeCheckData[row + "-" + col]?.split('compare-')[1] === "string" &&
            this.customizeCheckData[row + "-" + col]?.split('compare-')[1].match(/^[<>!=]+\s*\(.*\)$/)
          ) {
            let compareFomrula = this.customizeCheckData[row + "-" + col]?.split('compare-')[1]
            console.log('compareFomrula=', compareFomrula)
            this.parseFormula(compareFomrula, row, col, Number(newValue).toFixed(2));
            // 这里必须写后面的"format",不然会不停循环更新单元格
            this.hot.setDataAtCell(
              row,
              col,
              Number(newValue).toFixed(2),
              "format"
            );
          }

        }
      });

由于修改了表单横向滚动条高度,导致表单数据错行

解决办法:取消滚动条样式修改

设置最大列,可以避免复制粘贴新增列,maxCols

单元格内置日期插件

点击单元格 显示日期插件,直接选取时间。

点击查看代码
hotsettings:{
cells: (row, col, prop) => {
        const cellProperties = {};
        if (this.customizeData?.[row + "-" + col]?.includes("date-")) {
          // 配置日期编辑器(Handsontable 自动用 new 实例化)
          cellProperties.editor = "date"; // 内置日期编辑器,无需手动调用
          cellProperties.dateFormat = "YYYY-MM-DD"; // 编辑后显示格式
          cellProperties.correctFormat = true; // 自动修正输入格式
          // 配置日期渲染器
          cellProperties.renderer = this.dateRenderer;
          // 日期校验(可选,确保输入有效)
          cellProperties.validator = this.dateValidator;
        }
        // else {
        //   // 非日期单元格:默认文本编辑器和渲染器
        //   cellProperties.editor = "text";
        //   cellProperties.renderer = Handsontable.renderers.TextRenderer;
        // }

        return cellProperties;
      },
}

// 自定义日期渲染器:判断单元格是否为日期类型,若是则格式化
   export function dateRenderer(instance, td, row, col, prop, value, cellProperties) {
      // 调用默认渲染器,避免样式丢失
      Handsontable.renderers.TextRenderer.apply(this, arguments);
// let customizeData= sessionStorage.getItem("customizeData")
      // 检查单元格是否标记为日期类型
      // if (customizeData?.[row + "-" + col]?.includes("date-")) {
        // 格式化日期(使用 Moment.js 或原生 Date 处理)
        const date = new Date(value);
        if (!isNaN(date)) {
          // 确保是有效日期
          td.textContent = `${date.getFullYear()}-${String(
            date.getMonth() + 1
          ).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
        }
        return td
      // }
    }
	// 日期校验
export function dateValidator(value, callback) {
  if (!value) {
    callback(true);
    return;
  }
  // 检查是否为有效日期
  const isValid = moment(value, 'YYYY-MM-DD', true).isValid();
  callback(isValid);
}
posted on 2025-11-13 17:39  丶菜鸟也要飞  阅读(0)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3