views:

13

answers:

0

Extending the example here:

http://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm

How could I generate the subtotals, and additionally add in the Name of the product selected? Instead of:

Qty            Product              Price    Total
1              Learning jQuery      $39.99  $39.99
1              jQuery Donation      $14.99  $14.99
Grand Total:                                   $54.98

Have tabulated below it a line item like so:

Learning jQuery $39.99 x 3 = $119.97
jQuery Donation $14.99 x 3 = $45.97

Total

    function recalc(){
        //$("[id^=total_item]").calc(
        var beds = $("select[id^=room_]").sum();
        var nights = <%= user_cart.getDays.to_i %>
        var arrival = <%= user_cart.getDate.to_date.to_s %>
        var name = $("div[id^=name_]").val()

        $("div[id^=subtotal_]").calc(
            // the equation to use for the calculation
            "qty * price",
            // define the variables used in the equation, these can be a jQuery object
            {
                qty: $("select[id^=room_]"),
                price: $("div[id^=price_]")
                //name: $("div[id^=name_]")
            },
            // define the formatting callback, the results of the calculation are passed to this function
            function (s){
                // return the number as a dollar amount
                return "<%=session[:symbol]%> " + s.toFixed(2);
            },
            // define the finish callback, this runs after the calculation has been complete
            function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var sum = $this.sum();
                var total = sum * nights
                var texto = name + "[room name] for " + beds + " people total " + total + "."
                //var avg = sum;

                if (sum > 0) { $("#totals").fadeIn(); } else { $("#totals").fadeOut(); }

                $("#totals").html(
                    // round the results to 2 digits
                    "You have selected " + beds + " beds for "+ nights + " nights totaling <br /><span><%=session[:symbol]%> " + (sum * nights).toFixed(2) + "</span> <small>(<%=session[:symbol]%> " + (sum / beds).toFixed(2) + " avg. per night)</small>"
                );

                $("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI(texto));


            }
        );
    }