Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Help!


Message #1 by John Owen <JOWEN@f...> on Wed, 18 Jul 2001 14:05:03 -0500
I wrote the following functions in javascript and I fear they are too
inefficient. Especially when dealing with hundreds of items. I basically
have a page that can have any number of rows (usually < 1000) that contain 4
radio buttons and 2 text boxes. I iterate through the rows to keep a running
total any time a specific text box is changed (or one of the radio buttons
is checked). I'll also include a snippet of the HTML code for clarity. Any
help would be greatly appreciated, even if it's an alternate method. As long
as it's proven (tested,etc). :) I've tried several implementations and none
have performed optimally.

Thanks,

John Owen
Federal TransTel
Senior Developer

<JS snippet>

creditList = new Array();

function runningTotal(idx)
{
  var sValue = 0.0;
  // iterate through all rows
  for(var i=0;i<idx;i++)
  {
    // get radio group object for current row
    myObj = eval("chargesForm.creditType" + i);
    // iterate through all radio buttons in group
    for(var j=0;j<myObj.length;j++)
    {
      // if button checked, set value accordingly
      if(myObj[j].checked)
      {
        // create reference to amount box on current row
        myObj2 = eval("chargesForm.creditAmount" + i);
        // evaluate each type and calculate credit amount
        // accordingly
        if(myObj[j].value == 'F')
        {
          if(!isNaN(parseFloat(myObj2.value)))
            sValue = sValue + parseFloat(myObj2.value);
        }
        if(myObj[j].value == 'NU')
        {
          myObj2.value='';
        }
        if(myObj[j].value == 'P')
        {
          myObj3 = creditList[i];
          if(!isNaN(parseFloat(myObj2.value)))
            sValue = sValue + ((parseFloat(myObj2.value)/100.0)*myObj3);
        }
        if(myObj[j].value == 'W')
        {
            myObj3 = creditList[i];
            sValue = sValue + myObj3;
            myObj2.value='';
        }
        continue;
      }
    }
  }
  chargesForm.runningTtl.value = String(sValue);
}

function saveCredit(idx,sCredit,sType)
{
  // reset credit for Flat selection
  if(sType == 'F')
  {
    creditList[idx] = 0.0;
  }
  // set Credit to 0 for NU selection
  if(sType == 'NU')
  {
    creditList[idx] = 0.0;
  }
  // set Credit to Balance Amount
  // will be used to calculate actual credit after user enters it
  if(sType == 'P')
  {
    creditList[idx] = sCredit;
  }
  // set Credit to Balance Amount
  if(sType == 'W')
  {
    creditList[idx] = sCredit;
  }
  //alert(String(sCredit));
}

function setCreditList(idx)
{
  for(var i=0;i<idx;i++)
  {
    creditList[i] = 0.0;
  }
}

</JS snippet>

<html snippet>
<td>
<input type="radio" name="creditType<%= i %>" CHECKED value="NU"
onClick="saveCredit(<%=i%>,<%=chargeList2[i].getBalanceAmount()%>,this.value
);runningTotal(<%=numberOfRows2%>)">N
<input type="radio" name="creditType<%= i %>" value="W"
onClick="saveCredit(<%=i%>,<%=chargeList2[i].getBalanceAmount()%>,this.value
);runningTotal(<%=numberOfRows2%>)">W
<input type="radio" name="creditType<%= i %>" value="F"
onClick="saveCredit(<%=i%>,<%=chargeList2[i].getBalanceAmount()%>,this.value
);runningTotal(<%=numberOfRows2%>)">F
<input type="radio" name="creditType<%= i %>" value="P"
onClick="saveCredit(<%=i%>,<%=chargeList2[i].getBalanceAmount()%>,this.value
);runningTotal(<%=numberOfRows2%>)">P
</td>
<td><input type="text" size="6" name="creditAmount<%= i %>"
onBlur="runningTotal(<%=numberOfRows2%>)"></td>

</html snippet>

  Return to Index