Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For loop to identify element to delete, debugging

    Hi all,
    I'm new to programming and I'm wondering if anyone can give me some help figuring out what is wrong with a function I'm trying to fix. I didn't code this, but I'm having trouble with the function below which is suppossed to delete a line item in a table. It seems to be deleting one element and also whatever the last element is?

    Code:
    function delLineItem(ndx)
    {
    
        var table = document.getElementById('linetable');
        var linendxs = document.getElementsByName('linendx');
        var j=0;
        for (var i = 0; i < linendxs.length; i++) {
           var nv = linendxs[i].value.toString();
           if(nv == ndx){
              lineitem = document.getElementById("qli_quote_linenumber"+nv.toString());
              row = lineitem.value;
              lineitem.value = -1;
              table.deleteRow(row);
           }
        }
    //this second part seems to be resetting the line numbers in he edit mode to account for the removal of the deleted item
        var k=0;
        ni = eval('document.editFrm.numitems'); //editFrm is the name and id of the lineitem form when editing a quote
        for (var j = 0; j < linendxs.length; j++) {
           var nv = linendxs[j].value.toString();
           lineitem = document.getElementById("qli_quote_linenumber"+nv.toString());
         
           if(lineitem != null){
              k++;
              lineitem.value = k;
                alert(k);
              ni.value = k;
           }
           else {
               ni.value = k;
    
           }
        }
    updateTotal();
    
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I think we need to see the HTML that goes with the JS code.

    Best would be if you could show us a live URL.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And this is silly:
    Code:
        ni = eval('document.editFrm.numitems');
    Think "eval is evil"! (It isn't 100% of the time, but it's a good rule.)

    You don't need the eval() at all.
    Code:
        ni = document.editFrm.numitems;
    Though I would point out that this means that you are using a named <form>.

    <form>s with names are considered obsolete; you should be using in ID with each <form>, instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I dont have a live url I can show. Here's the html code for the table:
    Code:
    <TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0" WIDTH="100%" CLASS="std"id="linetable" >
           <tr>
           <th style="background:#736F6E;width:20px"><?php echo $AppUI->_('View/Delete/Move');?></th>
    	   <th style="width:10px"><?php echo $AppUI->_('Line');?></th>
    	   <th><?php echo $AppUI->_('Product #');?></th>
           <th><?php echo $AppUI->_('Item');?></th>
           <th><?php echo $AppUI->_('Description');?></th>
           <th><?php echo $AppUI->_('Price');?></th>
           <th><?php echo $AppUI->_('Discount');?></th>
           <th><?php echo $AppUI->_('Qty.');?></th>
           <th><?php echo $AppUI->_('Extend');?></th>
           <th style="background:#736F6E"><?php echo $AppUI->_('Lead Time ');?></th>
           <th style="background:#736F6E"><?php echo $AppUI->_('Contact ');?></th>
           </tr>
           <?php
    
        function showRow($lineitem,$ndx) {
          global $qlineitems;
         
          if($ndx == 1){
             $nav = "<a href='javascript:downLineItem($ndx)'>";
    	     $nav .= dPshowImage('images/navdown.gif');
             $nav .= '&nbsp;';
    	     $nav .= "</a>";
          }
          else if ($ndx == sizeof($qlineitems)){
             $nav = "<a href='javascript:upLineItem($ndx)'>";
    	     $nav .= dPshowImage('images/navup.gif');
             $nav .= '&nbsp;';
    	     $nav .= "</a>";
          }
          else{
             $nav = "<a href='javascript:upLineItem($ndx)'>";
    	     $nav .= dPshowImage('images/navup.gif');
             $nav .= '&nbsp;';
    	     $nav .= "</a>";
             $nav .= "<a href='javascript:downLineItem($ndx)'>";
    	     $nav .= dPshowImage('images/navdown.gif');
    	     $nav .= "</a>";
          }
          $iprice = $lineitem->qli_product_price;
          $price = number_format($iprice,2);
          $extend = number_format($lineitem->qli_extend,2);
          global $discountList;
          $CR = "\n";
          $s = '<tr>'.$CR;
          $s .= '<input type="hidden" id="linendx" value="'.$ndx.'" name="linendx" value="'.$ndx.'"/>'.$CR;
          $s .= '<input type="hidden" id="linendx'.$ndx.'" value="'.$ndx.'" name="linendx'.$ndx.'" value="'.$ndx.'"/>'.$CR;
      //    $s .= '<input type="hidden" id="qli_quote_linenumber'.$ndx.'" name="qli_quote_linenumber'.$ndx.'" value="'.$ndx.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_quote_number'.$ndx.'"  name="qli_quote_number'.$ndx.'" value="'.$lineitem->qli_quote_number.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_id'.$ndx.'" name="qli_product_id'.$ndx.'" value="'.$lineitem->qli_product_id.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_number'.$ndx.'" name="qli_product_number'.$ndx.'" value="'.$lineitem->qli_product_number.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_type' .$ndx.'" name="qli_product_type'.$ndx.'" value="'.$lineitem->qli_product_type.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_name'.$ndx.'" name="qli_product_name'.$ndx.'" value="'.$lineitem->qli_product_name.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_description'.$ndx.'" name="qli_product_description'.$ndx.'" value="'.$lineitem->qli_product_description.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_baseprice'.$ndx.'" name="qli_product_baseprice'.$ndx.'"  value="'.$lineitem->qli_product_baseprice.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_price'.$ndx.'" name="qli_product_price'.$ndx.'"  value="'.$lineitem->qli_product_price.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_quantity'.$ndx.'" name="qli_quantity'.$ndx.'" value="'.$lineitem->qli_quantity.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_extend'.$ndx.'" name="qli_extend'.$ndx.'" value="'.$lineitem->qli_extend.'"/>'.$CR;
          $s .= '<input type="hidden" id="qli_product_contact'.$ndx.'" name="qli_product_contact'.$ndx.'" value="'.$lineitem->qli_product_contact.'"/>'.$CR;
          $s .= '<td width="60" valign="top">';
    	  $s .= '<a href="?m=quotes&a=viewProduct&product_id='.$lineitem->qli_product_id.'">';
    	  $s .= dPshowImage('./images/icons/eyeglasses.gif');
          $s .= '&nbsp; &nbsp; &nbsp';
    	  $s .= "<a href='javascript:delLineItem($ndx)'>";
    	  $s .= dPshowImage('images/icons/stock_delete-16.png');
          $s .= '&nbsp; &nbsp; &nbsp';
    	  $s .= "</a>";
          $s .= $nav;
          $s .= '<td valign="top" >'.'<input type="text" name="qli_quote_linenumber'.$ndx.'" id="qli_quote_linenumber'.$ndx.'" value="'.$ndx.'"';
          $s .= 'style="text-align: right; width: 30px"/>'.'</td>'.$CR;
          $s .= '<td valign="top" >'.$lineitem->qli_product_number.'</td>'.$CR;
          $s .= '<td valign="top">'.$lineitem->qli_product_name.'</td>'.$CR;
          $s .= '<td valign="top">'.$lineitem->qli_product_description.'</td>'.$CR;
          $s .= '<td valign="top">' .'<input type="text" id="qli_product_price_TF'.$ndx.'" value ="'.$price.'"';
          $s .= 'style="text-align: right; width: 100px"'. 'onkeypress="voidKey(event)")"'.'</td>'.$CR;
          $s .= '<td valign="top">'.arraySelect($discountList, 'qli_discount_ndx'.$ndx.'', 'id="qli_discount_ndx'.$ndx.'" onChange="change_quantity('.$ndx.')" class="text"', $lineitem->qli_discount_ndx, false ).'</td>'.$CR;
          $s .= '<td valign="top" >'.'<input type="text" name="qli_quantity_TF'.$ndx.'" id="qli_quantity_TF'.$ndx.'" value="'.$lineitem->qli_quantity.'"';
          $s .= 'style="text-align: right; width: 30px" '.'onkeypress="qvalidate(event,'.$ndx.')" onchange="change_quantity('.$ndx.')"' .'</td>'.$CR;
          $s .= '<td valign="top" >'.'<input type="text" name="qli_extend_TF'.$ndx.'" id="qli_extend_TF'.$ndx.'" value="'.$extend.'"';
          $s .= 'style="text-align: right; width: 100px"'. 'onkeypress="voidKey(event)"'. 'onchange="change_extend('.$ndx.')"'.'</td>'.$CR;
          $s .= '<td valign="top">'.$lineitem->qli_product_leadtime.'</td>'.$CR;
          $s .= '<td valign="top">'.$lineitem->qli_product_contact.'</td>'.$CR;
          $s .= '<td valign="top" width="16">';
          $s .= '</td>'.$CR;
    	  $s .= '</tr>'.$CR;
    	return $s;
        }
        $s = '';
        $ndx=0;
        foreach ($qlineitems as $row) {
           $ndx++;
    	   echo showRow($row,$ndx);
        }
       
    ?>
       </TABLE>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •