Author Topic: Conditional Formatting of Currency or PCT  (Read 358 times)

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Conditional Formatting of Currency or PCT
« on: June 25, 2024, 05:09:48 pm »
I need to format a cell as currency or integer, based on the value of another column in the row.
I want to maintain the functionality that when the cell is selected to edit, the "$" is removed when editing the cell.

How would I do that?
« Last Edit: June 25, 2024, 05:33:43 pm by mikep »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #1 on: June 26, 2024, 11:08:27 pm »
you would use the column.render to conditionally format a cell depending on the value of another column.

Code: [Select]
render: function( ui ){
  if(ui.rowData.another_column_dataIndx == some_value ){
    //format as currency.
  }
  else{
    //format as number.
  }
}

and you can use the formatNumber for formatting: https://paramquery.com/pro/api#method-formatNumber

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #2 on: June 26, 2024, 11:34:01 pm »
Thanks, I got that far. What do I put for //format as currency?

"format: '$#,###' " is what I'm looking for, but this seems to only work when defining the column, but not during render. I need it during render, since its conditional based on the row.




paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #3 on: June 27, 2024, 03:07:25 pm »
You don't need to use column.format but call pq.formatNumber method with required format as parameter and return the formatted string from column.rernder

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #4 on: June 27, 2024, 04:31:31 pm »
Thank you. Can you correct this. the 2 options below dont work.

render: function( ui ){
  if(ui.rowData.another_column_dataIndx == some_value ){
    //format as currency.
    return { pq.formatNumber(1200, "$#,####")  }
  }
  else{
    //format as number.
   pq.formatNumber(1200, "#,####")
  }
}


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #5 on: June 28, 2024, 01:02:59 pm »
You need to substitute pseudo code with actual conditions i.e, another_column_dataIndx  is to be replaced with actual dataIndx

and you also need to return the formatted value in case of number.

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #6 on: June 28, 2024, 04:37:09 pm »
I understand that. I need the syntax to do the actual formatting in the render, the below 2 lines don't work.


return { pq.formatNumber(1200, "$#,####")  }

pq.formatNumber(1200, "#,####")

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #7 on: June 28, 2024, 04:50:15 pm »
That would be

Code: [Select]
return pq.formatNumber(1200, "#,####");

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #8 on: July 15, 2024, 09:07:56 pm »
Thank you. I also need to format the summary cell. I'm always getting the currency format


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6307
    • View Profile
Re: Conditional Formatting of Currency or PCT
« Reply #9 on: July 16, 2024, 10:50:45 am »
Same logic applies to summary cells. Please share a jsfiddle if facing issues.