views:

133

answers:

1

I have a HTML form that mimics invoice input form. This is the way I load invoice items (users select them thru autocomplete list using jQeury):

    $(document).ready(function() {
        $("#Products").focus().autocomplete('<%=Url.Action("GetProducts", "Product") %>', {
            dataType: 'json',
            parse: function(data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i].product_name1, result: data[i].product_name1 };
                }
                return rows;
            },

            formatItem: function(row, i, n) {
                return row.product_PrettyId + ' - ' + row.product_name1 + ' (' + row.product_price + ' €) ';
            },
            width: 900,
            minChars: 0,
            max: 0,
            mustMatch: true
        });

        $("#Products").result(function(event, data, formatted) {
            if (data) {
                $(this).parent().next().find("input").val(data["product_id"]);
                $("#InvoiceItems > tbody").append(
                    "<tr>" +
                    "<td>num</td>" +
                    "<td>" + data["product_PrettyId"] + "</td>" +
                    "<td>" + data["product_name1"] + "</td>" +
                    "<td>" + data["product_price"] + "</td>" +
                    "<td></td>" +
                    "<td>1</td>" +
                    "<td>" + data["product_price"] + "</td>" +
                    "</tr>");
            }
        });

After each invoice item added I need to enumerate the table that I'm building - count the items, multiply price with quantity and sum all the items.

How can I do that?

+1  A: 

You might want to add some classes to specific tds for things like quantity, price, and with the total number of items, each time you add a new one you can update a counter and store it within a hidden field and read from that value

EDIT: wrap .each loop in function

function functionName() {
var itemCounter = 0;
$('#InvoiceItems tr').each(function() {
    itemCounter += 1;
    //this loops through each row in the table
    var quantity = parseFloat($('td.quantity', this).text());
    quantity = isNaN(quantity) ? 1 : quantity;//if a number isn't parsed, replace it with 1
    var price = parseFloat($('td.price', this).text());
    price = isNaN(price) ? 0 : price;
    var total = quantity * price;
    //do something with the total
});
}

you could call this function whenever a new item is added so the totals will always be up to date

Jimmy
Your code looks fine but I have problems with it. The line with the val() returns "undefined" and as such the total is NaN. What could be wrong?
mare
i will update the code with some NaN checks
Jimmy
you can also add validation checks on your inputs to ensure the values are numeric
Jimmy
Hey thanks, but the values are there and parseFloat still returns NaN. Actually the problem is in find() - it does not find anything. I tried just outputting the value and it is undefined. I have the classes specified. It seems that each() is working and it does find the added rows but find() does not find the <td>s
mare
Hey guess what, I had to replace val() with html(). Will that be ok?EDIT: No the .html() won't work because it picks up entire html markup that is contained in the cell. But it does pick it up, unlike val(), which is having some issues??
mare
I was using val() assuming in the cells were inputs, if that is not the case you can use the .text() method to get the text in the element, i updated the code to replace the .find() method, give that a whirl
Jimmy
Great, this works!yeah i need a deep dive into jQuery, until now I've been studiying it like this - example by example.i marked u as answer..thx
mare
thanks for the answer, also a great tool for learning more jQuery is the jQuerify bookmarklet + firebug in firefox and just destroying sites by playing around with them
Jimmy