Total of checked items (Selected Items) from list

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

Total of checked items (Selected Items) from list

Post by rajeev_rattra »

Hi,

I want to know whether it is possible that I can 'total of' only checked items from list?

Hope my question is clear.

For example, I have list of 10 items and customer wants to buy only 3 from that list. So bottom of my page should display on total of three and not of all.

If there is any example in demo project (or code) please share.

Please advise.

Regards


arbei
User
Posts: 9355

Post by arbei »

Are you talking about Aggregate Value (e.g. TOTAL) in the List page? It is sum of all rows in the page, it is calculated on the server side, it does not know which records are selected on the client side yet.

To change the value you can re-calculate it on the client side with your own code. For example,

  1. Add your checkbox as a ListOptions by adding code in "ListOptions_Load" and "ListOptions_Rendered" Server Event.

For example:
$opt = &$this->ListOptions->Add("new");
$opt->Header = "header";
$opt->ShowInButtonGroup = FALSE;

(ListOptions_Rendered)
$this->ListOptions->Items["new"]->Body = "<input type='checkbox' id='mycheckbox' data-index='" . $this->RowCnt . "'>";

  1. In Startup Script, subscribe the click event of the above checkbox, calculate the selected records with the data-index attribute, which is the same value as the row index of the records.

  2. Update the calculated value to the "Total" by .val() or .html() function of jQuery function.


rajeev_rattra
User
Posts: 60

Post by rajeev_rattra »

Thanks for reply.

I want something similar to purchase page of PHPMaker.

http://www.hkvstore.com/purchase.asp?prodid=11

I don't think it has to do some thing with server side.

Please advise.

Regards


kirondedshem
User
Posts: 642

Post by kirondedshem »

I would recommend using the normal master and detail add wth the detail page having an option to select the product and input the quantity, if u just want to add thing to an order since it can auto impliment edit for you etc etc.

***BUT if you really really really want it that way, then a manual way is all i see for now:

NOTE: Ive not tested this, you still have to handle saving part when user submits but am sure it can help you draw the list as seen from your example, I could not find time to finish everything even the sql bit since I'll have to craete a separate test phpmaker project for it, so this is all for now.

I would draw the whole thing manually since I would like to populate the whole list of dynamic products as well as attach scripts to allow updating the total column at the bottom.say you have a master table called order and a detail table called order_detail, you want to show all products on a list with a quantity input, then on submit you will loop through the list and determine those that have quantities,let phpmaker add the order record then on row inserted insert appropriately into the detail table of that order.
Assume all your products and thier unit prices are in the product table.

  1. customise the order add page to also draw a list of all products with input boxes for quantity.
    got to custom add template, put this and edit to fit your needs

<script>
//write functions to help auto update the total column

//called everytime quantity of any item in the list is changed
function RecalculateTotals()
{

//loop through the list of all products in the list.
//get avlues for quantity and price and do the math
//set the total contents after

//use the value in total_rows input box to loop throught all the products on the list

}

</script>

<table>
<tr>
<!-- darw the custom fields for the order add page you want user to fill up -->
</tr>

<!-- draw the list of products as well as quantity input boxes -->
<tr>

<table>
<!-- draw heading for the table -->
<tr>
<td>
Prduct Name
</td>
<td>
Unit Price
</td>
<td>
Quantity
</td>
</tr>

<!-- Loop through all products and draw them in the list -->

<?php
$TheQuery = "select id,product_name,unit_price from product";
$all_products_result = ew_Execute($TheQuery) or die("error during: ".$TheQuery);
$rows_products = $all_products_result->GetRows();

$row_count = 1;
foreach ($rows_products as $rec_product )
{

?>

<tr id = "<?php echo 'pdt_row'.$row_count; ?>">
<td id = "<?php echo 'pdt_name
'.$row_count; ?>">
<?php echo $rec_product["product_name"]; ?>
</td>
<td id = "<?php echo 'pdt_price'.$row_count; ?>">
<?php echo $rec_product["unit_price"]; ?>
</td>
<td>
<?php
//draw the quantity and attach scripts to help update the total column
$input_id = 'pdt_qty
'.$row_count;
$the_price = $rec_product["unit_price"];
echo '<input onchange="RecalculateTotals();" name="$input_id" id="$input_id" size="15" maxlength="100" placeholder="Qty" value="" class="form-control" type="number">';
?>
</td>
</tr>

<?php
}//close loop for products
?>

<!-- draw total column -->
<tr>
<td>
Total
</td>
<td id="total_cost">Total Cost</td>
<td id="total_quantity">Total Quantity</td>
</tr>

</table>

<?php
//keeps the total of all items shown on the list to help loop throught the list
echo '<input name="total_rows" id="total_rows" size="15" maxlength="100" placeholder="Qty" value="<?php echo $row_count ?>" class="form-control" type="number">';
?>

</tr>
</table>


digitalphotoworld
User
Posts: 416
Location: Nürnberg/Germany

Post by digitalphotoworld »

For your intention you have to use javascript. Only with javascript you can get results "on the fly".

If you look at the sourcecode of the purchase site from hkvstore you will see that there is an onchange() at each quantity-field that calls a function that is updating the price. Take a closer look at this function to understand how to get the values and prices from the fields.


kirondedshem
User
Posts: 642

Post by kirondedshem »

Might be too late, but i found myself with a similar problem and after tackling it,i though i should share the solution. I was able to use this on master/detail add as well as normal grid add forms. the hard part is detrmining the grand total but the rest is basic stuff

In my case I have the following tables
table(1) product:id,name
table (2)sale_detail:id,product_id,quantity,price,total_amount

I wanted to use the gridadd page of sale_detail table to:

  1. initialise it with all products in system,
    2.allow user to put type in quantity & price for any of the rows, and i use that to calculate the total_amount on each row.
    3.Be able to display the sum of all total amounts(net_amount) somewhere else everytime the total_amount of any of the rows changes.

So here goes:

  1. setup product_id in sale_detail to use lookup table into product value_field=id and display field = name, make it text/select
    2.Enable grid add otpion in table settings to show the grid add button.

  2. setup the gridAddrowcount of the sale_detail table to draw the exact number of orws equal to number of products.
    Go to page load event of the sale_detail list page and paste
    function Page_Load() {
    //set count to number of products
    $this->GridAddRowCount = ew_ExecuteScalar("select count(*) from product");
    }

  3. For each row in sale_detail set the a product_id as default selected . if lookup settings are correct each row shoud display the actual product name instead of the id
    got to Row_Rendering() event of the sale_detail and paste.
    function Row_Rendering() {
    // Enter your code here

//incase its grid add, you also change it to work if its master detail
if($_GET["a"] == "gridadd")
{
$grid_count = $this->GridAddRowCount;
$grid_num = $this->GridCnt;
if(isset($this->RowIndex))
{
$grid_num = $this->RowIndex;
//only when we are dawing actual rows
if(($grid_count >= $grid_num) && is_int($grid_num) && ($grid_num >= 1))
{
$offseter = $grid_num - 1;
//get & set product details
$product = ew_ExecuteRow("SELECT id FROM product limit 1 OFFSET $offseter");
$this->product_id->CurrentValue = $product["id"];

//hide grid delete
$this->ListOptions->Items["griddelete"]->Visible = FALSE;

}

}
}

}

5.Attach scripts to auto re calculate the total_amount for every row when quantity or price has changed, got to field settings of quantity&price in sale_detail->client side events and put
{ // keys = event types, values = handler functions
"keyup": function(e) {
// Your code

	var $row = $(this).fields();

	var total_amount = $row["quantity"].toNumber() * $row["price"].toNumber();
	//update total amount for this row
	if((total_amount > 0) || (total_amount < 0))
	{$row["total_amount"].value(total_amount);}
	else
	{$row["total_amount"].value("0");}

            //recalculate net amount
            CalculateNet();

	
}

}

NOTE: notice the function "CalculateNet()" it is the one responsible for recalculating the net amount so we call it after we are sure total_amoutn on a given row has changed

  1. put the CalculateNet() function in client scripts->table specific->add section so that its available to be called. SO paste the function as below
    NOTE: if you inspect your a total_amount input field you'll see is has certain properties id="x[n]_total_amount" where n is the row number, class="form-control" and data-field="x_total_amount", so we just have to look for these controlls and get thier values and sum them up

function CalculateNet(){
var net_amount = 0;
var all_amounts = $('[data-field="x_total_amount"]');
for(var i = 0;i < all_amounts.length;i++){
var the_amount = 0;
//ensure that its the input controll of interest
if(all_amounts[i].className == "form-control"){
the_amount = all_amounts[i].value;
if(!(the_amount > 0)){the_amount = 0;}
net_amount += parseFloat(the_amount);
}
}
//show the net amount anywhere for now am putting in console log
console.log(net_amount);
}

  1. if you've done it correctly you should see the net_amount in console log change every time you change the qauntity/price on any row. now you just have to draw a row below to show it or something.

Post Reply