Reverse sign value of specific column based on value of other displayed column

QuestionsReverse sign value of specific column based on value of other displayed column
Gaëtan asked 8 years ago

Hi,
How can I do the following:
I have a table with various fields, amongst which:
operation (value C for Credit, D for Debit)
amount (always saved with positive value)

When retrieve data and displaying them, I would like the amount to be NEGATIVE when operation = D else positive.
Then I would use it for calculating correct running balance.

Thanks

3 Answers
Abu Ghufran answered 8 years ago

Hello,

One option is to use IF() condition in SQL (select_command) and make that value negative. IF(operation='D',amount*-1,amount)
Second option is to use on_data_display event handler and write you custom code to change values before they are displayed on grid.

You can refer demos/appearance/calc-column.php for help.

Gaëtan answered 8 years ago

Hi Abu,
I did actually use the following code:

$col = array();
$col["title"] = "Montant";
$col["name"] = "cbt_montant";
$col["width"] = "30";
$col["editable"] = true; // this column is not editable
$col["align"] = "center"; // this column is not editable
$col["search"] = true; // this column is not searchable
$col["hidden"] = false; // HIDE this column

$col["on_data_display"] = array("display_keyword","");

function display_keyword($data)
{
$op = $data["cbt_operation"];
$montant = $data["cbt_montant"];
if ($op == 'D') {$nouveau_montant = number_format(-$montant,2, '.',''); return $nouveau_montant;}
else {return $montant;}
}

$cols[] = $col;

Abu Ghufran answered 8 years ago

The on_data_display if set with column options only allow you to change that specific column.

The demo code i mentioned uses on_data_display that is grid event (not column) and allow to play with all data.

Please refer the code mentioned above and let me know for further help.

Your Answer

20 + 6 =

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?