Author Topic: Question about cell fromat  (Read 5442 times)

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Question about cell fromat
« on: July 22, 2014, 07:22:08 am »
Hi team,

I a problem about display value on paramquery table. I hope you can give me some suggestion.
I have a demand, we have a table to display some records and each column display the currency value. So the column type I use "float". Then our client have a demand, our system user from different countries, so they hope the value which display on the table can have the different currency format for different user location.

For example, the value I get is a string "1234.56"
If a user from US, I will format this value like "1,234.56".(Use comma format value)
If a user from Sweden, I will format this value like "1 234,56". (Use blank and comma format value)
If a user from Denmark, I will format value like "1.234,56". (Use dot and comma format value.)

So the problem is my column type is "integer/float", so if I format the value like the above format string, and use render function display them, I think the pggrid will auto format the string value to "integer/float" type, the some part for my value will lost.

Like
Code: [Select]
{ title: "Volume", width: 50, dataType: "integer",halign:"center",align: "right",dataIndx: "volume",
    render: function (ui) {
       return (renderTotalorBlank(ui))
    }                     
}


function renderTotalorBlank(ui){
    return ((ui.cellData > 0) ? commafy(ui.cellData) : '');
}
The "commafy()" function will format my value. And return a string.


I don't know whether I described clearly. I hope you can give me some suggestion. Actually, I don't want to change my column type to string, because I need to write some validation manually for a string cell.


Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6313
    • View Profile
Re: Question about cell fromat
« Reply #1 on: July 22, 2014, 07:57:46 am »
You need to specify dataType = 'float' for currency when you do sorting and filtering on the data, otherwise you can use string.

Assuming that you want to keep it float which is also recommended, there is a simple solution to this:

Store the float value in data e.g., 1234.56, add one more field in rowData

1) country if you want to do formatting on client side in javascript.

column.render = function(ui){
  return commafy( ui.cellData, ui.rowData['country']);
}

2) formatted value if you have already done formatting on the server side.

column.render = function(ui){
  return ui.rowData['formatted_value'];
}
« Last Edit: July 22, 2014, 08:04:07 am by paramquery »

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Re: Question about cell fromat
« Reply #2 on: July 22, 2014, 08:30:34 am »
You need to specify dataType = 'float' for currency when you do sorting and filtering on the data, otherwise you can use string.

Assuming that you want to keep it float which is also recommended, there is a simple solution to this:

Store the float value in data e.g., 1234.56, add one more field in rowData

1) country if you want to do formatting on client side in javascript.

column.render = function(ui){
  return commafy( ui.cellData, ui.rowData['country']);
}

2) formatted value if you have already done formatting on the server side.

column.render = function(ui){
  return ui.rowData['formatted_value'];
}

Hi,

Thanks for your response.
Now I use the first way return my format value and display them on my table. But I just remember a problem is the cell will auto remove the "0" for my return value when this value have decimal.
E.g.

Code: [Select]
column.render = function(ui){
  return commafy( ui.cellData);
}
The result after my format is "2 000.00", then the value display on the cell is "2 000", or if the result is "2 000.10", the value display on the cell is "2 000.1". The zero on decimal will be removed.
I think this caused by my column type. I use "float".

I also try to use "parseFloat(formatStr).toFixed(2)". But the new problem is if my format return string is "2 000.00", the result after this function "parseFloat(formatStr).toFixed(2)" will be "2", I think the js "parseFloat" wont treat this "2 000.00" as a whole integer. They can only identify the "2" before the blank.


Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6313
    • View Profile
Re: Question about cell fromat
« Reply #3 on: July 22, 2014, 08:58:16 am »
It seems you are mixing up the 2 different scenarios:

If you have pre formatted values coming from the server, then you should use 2nd method.

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Re: Question about cell fromat
« Reply #4 on: July 22, 2014, 10:40:46 am »
It seems you are mixing up the 2 different scenarios:

If you have pre formatted values coming from the server, then you should use 2nd method.

Hi,

About the value,
I pass a json string from server to my page. And the value in the json is double type. And my column type is "float", then the value can display normally.
At before I want to display value with two decimal on the cell, so I have the following method:

Code: [Select]
function render2Decimals(ui) {
        return((ui.cellData > 0) ? parseFloat(ui.cellData).toFixed(2) : '');
}
So the value on the pggrid table will display like "2000.00" or "2000.21".

Now I need to format the value as currency. So before I return the value, I did some operate about the value. Like the following code:

Code: [Select]
function render2Decimals(ui) {
    var formatValue = commafy(ui.cellData);       
    return formatValue;
}

By this code, the last "0" in decimal will be removed. When they display on cell. Like the format value is "2,000.10", will display as "2,000.1". So actually, I did the format in js function.

About your solution two
Quote
2) formatted value if you have already done formatting on the server side.

column.render = function(ui){
  return ui.rowData['formatted_value'];
}

As I understanding, the ui.rowData['formatted_value'], the "formatted_value" should be a dataIndx, not the value.

Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6313
    • View Profile
Re: Question about cell fromat
« Reply #5 on: July 22, 2014, 12:40:46 pm »
Noctrona

Quote
As I understanding, the ui.rowData['formatted_value'], the "formatted_value" should be a dataIndx, not the value.

Yes, it's the dataIndx of the preformatted value.

ui.rowData['dataIndx_of_preformatted_value']

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Re: Question about cell fromat
« Reply #6 on: July 22, 2014, 02:07:16 pm »
Noctrona

Quote
As I understanding, the ui.rowData['formatted_value'], the "formatted_value" should be a dataIndx, not the value.

Yes, it's the dataIndx of the preformatted value.

ui.rowData['dataIndx_of_preformatted_value']

Got it.

Thanks for your help  :)