Author Topic: named function on format  (Read 1699 times)

lhohman

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 9
    • View Profile
named function on format
« on: May 21, 2020, 10:07:37 am »
Hello,

Im trying to pass a simple format and deFormat as named functions and they dont seem to work.
Code: [Select]
function pqFormatPercent(val) {
       return (val==null || val==="")? "": (pq.formatNumber(val * 100, "#,###.0") + "%");
}
function pqDeFormatPercent(val) {
       return (pq.deFormatNumber(val.split('%')[0], '#,###.00') / 100);
}

This is because my colModel comes from a JSON

Code: [Select]
"postpaid_sales": {
"align": "right",
"title": "Postpaid Sales",
"format": "pqFormatPercent",
"deFormat": "pqDeFormatPercent",
"dataIndx": "postpaid_sales",
"dataType": "float"
},

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: named function on format
« Reply #1 on: May 21, 2020, 10:52:17 am »
Please define your functions as object methods.

Code: [Select]
var my_obj = {
pqFormatPercent: function(val) {
   return (val==null || val==="")? "": (pq.formatNumber(val * 100, "#,###.0") + "%");
},
pqDeFormatPercent: function(val) {
   return (pq.deFormatNumber(val.split('%')[0], '#,###.00') / 100);
}
}

And run a loop on the colModel.

Code: [Select]
colModel.forEach(function(column){
column.format = my_obj[ column.format ] || column.format;
column.deFormat = my_obj[ column.deFormat ] || column.deFormat;
})