how to show the total of the onchange?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
-Abubakr-
User
Posts: 7

how to show the total of the onchange?

Post by -Abubakr- »

Hello dear people I'm new to phpmaker.
Problem how to show the totals of the onchange?
Beforehand Thanks.

-------------Example--------------
1) add onchange event by using Row_Rendered server event that passes the element and row index to the MyFunction function
$this->quantity->EditAttrs["onchange"] = "MyFunction.call(this, " . $this->RowIndex . ");";
2)add a JavaScript MyFunction in Start-up Script Client event


tion MyFunctionAdd(rowindex) {
var form = this.form;
var quantity = form.elements["x" + rowindex +"quantity"];
var price = form.elements["x" + rowindex +"
price"];
sum.value = quantity.value * arzish.value //All this work

_Namequantityprice Sum
1 Lemon 1 1$ 1$
2 banana 2 1$ 2$
3 apple 2 1.5$ 3$
totals: 6$___(Problem)


danielc
User
Posts: 1601

Post by danielc »


bksurik
User
Posts: 31

Post by bksurik »

here a little differently (See this topic: http://www.hkvforums.com/viewtopic.php?f=4&t=35979)
give more examples.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

My code works


function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
if ($this->IsGridAdd()){
$this->quantity->EditAttrs["onchange"] = "MyFunctionAdd.call(this, " . $this->RowIndex .");";
}
}

Client script


// Write your global startup script here
// document.write("page loaded");
function MyFunctionAdd(rowindex) {
var form = this.form;
var quantity = form.elements["x" + rowindex +"quantity"];
var price = form.elements["x" + rowindex +"
price"];
var Sum = form.elements["x" + rowindex +"_Sum"];
Sum.value = quantity.value * price.value;

}
_______Table_sale___
|____________________|
|name |quantity|price|Sum|
|-------|----------|------|-----|
|banan|
1$|1$|1$|
|-------|----------|------|-----|
|Limon|2$|1$|2$|
|-------|----------|------|-----|
|apple|
2$|1.5$|3$|
|------|-----------|-------|---- |
|RESULT SUM
TOTAL: 6$|
|
_________________|
Onchange
(Here this problem needs to be solved_RESULT SUM
_____TOTAL: 6$)???

Offer solutions.


danielc
User
Posts: 1601

Post by danielc »

The code you posted is for the row only. Your onchange event calculates the row sum but there is no code to show the result(total) row and calculate the total. (Assume you are using Grid-Add/Edit.)

Write your code and post for discussion if it does not work.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

(Yes, you are rightly, I use Grid-Add / Edit.)

I do not know how and where to write code?
I need in your help.


danielc
User
Posts: 1601

Post by danielc »

You can add your client script to Client Scripts->Table-Specific->List Page->Startup Script. See Server event and Client script in help file.

Refer to jQuery change event (see api.jquery.com/change/) how to attach change event to a field. View HTML source to find out the element ID. Then try to write your code and post your code for discussion.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

--------added a new field-------result_sum--------

Client script


// Write your global startup script here
// document.write("page loaded");
function MyFunctionAdd(rowindex) {
var form = this.form;
var quantity = form.elements["x" + rowindex +"quantity"];
var price = form.elements["x" + rowindex +"
price"];
var Sum = form.elements["x" + rowindex +"_Sum"];
Sum.value = quantity.value * price.value;
var result_sum = form.elements["x_result_sum"]; // my code does not work
x1_result_sum.value = 0;
x1_result_sum.value = sum.value + x1_result_sum.value;
}
_______Table_sale___
but must be
|_________________________________
|name |quantity|price|Sum|result_sum|
|-------|----------|------|-----|------------|
|banan|1$|1$|1$|3$| //The last value of the sum
|-------|----------|------|-----|-------------|//but must be 6$
|Limon|
2$|1$|2$|___|
|-------|----------|------|-----|-------------|
|apple|
2$|1.5$|3$|______|
Thank you for any help


danielc
User
Posts: 1601

Post by danielc »

Actually, you just sum up for row 1. You need to recalculate the total sum once if there is any change in the input field.

If you add a field result_sum, you need to find out the total number of rows (use jQuery .length, see api.jquery.com/length/) and need to exclude the template row (assume there is no delete of one row in gridadd page) and sum up every row (exclude empty value) and put your sum into your desired column (use jQuery .val, see http://api.jquery.com/val/). View HTML source to get element id.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

Client script


// Write your global startup script here
// document.write("page loaded");
var x=0; // global variable
function MyFunctionAdd(rowindex) {
var form = this.form;
var quantity = form.elements["x" + rowindex +"quantity"];
var price = form.elements["x" + rowindex +"
price"];
var Sum = form.elements["x" + rowindex +"Sum"];
Sum.value = quantity.value * price.value;
var result_sum = form.elements["x1_result_sum"];
x = quantity * price + x; //the result_sum of change quantity.
result_sum.value =x;
}
______Table_sale___
|_________________________________
|name |quantity|price|Sum|result_sum|
|-------|----------|------|-----|------------|
|banan|1$|1$|1$|3$| //The last value of the sum
|-------|----------|------|-----|-------------|//but must be 6$
|Limon|
2$|1$|2$|___|
|-------|----------|------|-----|-------------|
|apple|
2$|1.5$|3$|___|
and now it's every time we change the quantity field
_changing the values of the result_sum???
-------------------------------------example---------------------------------------
when change the quantity from 3 to 2 (price 1$) result_sum = 5$? "but must be 2$".


danielc
User
Posts: 1601

Post by danielc »

Assume this is the case:
row 1 x1_qty ... ... x1_resultsum
row 2 x2_qty ... ... x2_resultsum
row 3 x3_qty (delete this row)
row 4 x4_qty ... ... x4_resultsum

The total number of row is 3 but the maximum index will be 4 (assume user may delete one row in gridadd). When user enter value in x1_qty or x2_qty (user may not enter in order), you need to get the maximum index of row. You need to calculate the sum of other row as well and perform the calculation:
resultsum = x1_qty * x1_price + ... + x4_qty * x4_price

Use jQuery .val to put your resultsum to your desired location. Hope I make the concept clear.


strustam
User
Posts: 34
Location: Tajikistan

Post by strustam »

danielc, thanks for good and working solution,
one more question, is it possible identify how many rows (count) there are after add/delete at gridadd, and use loop for/while instead of
resultsum = x1_qty * x1_price + ... + x4_qty * x4_price
thanks in advance


danielc
User
Posts: 1601

Post by danielc »

strustam wrote:
one more question, is it possible identify how many rows (count) there are after
add/delete at gridadd, and use loop for/while

danielc wrote:
you need to find out the total number of rows (use jQuery .length, see api.jquery.com/length/)

Use jQuery .length to count the total number of rows but you still need to keep track the maximum number of index so we will know if there is any row deleted. You can use loop to calculate the sum. View HTML source and you will find attribute data-name="yourfieldname" for the column.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

how to do when you delete a row changed value of the sum?


danielc
User
Posts: 1601

Post by danielc »

If user is allowed to delete any row, you should attach a click event to the delete button to trigger the recalculation of sum. View HTML source to find out the element id.


-Abubakr-
User
Posts: 7

Post by -Abubakr- »

User is allowed to delete any row.
And now how to attach a click event to the delete button to trigger the recalculation of sum?
thanks for any help your.


Post Reply