views:

33

answers:

1

I used the Jquery calculation plugin successfully to perform two calculations on both subtotals and a grand total much like the last example on the demo page:

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

However I'd like to also get a total count of the number of beds that are selected but can't seem to figure out how to add that it as well, any ideas?

    function recalc(){
        //$("[id^=total_item]").calc(
        $("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_]")
            },
            // 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 count = $this.size(); <--- doesn't give me a correct count
                var sum = $this.sum();


                $("#totals").text(
                    // round the results to 2 digits
                    "You have selected " + count + " rooms for a total of <%=session[:symbol]%>" + sum.toFixed(2)
                );
            }
        );
    }
A: 

Nevermind, this seems to work:

    $(document).ready(
        function (){
            $("select[id^=room_]").bind("change", recalc);
        }
    );

    function recalc(){
        //$("[id^=total_item]").calc(
        var counter = $("select[id^=room_]").sum();
        $("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_]")
            },
            // 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 count = $this.size();
                var sum = $this.sum();

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

                $("#totals").text(
                    // round the results to 2 digits
                    "You have selected " + counter + " beds for <%= user_cart.getDays %> nights totaling <%=session[:symbol]%> " + (sum * <%= user_cart.getDays.to_i %>).toFixed(2)
                );
            }
        );
    }
holden