adding column numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • decren
    New Member
    • Mar 2007
    • 11

    adding column numbers

    Hello,
    I have a table with an add-row setup. The table has several columns but I only need to calculate COLUMN FOUR. The results go to a textbox via a button. My code is.....

    Code:
    function sumup() {
     document.getElementById("Table1")
      column = document.getElementsByTagName("colFour");
      document.myForm.tbTotal.value = column;
    }
    .....
    Code:
    <TABLE NAME="Table1" id="Table1">
    <COL WIDTH=10*>
    	<COL WIDTH=139*>
    	<COL WIDTH=27*>
    	<COL WIDTH=29*>
    	<COL WIDTH=27* NAME="colFour" id="colFour">
    	<COL WIDTH=10*>
    .....
    Code:
    <INPUT TYPE=TEXT NAME="tbTotal">
    <INPUT TYPE=BUTTON NAME="pbTest" VALUE="TEST" onclick="sumup();"./>
    .....But when I click pbTest, the result in tbTotal is, with the brackets.....
    [object]
    .....What am I doing wrong?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You need to use getElementById instead.

    Comment

    • DutchKingCobra
      New Member
      • Mar 2007
      • 37

      #3
      hi pal,
      im working on your problem whenever i have some time,

      in firefox the price wont be written to the new cell created
      this is easily fixed
      use
      Code:
      // cell 4 - ***PRICE*** of record
      var itemCost = document.forms['R2CDorder1'].tbPrice.value;
      var cell4 = row.insertCell(4); 
      cell4.appendChild(document.createTextNode(itemCost));
      well peace m8

      Comment

      • decren
        New Member
        • Mar 2007
        • 11

        #4
        You need to use getElementById instead
        I assume you mean...

        Code:
        column = document.getElementBy("colFour");
        ...I did not realize you could use getElementById consecutively.. . Thanks, I'll try to work that out.

        im working on your problem whenever i have some time,
        I appreciate any help on this. I think its turning out to be a decent piece of work in progress but I hope not to paint myself into a corner... and thank you for the heads up in firefox.

        ...To anyone else, The project I'm working on is at:

        [HTML]http://www.aaronzjukeb ox.com/R2CDorder1[/HTML]

        but the problem is associated with Table4 instead of Table1. Any and all pointers are appreciated.

        Comment

        • mrhoo
          Contributor
          • Jun 2006
          • 428

          #5
          This is a general function to total the cells in a table column.
          It assumes that all of the rows are in a single tbody,
          though it is easy enough to call it on multiple tbodies and total the result.
          Empty cells in the column and cells without numbers are treated as 0.

          You can call the function with no arguments, columnSum();
          and it will return the total of the last column of the first tbody on the page,
          or you can specify the parent table or tbody and the column number.
          Code:
          function columnSum(pa,col){
              pa= pa || document.getElementsByTagName('tbody')[0];
              /* if no parent is passed, get the first tbody on the page */
          
              var G= pa.getElementsByTagName('tr');
              col= col || G[0].getElementsByTagName('td').length;
              --col;
              /* if no col is passed, get the index of the last column
              note- not 0 based, the argument for the first column is passed as 1 */
          
              var L= G.length,sum= 0,temA= [];
              for(var i= 0; i< L; i++){
                  tem= G[i].getElementsByTagName('td')[col];
                  var c=0;
                  while(tem && !tem.data ) tem= tem.childNodes[c++];
                   /* digs into the cell's innards, in case the data is in a child of the cell */
                  tem= tem? tem.data : 0;
                  sum+= parseFloat(tem) || 0;
              }
              return sum;
          }

          Comment

          • decren
            New Member
            • Mar 2007
            • 11

            #6
            =DutchKingCobra
            in firefox the price wont be written to the new cell created
            this is easily fixed
            use
            Code:
            // cell 4 - ***PRICE*** of record
            var itemCost = document.forms['R2CDorder1'].tbPrice.value;
            var cell4 = row.insertCell(4); 
            cell4.appendChild(document.createTextNode(itemCost));
            well peace m8
            I downloaded FireFox to see what was going wrong. My problem was that I had NAMEd all my elements in the table but had not assigned IDs. This worked fine in IE but halted the function in FireFox. Thanx for the heads-up.
            Last edited by decren; May 5 '07, 10:52 AM. Reason: forgot to give credit to DutchKingCobra

            Comment

            • decren
              New Member
              • Mar 2007
              • 11

              #7
              I want to thank everyone at TheScripts for their input to this problem. With your help and with a table located at:

              I was able to work out a solution. If anyone is interested in (what I believe to be unique) addRow/shoppingCart type form, you are welcome to take what you need from:

              Understand though that it is a work in progress so you will have to sift through a lot of garbage to get the scripts to your particular need.
              And thanx again

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Wouldn't it make more sense just to add an onchange handler to each of the inputs you want to total? Something like...

                [HTML]
                <td><input id="row1Price" onchange="sumRo ws(this);" /></td>
                [/HTML]

                Code:
                var $rowValues = {};
                function sumRows($element) {
                	$element.value = $element.value.replace(/[^\d.]/, '');
                	$rowValues[$element.id] = $element.value;
                	
                	document.getElementById('theSum').value = sum($rowValues);
                }
                
                function sum($obj) {
                	var $retVal = 0;
                	
                	for($r in $obj)
                		$retVal += $obj[$r];
                	
                	return $retVal;
                }

                Comment

                Working...