>I've done the following code in Coldfusion:
>
><cfset tilePrice = ListGetAt(productPricing.priceLevels,7)>
><cfloop from="1" to="#ListLen(productPricing.priceBreaks)-1#"
>index="i">
>       <cfif quantity GTE ListGetAt(productPricing.priceBreaks,i) AND
>quantity LT ListGetAt(productPricing.priceBreaks,i+1) >
>               <cfset tilePrice = ListGetAt(productPricing.priceLevels,i)>
>       </cfif>
></cfloop>
>
>Now, I'd like to do the same thing, but in Javascript / Jquery.  Can
>someone post code that will do this same sort of query/operation in
>Javascript?

I've been working (in my free time) on a Calculation plug-in:
http://www.pengoworks.com/workshop/jquery/calculation.plugin.htm

The interesting thing about the plug-in is that it will parse numeric values
from either form fields or from HTML element.

Take a look at the Qty/Product/Price/Total table. The code that hooks up the
Total column is:

// update the "total" column
$("[EMAIL PROTECTED]").calc(
        // the equation to calculate
        "qty * price",

        // define the variables
        {
                qty: $("[EMAIL PROTECTED]"),
                price: $("[EMAIL PROTECTED]")
        },

        // the formatting callback
        function (s){
                return "$" + s;
        },

        // the finish callback
        function ($this){
                // sum the total of the $("[EMAIL PROTECTED]") selector
                var sum = $this.sum();
                
                $("#grandTotal").text(
                        // round the results to 2 digits
                        "$" + Math.round(sum*100)/100
                );
        }
);

When you build your equations you define your variables in an object that
with the var names being keys in the object. The keys can be either a valid
jQuery object, array of values, static value or another variable.

If the variables are an array and their array length is the same length as
the main selector in the calc() method--in this case
$("[EMAIL PROTECTED]")--then it will use the correlating array index values,
otherwise it'll just use the first value in the array.

This is just something I've been playing with. I want to add event handlers
to any of the elements that are input elements so that changes to the
calculations would be reflected in real time.

There's also some aggregation functions (sum & avg) built in to this plug-in
as well.

-Dan

Reply via email to