Maths field stor in table

QuestionsMaths field stor in table
Gary Brett asked 10 years ago

Hi there, I am using a calculated field [not virtual field] in the grid on a total column, is it possible for this to be stored in table for reporting purposes?

$col = array();
$col["title"] = "total";
$col["name"] = "total";
$col["width"] = "30";
$col["editable"] = true;
$col["on_data_display"] = array("do_maths","");
function do_maths($data){
return $data ["amount"] * $data["adviser_split"]/100;}
$cols[] = $col;

Thank you

10 Answers
Gary Brett answered 10 years ago

Sorry Abu, I should mention that on insert this isn't added to table, if however I edit that row and save it does insert in the table?

Both insert & update functions are identical too.

Thank you

Gary Brett answered 10 years ago

Pastebin http://pastebin.com/pPVQdzPn if you have time

Cheers

Gary

Abu Ghufran answered 10 years ago

When you make this field editable, it will assume that there is a field 'total' in $g->table, and will try to insert data.
Alternate, You can make it editable=false, and do the maths again in on_insert or on_update event handlers.

e.g.
$data["params"]["total"] = $total * 200 / 123;

Gary Brett answered 10 years ago

Hi Abu, thank you once more. The total field is already set as editable and I do have a field in table called total, its odd as it does not insert on add but does on update even though that param isn't included on update function; I also commented out the $col["show"] to see if that helped but no joy..

$col = array();
$col["title"] = "total";
$col["name"] = "total";
$col["width"] = "20";
$col["editable"] = true;
$col["show"] = array("list"=>true, "add"=>false, "edit"=>false, "view"=>true);
$col["on_data_display"] = array("do_maths","");
//function do_maths($data){return $data ["received"] + $data["adviser_split"];}
function do_maths($data){return $data ["received"] * $data["adviser_split"]/100;}
$cols[] = $col;

Gary Brett answered 10 years ago

Hi Abu, just to note when editing the demo with this maths it also does not work, its only when I comment out maths and actually type a value does it store in table.

Thanks

Gary Brett answered 10 years ago

Also, it appears that on insert nothing is stored, but every time I update it inserts the last column value if thats makes sense?

Example;
Insert 10 * 10 = 0.00 in table
Update = 20 * 10 = 100 in table
Update = 30 * 10 = 200 in table
Update = 40 * 10 = 300 in table

Does that make any sense at all?

Thanks

Abu Ghufran answered 10 years ago

I think i found the issue …

In your function add_client(&$data)
{
$id = intval($_GET["rowid"]);
$data["params"]["invoice_id"] = $id;
$adviser = $_GET["adviser"];
$data["params"]["adviser"] = $adviser;
$data["params"]["total"] = $total; // < —————————–
}

The $total is undefined. So no matter whatever is posted from form, it is unset here.
If the field is posted on add/edit operations, you can just comment out this line.

This will insert the posted data in table.

Gary Brett answered 10 years ago

Hi Abu, I had tried that with no success unfortunately. I have just tried on the footer-row.php demo and the dame issue arises with no add/update functions.

Its the same issue where the total columns is not inserted until the save button his selected on that row, it then inserts the previous value only? If the user has no need to edit a row the value will never be inserted, if they do need= to edit then a previous value is inserted..

I have pasted my example footer-row.php here – http://pastebin.com/N8uijJDR

Line 89 I added the maths with simplified calculation..

Thanks

Gary

Gary Brett answered 10 years ago

Hi everyone, just wondering if anyone using the grid has managed to find a way to achieve the above, I've tried on many of the demos but just cannot get it to take value on insert?

Would appreciate anyones help who has this working, its pretty key to my project.

Thank you

Gary

Abu Ghufran answered 10 years ago

Hello Gary,

If you want to submit the calculated column, you will need to use on_insert / on_update events and perform the maths there.
The on_data_display event is just for display work and it never store presented data in database.

Working demo: http://pastebin.com/vELPw1vk

Commented line 92, Added 120.

Your Answer

11 + 18 =

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?