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

如何在Tabulator JS中结合自定义格式化器与预定义格式化器

解决Tabulator自定义格式化器复用formatterParams的问题

你的自定义格式化器已经能接收到formatterParams参数(即函数的第二个参数),要实现复用money格式化器的逻辑+自定义样式,只需在自定义函数里调用Tabulator内置的money格式化器,传入对应参数即可。

修改后的完整代码如下:

new Tabulator("#data-table", {
    data: data,
    layout: "fitColumns",
    columns: [
        {
            title: "Length",
            field: "length",
            formatter: zeroValueFormatter,
            formatterParams: {
                thousand: " ",
                precision: false
            }
        },
        {
            title: "New length",
            field: "newLength",
            formatter: "money",
            formatterParams: {
                thousand: " ",
                precision: false
            }
        }
    ]
});

function zeroValueFormatter(cell, formatterParams) {
    // 调用内置money格式化器,复用formatterParams里的配置
    const formattedValue = Tabulator.prototype.formatters['money'](cell, formatterParams);
    
    // 按需求添加单元格样式
    if (cell.getValue() === 0) {
        cell.getElement().style.backgroundColor = "#add8e6";
    }
    
    // 返回格式化后的最终内容
    return formattedValue;
}

关键说明

  • Tabulator.prototype.formatters['money']是调用Tabulator内置格式化器的标准方式,其他内置格式化器也可以用相同方式调用
  • formatterParams会自动传入自定义格式化器函数,直接使用即可,无需额外配置
  • 先执行内置格式化逻辑再添加样式,能保证该列的数值格式和另一列完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 09:52:22