473,408 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Returning current row index and applying to string javascript problem

129 100+
Here is the situation i am currently trying to update a sales order using PHP and javascript. When you click on the edit button it brings you a new site page with existing sales order details within. I am wanting the order lines to be within option boxes so they can modify the order if they wish. I created some javascript for the create sale site page which automatically assigned a price to a particular product once selected. Here is the code for this;

Expand|Select|Wrap|Line Numbers
  1.  $(document).ready(function() {
  2.  
  3.      $("#product_select > option").click(function () {
  4.         var selectvalue = $('#product_select').val();
  5.         $.post("<?php echo SITE_URL; ?>sale/ajax",
  6.         { id: selectvalue },
  7.         function(return_id)
  8.         {
  9.             var return_array = return_id.split(",");
  10.             $("#price").val(return_array[0]);
  11.             if (return_array[1] == 1) {
  12.                 $('#duration').removeAttr("disabled"); 
  13.                 $('#duration').attr('enabled', 'enabled');
  14.                 $('#duration').val('0');
  15.             } else {
  16.                 $('#duration').removeAttr("enabled"); 
  17.                 $('#duration').attr('disabled', 'disabled');
  18.                 $('#duration').val('0');
  19.             }
  20.         });
  21.     });
  22.  
  23.  });
  24.  
  25. </script>
Here i am pretty much posting the product_id to the site ajax and returning the price into the unit_price input on the site. This works fine. However i am wanting a similar procedure but if they change the order lines on the edit form. The form is setup as follows;

Expand|Select|Wrap|Line Numbers
  1. <div id="main">
  2.  
  3.     <h2><i><u>Order Header Details</u></i></h2><br />
  4.  
  5.     <table id="sale" border='1' style="border-style: outset; empty-cells: show">
  6.         <tr>
  7.             <th>Order No</th>
  8.             <th>Date</th>
  9.             <th>Account</th>
  10.             <th>Amount</th>
  11.             <th>Payment Method</th>
  12.             <th>Sales Rep</th>
  13.         </tr>
  14.             <tr>
  15.                 <td><strong><?php echo $order->order_id; ?></strong></td>
  16.                 <td><?php echo $order->date_added; ?></td>
  17.                 <td><?php echo $order->company_name; ?></td>
  18.                 <td>&pound;<?php echo number_format($order->sales_total,2) ?></td>
  19.                 <td><?php echo $order->payment_option; ?></td>
  20.                 <td><?php echo $order->rep_name; ?></td>
  21.             </tr>
  22.     </table>
  23.  
  24.     <h2><i><u>Order Line Details</u></i></h2><br />
  25.  
  26.     <table id="basket" border='1' style="border-style: outset; empty-cells: show">
  27.         <tr>
  28.             <th>Item type</th>
  29.             <th>Price (&pound;)</th>
  30.             <th>per</th>
  31.             <th>Duration</th>
  32.             <th>Update</th>
  33.             <th>Delete</th>
  34.         </tr>
  35.         <?php
  36.         //$i = 0;
  37.         foreach ($basket as $basket) {
  38.         ?>
  39.         <tbody>
  40.         <tr class="line edit_item">
  41.             <td class="product">
  42.             <select class="product_select" id="product_select<?php echo $i; ?>" name="product">
  43.                 <?php foreach ($products as $product) { ?>
  44.                 <option value="<?php echo $product->product_id; ?>" <?php if ($product->product_id == $basket->product_id) { echo 'selected="selected"'; } ?>><?php echo $product->product_name; ?></option>
  45.                 <?php } ?>
  46.             </select>
  47.             </td>
  48.             <td class="price">
  49.                 <input name="price" class="price" id="price<?php echo $i; ?>" type="text" value="<?php echo $basket->unit_price; ?>" size="5" />
  50.             </td>
  51.             <td class="per">&nbsp;</td>
  52.             <td class="duration">
  53.                 <select name="duration" class="duration id="duration<?php echo $i; ?>">
  54.                     <option value="0">(Select)</option>
  55.                     <option value="1">1 Month</option>
  56.                     <option value="3">3 Months</option>
  57.                     <option value="6">6 Months</option>
  58.                     <option value="12">1 Year</option>
  59.                 </select>
  60.             </td>
  61.             <td class="controls"><button type="button" id="update">Update</button></td>
  62.             <td class="controls"><button type="button" id="delete">Delete</button></td>
  63.         </tr>
  64.         <?php
  65.         //$i++;
  66.         } ?>
  67.         </tbody>
  68.     </table>
  69. </div>
As you can see i am using PHP syntax for various objects including the product_select option, price input and duration options. On this form page you can have many order lines. Using the create sales javascript does work for the editing of order lines but for the first record only, i realise it is due to my poor javascripting skills. I have only been learning it for 3 days.

My theory for the code is that i require the row.index of each order line and passing that value through to the javascript however i have tried many times to find and get the current row.index syntax to work. I don't know what i am doing wrong. Here is the code i have been looking at;

Expand|Select|Wrap|Line Numbers
  1. onload = function() {
  2.     if (!document.getElementsByTagName || !document.createTextNode) return;
  3.     var rows = document.getElementById('my_table').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
  4.     for (i = 0; i < rows.length; i++) {
  5.         rows[i].onclick = function() {
  6.             alert(this.rowIndex + 1);
  7.         }
  8.     }
  9. }
  10.  
All i am wanting is if users change the order line details via the product_select or duration options then to update the price field via the javascript code.

Any advice or views on how or what i should do to get this code working would be greatly appreciated. Thanks in advance.
Aug 27 '09 #1
1 5884
gits
5,390 Expert Mod 4TB
when you assign events with a loop and you need to preserve the index then you would need to use a so called closure ... which basicly is a function that stores the value you want to preserve (closure) in it. let me give you a short example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <script type="text/javascript">
  4.  
  5.     function attachClickEvents(doc) {
  6.         var ele = doc.getElementsByTagName('div');
  7.  
  8.         for (var i = 0, l = ele.length; i < l; ++i) {
  9.             var n = ele[i];
  10.  
  11.             n.onclick = function(idx) {
  12.                 return function() { alert(idx) }
  13.             }(i);
  14.         }
  15.     }
  16.  
  17.     </script>
  18. </head>
  19. <body onload="attachClickEvents(document);">
  20.     <div>foo</div>
  21.     <div>foo1</div>
  22.     <div>foo2</div>
  23.     <div>foo3</div>
  24.     <div>foo4</div>
  25.     <div>foo5</div>
  26.     <div>foo6</div>
  27.     <div>foo7</div>
  28.     <div>foo8</div>
  29.     <div>foo9</div>
  30.     <div>foo10</div>
  31.     <div>foo11</div>
  32.     <div>foo12</div>
  33.     <div>foo13</div>
  34.     <div>foo14</div>
  35.     <div>foo15</div>
  36.     <div>foo16</div>
  37.     <div>foo17</div>
  38. </body>
  39. </html>
  40.  
here you see that the index i is preserved by using the closure. when you instead use:

Expand|Select|Wrap|Line Numbers
  1. function attachClickEvents(doc) {
  2.     var ele = doc.getElementsByTagName('div');
  3.  
  4.     for (var i = 0, l = ele.length; i < l; ++i) {
  5.         var n = ele[i];
  6.  
  7.         n.onclick = function() {
  8.             alert(i);
  9.         };
  10.     }
  11. }
  12.  
then you will always get the last index returned ... since it just alerts the value that i had assigned after the complete loop.

kind regards
Aug 30 '09 #2

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

Similar topics

4
by: salty_dogs | last post by:
thanks to all who helped steer me away from 'slicing' photoshop images to positioning with css. new question: i have a web page that has a navbar, simple text links. each of these links will...
3
by: Kevin Gibbons | last post by:
Hello all, I've browsed through past usenet archives, but can't seem to come across quite the javascript I'm looking for. I'm looking for a simple javascript that will display the date as such:...
4
by: Sandy Bremmer | last post by:
I am wondering if the following can be accomplished with javascript (and if so, if you think javascript is an appropriate solution). I'm afraid my javascript skills lack. I need to build a...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: sienaman | last post by:
I have a C# dll with a COM interface that is successfully call by a C++ client. One of the parameters is a string, the method looks like void Goofy(string strUserInput, out string strOutput); ...
0
by: GFro | last post by:
I have a calendar page that returns a date to a textbox on the parent page. It is returning the wrong format on the deployment server. On the development server the calendar returns to textbox in...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
8
by: Mateusz Viste | last post by:
Hi, I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)... ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.