473,804 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help in adding values from a table row, dynamically

67 New Member
Well friends, I am having a table like this ...



Where, the left most and right most columns have zero values when the page loads. As we click on the + or - sign associated with the left most column, the value of the left most column increases or decreases by 1. The value of the right most column also changes at the same time, and gives the value of the left most column * the value of the third column, prix.

Here are the two functions that I am using for this.

Expand|Select|Wrap|Line Numbers
  1. function addChoc(str,str2,str3) {
  2.     chocobox = document.getElementById(str);
  3.     chocobox.value = Number(chocobox.value)+1;
  4.     chocoPrice = document.getElementById(str2);
  5.     chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
  6. }
  7. function subChoc(str,str2,str3) {
  8.     chocobox = document.getElementById(str);
  9.     if (chocobox.value != 0) {
  10.         chocobox.value = Number(chocobox.value)-1;
  11.         chocoPrice = document.getElementById(str2);
  12.         chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
  13.  
  14.     } else {
  15.         chocobox.value = chocobox.value;
  16.     }
  17. }
  18.  
where,

str = value of first column, qnty.
str2 = value of last column, total
str3 = value of third column, prix

till now, we are dealing with a single row, and so far, everything is fine, working as it should (I have tested only in FF till now).

The rows of the given table is generated by php, using foreach loop, fetched from mySQL.

*************** *************** *************** *************** *************** ***************

The problem starts now. What I want, is as I click on the + or - sign, along with the two values that are already changing, the value of the last row, right most column should also change (grande totale). And here I am totally lost at this moment ... even after spending several hours pulling out my hairs I was unable to find a solution that works.

Is there anybody who can help me out? :)
Thanks a lot for your time.
Nov 20 '07 #1
15 1724
kigoobe
67 New Member
Some 3 hours later (including my dinner) ... here is what I have got ...

Expand|Select|Wrap|Line Numbers
  1. function addChoc(str,str2,str3,chocArray,id) {
  2.     chocobox = document.getElementById(str);
  3.     chocobox.value = Number(chocobox.value)+1;
  4.     chocoPrice = document.getElementById(str2);
  5.     chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
  6.     chocArray[id] = chocoPrice.innerHTML;
  7.     chocoTotal = document.getElementById('gr_totale');
  8.     chocoTotal.innerHTML = Number(chocArray[id]);
  9. }
  10.  
I am now sending the whole thing in an array and dynamically changing the price. As in the above code, the last line displays the total of a given row by calling it with its id as array key. However, since I am passing the whole array here, I could easily fetch their values and add them to get the desired result.

Say, a javascript equivalent of something like ...

[php]
$total = '';
foreach ($array as $v) {
$total = $total + $v;
}
[/php]

If this works, hopefully the issue will be solved. :)

EDIT

The following is giving NaN error.

Expand|Select|Wrap|Line Numbers
  1. function addChoc(str,str2,str3,chocArray,str4) {
  2.     chocobox = document.getElementById(str);
  3.     chocobox.value = Number(chocobox.value)+1;
  4.     chocoPrice = document.getElementById(str2);
  5.     chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
  6.     chocArray[str4] = chocoPrice.innerHTML;
  7.     chocoTotal = document.getElementById('gr_totale');
  8.     var total = "";
  9.     for(i=0;i<chocArray.length;i++) {
  10.         total = Number(total) + Number(chocArray[i]);
  11.     }
  12.     chocoTotal.innerHTML = Number(total);
  13. }
  14.  
hmmm .... :(:(:(

EDIT 2 @ another hour later ...

solved ... js was not recognizing the $key=>$value type of array as array and was treating them as object ... :) solved now. :)
Nov 20 '07 #2
gits
5,390 Recognized Expert Moderator Expert
hi ...

glad to hear you got it working for yourself ... :) and telling so and even the solution. post back to the forum anytime you have more questions ...

kind regards
Nov 20 '07 #3
kigoobe
67 New Member
Thanks gits. That's a great spirit that I have seen among the moderators of this site. :)

Here is my function at present, and it's doing all what it should do, exactly as I want.

Expand|Select|Wrap|Line Numbers
  1. function subChoc(qnty,totale,prix,chocArray,id,action) {
  2.     chocobox = document.getElementById(qnty);
  3.     if ( (action == 'plus') || ((action == 'minus') && (chocobox.value != 0)) ) {
  4.         if (action == 'plus') {
  5.             chocobox.value = Number(chocobox.value)+1;
  6.         } else if (action == 'minus') {
  7.             chocobox.value = Number(chocobox.value)-1;
  8.         }
  9.         chocoPrice = document.getElementById(totale);
  10.         chocoPrice.innerHTML = Number(prix) * Number(chocobox.value);
  11.         chocArray[id] = chocobox.value + "~" + chocoPrice.innerHTML;
  12.         chocoTotal = document.getElementById('gr_totale');
  13.         var total = 0; var price = 0;
  14.         for(var i=0;i<chocArray.length;i++) {
  15.             var splitMe = chocArray[i].split("~");
  16.             if (Number(splitMe[0]) == 0) {
  17.                 price = 0;
  18.             } else {
  19.                 price = Number(splitMe[1]);
  20.             }
  21.             total = Number(total) + Number(price);
  22.         }
  23.         chocoTotal.innerHTML = total;
  24.     } else {
  25.         chocobox.value = chocobox.value;
  26.     }
  27. }
  28.  
I am sure that this may not be the perfect script, given that I am new in JS, I hope that in the future I will be able to make similar functions in a more 'pure', 'OO' way.

Now, later down the page, I am calling a function like this =>
Expand|Select|Wrap|Line Numbers
  1. getAjax('ajax/getStatus.php','getStatus','text','quantity','product','','');
  2.  
Here in this function, I need to pass two arrays, one array consisting all the ids from the above table as product, and one array consisting of all the quantities (chocobox.value ) including zeroes where the quantities were not altered, as quantity. I am a bit lost about how to construct such arrays and pass that to the above function getAjax.

I would be greatful if someone can help me here. If by that time I found a solution myself, I will be post it back. :)
Nov 21 '07 #4
gits
5,390 Recognized Expert Moderator Expert
hi ...

you want to create the arrays from the table?

kind regards
Nov 21 '07 #5
kigoobe
67 New Member
Hi gits. Thanks for your reply. Actually I need these two arrays so that later I can work on data fetched from this table.

I am not sure how to make the two arrays (php | javascript | ?) and importantly, how to pass the two arrays in the function getAjax.


*************** *************** *************** *************** *************** ***************

Actually, whenever I am calling the function subChoc, I am sending the related id and thereby getting the quantity value. Thus, probably I can have a key value pair with these two in the function itself, and probably I can even make an array there.

Probably the bigger question here is how to pass the array in my getAjax function? !!!

:(:(:(
Nov 21 '07 #6
gits
5,390 Recognized Expert Moderator Expert
no problem :) ... we have to retrieve the nodes of the table and store the value in the corresponding arrays ... so could you please post your current table-html-code, so that we could see how to parse the table?

the alternative with the subChoc method is good ... we would have to store the used ids and values and fill the arrays with ids and zeros for the unhandled items ...

kind regards
Nov 21 '07 #7
gits
5,390 Recognized Expert Moderator Expert
Probably the bigger question here is how to pass the array in my getAjax function? !!!
when you have an array:

Expand|Select|Wrap|Line Numbers
  1. var arr = [1, 2, 3];
  2.  
you may simply pass it to a function like this:

Expand|Select|Wrap|Line Numbers
  1. function use_array(arr) {
  2.     alert(arr);
  3. }
kind regards
Nov 21 '07 #8
kigoobe
67 New Member
Oh thanks Gits ... you mean that passing an array to a function is alike passing a normal variable !!! Then it's really great. For making the array, I can simply fo with the subChoc() ... would be simpler that way ... :)

You don't know how much you have helped me just now. Thanks a lot. Thanks again Gits. :)
Nov 21 '07 #9
kigoobe
67 New Member
May be one question more here. The said array will be inside the function subChoc. That would mean, I will have to call the function subChoc somewhere inside the function getAjax to transfer the array properly, isn't it?
Nov 21 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2012
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my form i have a table with 3 rows (3 input text), in which user can enter values and then clik save. When he clicks the Save button, i'd like to be able to add the created items to the end of my table
3
1723
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they have finished leaves that particular cell on the grid with the correct value. Seems...
4
2279
by: cjm | last post by:
I have two problems that I suspect will be bread-and-butter problems for the more experienced guys on here, but I'm not the greatest with js. NB: Code snippets at the bottom The first problem is that after a bit of fiddling I'm getting am 'Object Expected' error when I click on the Depot dropdown which I can't seem to get round. The code I had was working OK but then I cut it all out, tidied it all up, and put it back in and now it...
3
1704
by: Punisher | last post by:
I'm adding checkbox controls to a table dynamically when the form loads for the first time. When a postback occurs, the controls are not maintained in the table. How do I get them to retain without having re-add them again.
0
1880
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to collect the data in the Textboxes which i added dynamically from server side, i am not able to do . Alos i tried to bedug, the total no of rows in Html table does not reflect the dynamically added rows.
1
2034
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like product types etc. So for instance Im adding a fruit company while adding a fruit company I allow the user to add types of fruit they carry and display it dynamically using an <asp:table> with image
0
2410
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta version, the program is not working as in version 1.1. So what is the problem? Microsoft declared that the version 2.0 is fully backward support, but it is not. Is that the problem with 2.0 version? I find out the problem in version 2.0. After...
5
3737
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button. When the user clicks the "Add another email" it will call a client side JavaScript function, add_email, which will dynamically add a new set of controls to the webpage using the innerHTML method. It appears to work perfectly fine in the browser. The...
1
2054
by: srneu71 | last post by:
I have a project that requires a dynamically generated matrix table. The table is setup with 4 quadrants (N,S,E,W) with checkboxes to "link" the data in adjacent quadrants. The table has to be able to grow & shrink according to the number of items in each quadrant. I have coded the table as a user control in vb.net. My question is on how to retrieve the data out of the table on a post back. I have tried adding check boxes as straight...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.