首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态单元格格式

问动态单元格格式
EN

Stack Overflow用户
提问于 2019-04-17 18:13:45
回答 3查看 1.7K关注 0票数 0

我想为行单元格和内置计算列单元格动态格式化货币总数单元格的样式。

例如,将货币符号、十进制符号、千分隔符更改为单元格。

下面的代码将一行单元格格式化为货币类型,以显示部分成本,包括货币符号、千和小数符号以及精度。同样也适用于底部计算单元。

代码语言:javascript
复制
{
  "title": "Partial",
  "field": "Partial",
  "width": 150,
  "align": "right",
  "formatter": "money",
  "formatterParams": {
    "decimal": ",",
    "thousand": ".",
    "symbol": "USD ",
    "symbolAfter": false,
    "precision": 2
  },
  "bottomCalc": "sum",
  "bottomCalcFormatter": "money",
  "bottomCalcFormatterParams": {
    "decimal": ",",
    "thousand": ".",
    "symbol": "USD ",
    "symbolAfter": false,
    "precision": 2
  }
}

这段代码工作得很好,但我无法获得文档来精确地更改货币符号、小数点和千位分隔符以及精确参数。

我希望通过让用户在表单中更改自己的参数来动态地更改这些参数。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-18 17:10:24

如果您想拥有动态货币,您应该在每次我建议的时候将其设置在上。

但是,如果您仍然希望在运行时更改它,可以使用Javascript进行更改,请参见下面的代码

代码语言:javascript
复制
    <!DOCTYPE html>
<html lang="en">

  <head>
    <link href="https://unpkg.com/tabulator-tables@4.2.4/dist/css/tabulator.min.css" rel="stylesheet">
  </head>

  <body>
    <div id="example-table"></div>
    <button id='changeCurrency'>
Change $ to £
</button>

    <script src="https://code.jquery.com/jquery-3.4.0.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.2.4/dist/js/tabulator.min.js"></script>

    <script>
      const tabledata = [{
      id: 1,
      name: "Oli Bob",
      money: "12",
      col: "red",
      dob: ""
    },
    {
      id: 2,
      name: "Mary May",
      money: "1",
      col: "blue",
      dob: "14/05/1982"
    },
    {
      id: 3,
      name: "Christine Lobowski",
      money: "42",
      col: "green",
      dob: "22/05/1982"
    },
    {
      id: 4,
      name: "Brendon Philips",
      money: "125",
      col: "orange",
      dob: "01/08/1980"
    },
    {
      id: 5,
      name: "Margret Marmajuke",
      money: "16",
      col: "yellow",
      dob: "31/01/1999"
    },
  ];

  const table = new Tabulator("#example-table", {
    height: 205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
    data: tabledata, //assign data to table
    layout: "fitColumns", //fit columns to width of table (optional)
    columns: [ //Define Table Columns
      {
        title: "Name",
        field: "name",
        width: 150
      },
      {
        title: "money",
        field: "money",
        align: "left",
        formatter: "money",
        bottomCalc: "sum",
        bottomCalcParams: {
          precision: 3
        },
        bottomCalcFormatter: "money",
        bottomCalcFormatterParams: {
          decimal: ".",
          thousand: ",",
          symbol: "$"
        },
        formatterParams: {
          decimal: ".",
          thousand: ",",
          symbol: "$"
        }
      },
      {
        title: "Favourite Color",
        field: "col"
      },
      {
        title: "Date Of Birth",
        field: "dob",
        sorter: "date",
        align: "center"
      },
    ]
  });



  $("#changeCurrency").click(function() {

    const field = 'money';

    $('[tabulator-field=' + field + ']').each(function() {
      const oldCurrency = $(this).text();
      if (oldCurrency !== field) {
        $(this).text(oldCurrency.replace('$', '£'));
      }


    });

  });
</script>


  </body>

</html>
票数 0
EN

Stack Overflow用户

发布于 2019-04-25 15:22:18

通过扩展formatters模块而不是使用"money"来创建自己的calc格式化程序类型。然后,一旦您设置了动态Tabulator.prototype.moduleBindings.format.prototype.formatters.money,就可以使用bottomCalcFormatterParams调用原始的money格式化程序实现。

票数 1
EN

Stack Overflow用户

发布于 2019-05-11 07:07:00

在通过GitHub和制表器文档花费时间之后,当用户在SELECT输入中更改货币值时,我可以找到一种非常迅速的方式来实现这种行为。

代码语言:javascript
复制
//get Currency Symbol from SELECT input
CurSymbol = $('#fldMoney').children('option:selected').val()+' ';
var i=0;
var colDefs = tabulatortable.getColumnDefinitions();

while(i < colDefs.length) {
    //compare which columns to change the currency symbol
    if (colDefs[i].title == 'UnitPrice') colDefs[i].formatterParams.symbol = CurSymbol;
        if (colDefs[i].title == 'PartialPrice') colDefs[i].formatterParams.symbol = CurSymbol;
        if (colDefs[i].title == 'PartialPrice') colDefs[i].bottomCalcFormatterParams.symbol = CurSymbol;

        i++;
};

//applies the new columns definition
tabulatortable.setColumns(colDefs); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55733684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档