formatoptions VS custom formatter and footerrow

Questionsformatoptions VS custom formatter and footerrow
Massimo Gagliardi asked 9 years ago

Hi Abu
I use this definition, in a "price" column, to customize the currency:

$col["formatter"] = "function(cellvalue, options, rowObject){ return formatPrice(cellvalue, options, rowObject);}";

and this javascript fragment to return my desired currency value:

function formatPrice(a,b,c) {
if (a!='') {
var n = nv = '';
if (a.substr(0, 1) == '-')
n = '-';
var v = a.replace(/[^d]/g,'').replace(/^0+/g,'');
var d = v.substr(v.length – 2);
v = v.substr(0, v.length – 2);
while(v.length > 3) {
nv = '<?php echo $GLOBALS['language']['thousandsSeparator']; ?>' + v.substr(v.length – 3) + nv;
v = v.substr(0, v.length – 3);
}
var r = v + nv + '<?php echo $GLOBALS['language']['decimalSeparator']; ?>' + d;
return n + ' ' + c.ID5 + ' ' + r;
}
else
return a;
}

where $GLOBALS['language']['thousandsSeparator'] contains the localized thousands separator, $GLOBALS['language']['decimalSeparator'] the localized decimal separator and ID5 the currency symbol ('$' or '€').

However with this approach it's impossible to use the footer row to get the sum of the "price" columns because, also if you don't filter the result (a!='') you have an error (a.substr is not a function) and if you use the filter then you get 0.
There is a solution?
Thanks in advance
Massimo Gagliardi

8 Answers
Abu Ghufran answered 9 years ago

When setting footer row, you can disable formatter like:

// 4th arg value of false will disable the using of formatter
grid.jqGrid('footerData','set', {………….}, false);

This way, it will not use formatter in footer row and you can formatting manually in footer.

Second, you also need unformat function when you use formatter, that return plain value (non formatted)

e.g.
$col["unformat"] = "function(cellvalue, options, rowObject){ return parseFloat(cellvalue.replace('$','').trim());}";

See attached demo code.
http://pastebin.com/0mLZe40v

Massimo Gagliardi answered 9 years ago

Thanks for demo code.
It's good but I want also the thousands delimiter on the totals row and it's seems impossible because I can't attach a function.

Abu Ghufran answered 9 years ago

you can call custom function in footer, as in above demo code:

grid.jqGrid('footerData','set', {id: 'Total: $ ' + sum.toFixed(2), invdate: 'Sub Total: '+sum_running, total: 'Grand Total: '+sum_table}, false);

Instead of sum.toFixed(2) you can call your function to add thousand sep.

Massimo Gagliardi answered 9 years ago

Hi Abu.
I tried.
This is my code:

function do_onload_dettaglio(id) {
var grid = $("#list2");
var sum1 = grid.jqGrid('getCol', 'Imponibile', false, 'sum');
var sum2 = grid.jqGrid('getCol', 'Iva', false, 'sum');
var sum3 = grid.jqGrid('getCol', 'Totale', false, 'sum');
var selr = $("#list1").jqGrid('getGridParam','selrow');
if ((selr > 0) && (sum1!=0 || sum2!=0 || sum3!=0)) {
sum1 = formatPrice(sum1, '', selr);
sum2 = formatPrice(sum2, '', selr);
sum3 = formatPrice(sum3, '', selr);
grid.jqGrid('footerData','set', {Descrizione: '<?php echo $GLOBALS['language']['totals']?>: ',
Imponibile: sum1, Iva: sum2, Totale: sum3}, false);
}
else
grid.jqGrid('footerData','set', {Descrizione: '<span color=white> </span>', Imponibile: '<span color=white> </span>', Iva: '<span color=white> </span>', Totale: '<span color=white> </span>'});
}

I use the same function (formatPrice, in the previous message) used for prices formatting in the rows but Firebug shows this error:
TypeError: a.substr is not a function
http://localhost:81/hmr/documentivendita.php?hw=493&ww=1337
Line 167

I don't understand
Thanks in advance
Massimo Gagliardi

Abu Ghufran answered 9 years ago

Hello,

It's your custom JS code, and not specific to phpgrid.
you can try deubugging with alert() statement or console.log() functions.

Massimo Gagliardi answered 9 years ago

Hi Abu
2 things:
1st: I put an alert before the instruction that causes the error ('a.substr is not a function') to see the value of a; contains 38.5, that seems a correct value, but right after the script abort by error. ??? I don't understand.

2nd: removing the pair of instructions:

$col["formatter"] = "function(cellvalue, options, rowObject){ return formatPrice(cellvalue, options, rowObject);}";
$col["unformat"] = "function(cellvalue, options, rowObject){ return parseFloat(cellvalue.replace('$','').trim());}";

and restoring the standard:

$col["formatter"] = "currency";
$col["formatoptions"] = array(…

the behavior of the rows with radio box in inline modal (see my other ticket) seems almost good.
Look at the other ticket to explanations.
Thanks

Abu Ghufran answered 9 years ago

Please email code and database sample data to regenerate this issue.
It's difficult to analyse the issue with code snippets.

You can email at [email protected]

Abu Ghufran answered 9 years ago

If default currency formatter is working, ignore my last message.

Your Answer

20 + 8 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?