首页 > 解决方案 > 如何使用 jquery Devextreme 在 cellTemplate 中格式化数字?

问题描述

我想自定义我的借方和贷方列。当有数量时,它会变成blue color,当点击它时,它会show pop up。如果是amount is zero那么它将是blank。问题是当我使用 cellTemplate 自定义单元格时,customText 和格式不起作用。有人对此有想法吗?请看我的JS Fiddle

起初,通过使用它,我能够使零值变为空白(见借方列)

{
  dataField: "debit",
  caption: "DEBIT",
  dataType: "number",
  width: 150,
  format: "#,##0.00;(#,##0.00)",
  customizeText: function(cellInfo) {
    if (cellInfo.valueText === "0.00") {
      return " ";
    } else {
      return cellInfo.valueText;
    }
  },
}

添加 cellTemplate 后,customText 和格式不起作用。金额格式不正确,零也没有变成空白。(见学分栏)

{
dataField: "credit",
  caption: "CREDIT",
  dataType: "number",
  width: 150,
  format: "#,##0.00;(#,##0.00)",
  customizeText: function(cellInfo) {
    if (cellInfo.valueText === "0.00") {
      return " ";
    } else {
      return cellInfo.valueText;
    }
  },
  cellTemplate: function(container, cellInfo) {
    var credit = cellInfo.data.credit;

    if (credit !== "0.0000") {
      var color1 = "blue"
    }
    $('<a/>').addClass('dx-link')
      .text(cellInfo.data.credit)
      .css("color", color1)
      .on('dxclick', function() {
       $("#popup").dxPopup("instance").show();
$("#txt").dxTextArea("instance").option("value", cellInfo.data.credit);
      })
      .appendTo(container);
  }
}

我当前的输出

电流输出

标签: devextreme

解决方案


当你使用cellTemplateandformatcustomizeText被忽略。使用时,cellTemplate您必须手动格式化值。


推荐阅读