Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage calculation in a list

Status
Not open for further replies.

Flacker0763

IS-IT--Management
Jun 15, 2009
13
NO
Hi!
This is my first post here. I'm new to javascript, but have done a lot of html/asp programming. If I post this on the wrong forum, I'm sorry.

My case is like this:
I'm making a pricing tool for our intraweb. This tool gets our SKUs from an sql-server.
I then have the cost for all SKUs, and an input field for all SKUs, and an empty input field right after.
What I need is to get the difference, in percentage, between the number written in the input-field and the cost. I though the onChange event would handle this, since I really don't want to load the whole page to update this field.

Is there an easy way to make a function doing this, wich can be called from all lines with SKUs?
I have managed to do this with 3 SKUs, but this required 3 separate functions.

The SKU-listing can be from 30 to over 100 SKUs, depending on the cathegory chosen.


/Geir
 
It won't work to put the function inside the loop, it just takes one costprice and using that for all the SKUs.

To get this to work, I need to get the function to read this value somehow.
 
Hi

Then let us use [tt]hidden[/tt] field, so for SKU=2009 will have the [tt]input[/tt]s :
[ul]
[li]2009_1 - price ( [tt]type="text"[/tt] )[/li]
[li]2009_1_a - percentage ( [tt]type="text" readonly="readonly"[/tt] )[/li]
[li]2009_1_b - costprice ( [tt]type="hidden"[/tt] )[/li]
[/ul]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]UpdateDG[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  c [teal]=[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]what[teal].[/teal]value[teal])/[/teal][highlight][COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]what[teal].[/teal]form[teal][[/teal]what[teal].[/teal]name[teal]+[/teal][green][i]'_b'[/i][/green][teal]].[/teal]value[teal])[/teal][/highlight][teal];[/teal]
  d [teal]=[/teal] [teal]([/teal]c[teal]*[/teal][purple]100[/purple][teal])-[/teal][purple]100[/purple][teal];[/teal]
  what[teal].[/teal]form[teal][[/teal]what[teal].[/teal]name[teal]+[/teal][green][i]'_a'[/i][/green][teal]].[/teal]value [teal]=[/teal] Math[teal].[/teal][COLOR=darkgoldenrod]round[/color][teal]([/teal]d[teal]*[/teal][purple]100[/purple][teal])/[/teal][purple]100[/purple] [teal]+[/teal] [green][i]"%"[/i][/green][teal];[/teal]
[teal]}[/teal]
Code:
    do while not rs11.eof
        sql1="select distinct top 1 beskrivelse, varenr, hwid[highlight], kostpris[/highlight] from pricetool..pricelog with (nolock) where hwid="&rs11(0)
        set rs1 = conn.Execute (sql1, adCmdText)
        
        
        ''Varenr og beskrivelse
        if isnumeric(rs1(1)) then
            varenrstr = varenrstr &","&trim(rs1(1))
            vareteller = vareteller + 1
            response.write "<tr onMouseover=""this.bgColor='#EEEEEE'"" onMouseout=""this.bgColor='#FFFFFF'""><td>"&rs1(1)&"</td><td>"&rs1(0)&"</td>"
        else
            response.write "<tr onMouseover=""this.bgColor='#EEEEEE'"" onMouseout=""this.bgColor='#FFFFFF'""><td bgcolor=#ffb48f></td><td>"&rs1(0)&"</td>"
        end if


        ''Ny pris
        if isnumeric(rs1(1)) then
            response.write "<td>[highlight]<input name='"&trim(rs1(1))&"_1_b' type="hidden" value='"&trim(rs1(4))&"'>[/highlight]&nbsp;<input name='"&trim(rs1(1))&"_1' type=text size=5 onkeyup='UpdateDG(this);' value='"&trim(rs1(1))&"_1'> <input name='"&trim(rs1(1))&"_1_a' type=text size=3 class=noborder readonly>"
            response.write "<br>[highlight]<input name='"&trim(rs1(1))&"_1_b' type="hidden" value='"&trim(rs1(4))&"'>[/highlight]&nbsp;<input name='"&trim(rs1(1))&"_2' type=text size=5 onkeyup='UpdateDG(this);'> <input name='"&trim(rs1(1))&"_2_a' type=text size=3 class=noborder readonly>"
            response.write "<br>[highlight]<input name='"&trim(rs1(1))&"_1_b' type="hidden" value='"&trim(rs1(4))&"'>[/highlight]&nbsp;<input name='"&trim(rs1(1))&"_3' type=text size=5 onkeyup='UpdateDG(this);'> <input name='"&trim(rs1(1))&"_3_a' type=text size=3 class=noborder readonly></td>"
        else
            response.write "<td></td>"
        end if


        rs11.movenext
    loop
Again, I have no idea about ASP, the above suggestion is just theory.


Feherke.
 
Fantastic fantastic!!!
You are a magician with javascript :) :) :)

That did the trick, now the rest is easy solvable!

If you're ever going to norway, I'll buy you a beer ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top