如何在 Tabulator JS 中将自定义格式化程序与预定义格式化程序结合起来?
P粉676588738
P粉676588738 2023-09-13 15:40:50
[JavaScript讨论组]

我使用 Tabulator JS,并且有 2 列包含数字数据。首先,我使用了带有 formatterParams 参数的内置 money 格式化程序,对于第二个,我想应用完全相同的、具有相同参数的,但还要添加,例如,单元格的颜色。如何以自定义格式应用formatterParams

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 /* ? */) {
    if (cell.getValue() === 0) {
        cell.getElement().style.backgroundColor = "#add8e6";
    }    
    return cell.getValue();
}

P粉676588738
P粉676588738

全部回复(1)
P粉592085423

一种选择是使用 rowFormatter 并将格式应用于您需要的特定列。在您的示例中,是 length 列。这样,您可以在两列的列设置级别使用内置的 money 格式化程序,但在行级别应用其他格式。这是一个例子:

const data = [
  { length: 0, newLength: 10000 },
  { length: 2000, newLength: 20000 },
  { length: 3000, newLength: 0 }
]

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

function zeroValueFormatter(row) {
  const rowData = row.getData()

  if (rowData['length'] === 0) {
    row.getCell('length').getElement().style.backgroundColor = "#add8e6";
  }

}
<link href="https://unpkg.com/tabulator-tables@5.5.1/dist/css/tabulator.min.css" rel="stylesheet" />
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@5.5.1/dist/js/tabulator.min.js"></script>
<div id="data-table"></div>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号