0

I have seen a few examples for jquery and adding column values; however, the examples had only one column and one value.

I wanted to know if you had multiple columns then how do you grab specific ones?

For example, if I want to add all of the Quantity columns together or multiply Cost column with its adjacent Quantity?

So far I have the beginning set up but it doesn't work. Initially I tried

 child_nodes.eq(2).text() 

to get the column of quantity but I know my logic is off.

$(document).ready(function() {

$('#checkout').on('click', function() {

        item_count();
});
});

function item_count(){

    $('.product').each(function() {

        //find number of child nodes
        var child_nodes = $(this).text(); //children returns all

        var count = "";
        for (var i=0; i<child_nodes.length; i++){
        //whole row output
        count += child_nodes.eq(i).text() + "\n";
        }
    });

    //var total = parseInt($('.product').val());
    var total += parseInt(count);
    //update value
    return $('#nitems').text(total);

};

I just started trying j query so I feel like my code looks very noobish so I analogize.

<h4>Shopping Cart</h4>
 <table class='table'>
  <thead>
    <tr>
      <th>Product</th><th>Unit cost</th><th>Quantity</th>
    </tr>
  </thead>
  <tbody>
    <tr class='product'>
      <td>Milk Chocolate</td>
      <td>7.48</td>
      <td>5</td>
    </tr>
    <tr class='product'>
      <td>Assorted Fine Chocolates</td>
      <td>9.98</td>
      <td>7</td>
    </tr>

    <tr class='product'>
      <td>Assorted Milk & Dark Chocolates</td>
      <td>12.98</td>
      <td>9<td>
    </tr>

    <tr class='product'>
      <td>Assorted Dessert Truffles</td>
      <td>15.98</td>
      <td>4</td>
    </tr>
   </tbody>
  </table>
</div>
</div>
<div class='row'>
<div class='col-md-4'>

  <h3>Finalize Sale </h3>
  <table class='table'>
    <tr>
      <td>Total Items</td>
      <td><span  id="nitems" >0</td>
    </tr>
    <tr>
      <td>Subtotal</td>
      <td><span  id="subtotal" >0</td>
    </tr>
    <tr>
      <td>5% Sales tax</td>
      <td><span id="tax" >0</td>
    </tr>
    <tr>
      <td>Total</td>
      <td><span id="total" >0</td>
    </tr>
    <tr>
      <td>Final amount (with 15% discount)</td>
      <td><span id="final"  >0</td>
    </tr>
  </table>
narue1992
  • 1,143
  • 1
  • 14
  • 40
  • Can you give more detail than "it doesn't work"? Is it giving an error? Not giving results you want? – Scottie Mar 13 '15 at 14:06
  • only error is I am apparently missing a ; ... from `count += child_nodes.eq(i).text() + "\n"; } }); //var total = parseInt($('.product').val()); var total += parseInt(count);` – narue1992 Mar 13 '15 at 14:12
  • @Scottie aside that, nothing prints – narue1992 Mar 13 '15 at 14:12

1 Answers1

0

The .each function should take a variable that will be used as an iterator. Change this:

$('.product').each(function() {

to this

$('.product').each(function(index, val) {

then inside, you should have access to each of the TD's.

Also, this line:

count += child_nodes.eq(i).text()

is going to give you a value of 5794. It's appending each of the strings, not adding the int values. You'll want something like:

count += parseInt($("td", val).eq(3)), 10);

To put it all together, I'd do this...

function item_count(){

    var count = 0;

    $('.product').each(function(index, val) {

        var total += parseInt($("td", val).eq(3), 10);
    }       

    $('#nitems').val(total);
};

Note that this is all un-tested, so some tweaks may be necessary.

Scottie
  • 11,050
  • 19
  • 68
  • 109
  • So I am new to jquery so can you explain how the values index and val are automatically understood values? In other words I don't see you declaring them. Also what does the "10" mean? – narue1992 Mar 13 '15 at 14:20
  • Honestly, I'm not sure exactly how the inner workings of the .each assigns the index and val fields. That's a really good thing for me to research! Suffice to say, it just works. parseInt by default uses base 8 for some mysterious reason. Here is a link describing it: http://stackoverflow.com/questions/5600366/why-does-the-radix-for-javascripts-parseint-default-to-8 – Scottie Mar 13 '15 at 16:14