473,411 Members | 2,078 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,411 software developers and data experts.

Retrieve values into JavaScript from a Table

gchq
96
Hi there

Here is the situation - a table is built dynamically with values in the cells I need to retrieve and enter into a database. I have found a way of getting values using JavaScript, but of course with the table being dynamic I have no idea how many rows it will contain. One way is using the TableID.rows.count and then building if statements based on the value - but that is going to be an awfull lot of coding...... So the question is - is there an easier way to retrieve the values? I know that building an array is the answer, but actually getting from A to B is giving me the headache. Any pointers would be appreciated!

Here is the JavaScript that works - but has the limits above. Any ideas would be appreciated!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim GetValues As String = "<script language='JavaScript'>" & _
  3.                         "var table= document.getElementById('Table1'); " & _
  4.                         "var nRows = new Array(); " & _
  5.                         "nRows[0] = table.rows[2]; " & _
  6.                         "nRows[1] = table.rows[3]; " & _
  7.                         "nRows[2] = table.rows[4]; " & _
  8.                         "nRows[3] = table.rows[5]; " & _
  9.                         "nRows[4] = table.rows[6]; " & _
  10.                         "nRows[5] = table.rows[7]; " & _
  11.                         "nRows[6] = table.rows[8]; " & _
  12.                         "nRows[7] = table.rows[9]; " & _
  13.                         "nRows[8] = table.rows[10]; " & _
  14.                         "nRows[9] = table.rows[11]; " & _
  15.                         "var ID1 = nRows[0].cells[0]; " & _
  16.                         "var ID2 = nRows[1].cells[0]; " & _
  17.                         "var ID3 = nRows[2].cells[0]; " & _
  18.                         "var vID1 = ID1.innerText; " & _
  19.                         "var vID2 = ID2.innerText;" & _
  20.                         "var vID3 = ID3.innerText; " & _
  21.                         "var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
  22.                         "document.getElementById('Label1').innerText = vID1 ; " & _
  23.                         "document.getElementById('Label2').innerText = vID2 ; " & _
  24.                         "document.getElementById('Label3').innerText = vID3 ; " & _
  25.                         "document.getElementById('Label4').innerText = vQty1 ; " & _
  26.                         "</script>"
  27.  
  28.             GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID).Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
  29.             Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
  30.  
  31.  
Jan 6 '07 #1
7 21458
acoder
16,027 Expert Mod 8TB
Define a tbody under your table that encapsulates your TRs and TDs.

Then, assuming an id of "tbody", use the following code to retrieve the current row count:
Expand|Select|Wrap|Line Numbers
  1. var currRow = document.getElementById("tbody").rows.length;
Jan 6 '07 #2
acoder
16,027 Expert Mod 8TB
Actually, I'm not sure if defining tbody is necessary, but it works for me. Give it a shot.
Jan 6 '07 #3
gchq
96
I can get the rows simply by :-

Expand|Select|Wrap|Line Numbers
  1. var totalrows = table.rows.length;
The problem I've got is how to define the array (once the total number of rows are known) and then get the variables into a database.

BTW, the first two rows are only headers and don't contain data, and the last row contains a total in cell 3.
Jan 6 '07 #4
acoder
16,027 Expert Mod 8TB
Why don't you just define the array when constructing the table in your ASP (I presume you're using ASP?) code. You can ignore the first two rows when populating the array with values. Then you can also have a variable which stores the total value that you mention. The database problem is totally ASP-related, nothing to do with javascript.
Jan 6 '07 #5
gchq
96
Having re-read my initial posting, it does seem rather ambiguous. I apologise for that.

Here is a better description of the dynamic table:-

Row 0 is the header
Row 1 contains description in 6 columns
Row 2 onwards contain the dynamic rows. Cell 2 contains an <input> textbox (not a TextBox Control).
The final row contains a total in cell 3

Under the me.load event I then need to run a script to first find how many rows, then get those rows into an Array, then manipulate the data from that array (update a database, send to another page..)

I do know that by using:-

var table= document.getElementById('Table1');

I can get the table rows and cells, and by using

var totalrows = table.rows.length; (Thanks acoder :-) )

I can get the total number of rows

I also know that

var nRows = new Array();
nRows[0] = table.rows[2]


defines the first set of values in the first column (I can retrieve the other values back from the database once I have the ID number from this column)

and that I can get the value of the first TextBox (that gives the quantity) with

vQty1 = document.getElementById('txt0').value ;

each textbox in the row has an incremented ID value - txt0, txt1, txt2 and so on

Where I hit the buffers is formatting an array to hold the data, then finding a way of retrieving it again!

I have come across the following snippet :-

for(var i = 0; i < x.x.length; i++)

Whilst I know there is a key there somewhere,,,, sigh
Jan 6 '07 #6
gchq
96
Why don't you just define the array when constructing the table in your ASP
The main problem is the table is fomed in a .NET MasterPage - the processing page is the content. .NET loads the processing page first, then the masterpage, so it's impossible to capture the variables in VB - since JavaScript runs AFTER the full page load (and I can do that in VB code behind) I will get the variables that have loaded AFTER the processing page. (Otherwise VB will always return a value of 3 for the rows (the two headers and the footer).
Jan 6 '07 #7
gchq
96
This seems rather messy, but it gets values up to a max. pre-defined amount of rows into a container to process!

If anyone knows an easier way (and I'm sure there is) I'd love to know about it!

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Dim vTotalRows As Integer = Request.QueryString("vID")
  4.         HiddenField1.Value = CStr(Request.QueryString("total"))
  5.         Dim Table1 As HtmlTable = Master.FindControl("Table1")
  6.         Dim vValue1 As String = CType(Me.Page.Master.FindControl("TextBox3"), TextBox).Text
  7.         TextBox3.Text = vValue1
  8.  
  9.         If vTotalRows > 3 Then
  10.             'start JavaScript
  11.             Dim GetValues As String = "<script language='JavaScript'>" & _
  12.             "var table= document.getElementById('Table1'); " & _
  13.             "var totalrows = table.rows.length; " & _
  14.             "var i = (totalrows - 3) ; " & _
  15.             "var nID = new Array(i); " & _
  16.             "nID[0] = table.rows[2].cells[0].innerText; " & _
  17.             "var vQty1 = document.getElementById('ctl00_txt0').value ; " & _
  18.             "if (i > 1) { " & _
  19.             "nID[1] = table.rows[3].cells[0].innerText; " & _
  20.             "var vQty2 = document.getElementById('ctl00_txt1').value ; " & _
  21.             "}" & _
  22.             "else { " & _
  23.             "nID[1] = ''; " & _
  24.             "var vQty2 = '' ;" & _
  25.             "}" & _
  26.             "if (i > 2) { " & _
  27.             "nID[2] = table.rows[4].cells[0].innerText; " & _
  28.             "var vQty3 = document.getElementById('ctl00_txt2').value ; " & _
  29.             "}" & _
  30.             "else { " & _
  31.             "nID[2] = '' ;" & _
  32.             "var vQty3 = '' ;" & _
  33.             "} " & _
  34.             "if (i > 3) { " & _
  35.             "nID[3] = table.rows[5].cells[0]; " & _
  36.             "var vQty4 = document.getElementById('ctl00_txt3').value ; " & _
  37.             "} " & _
  38.             "else { " & _
  39.             "nID[3] = '' ; " & _
  40.             "var vQty4 = '' ; " & _
  41.             "}" & _
  42.             "if (i > 4) { " & _
  43.             "nID[4] = table.rows[6].cells[0]; " & _
  44.             "var vQty5 = document.getElementById('ctl00_txt4').value ; " & _
  45.             "} " & _
  46.             "else { " & _
  47.             "nID[4] = ''; " & _
  48.             "var vQty5 = '' ; " & _
  49.             "} " & _
  50.             "if (i > 5) { " & _
  51.             "nID[5] = table.rows[7].cells[0]; " & _
  52.             "var vQty6 = document.getElementById('ctl00_txt5').value ; " & _
  53.             "} " & _
  54.             "else { " & _
  55.             "nID[5] = '' ; " & _
  56.             "var vQty6 = '' ; " & _
  57.             "}" & _
  58.             "if (i > 6 ) { " & _
  59.             "nID[6] = table.rows[8].cells[0]; " & _
  60.             "var vQty7 = document.getElementById('ctl00_txt6').value ; " & _
  61.             "} " & _
  62.             "else { " & _
  63.             "nID[6] = '' ; " & _
  64.             "var vQty7 = '' ; " & _
  65.             "}" & _
  66.             "document.getElementById('Label1').innerText = nID[0] ; " & _
  67.             "document.getElementById('Label2').innerText = nID[1] ; " & _
  68.             "document.getElementById('Label3').innerText = nID[2] ; " & _
  69.             "document.getElementById('Label4').innerText = vQty1 ;" & _
  70.             "document.getElementById('Label5').innerText = vQty2 ; " & _
  71.             "document.getElementById('Label6').innerText = vQty3 ; " & _
  72.             "</script>"
  73.             'end JavaScript
  74.             GetValues = GetValues.Replace("Table1", Table1.ClientID).Replace("Label1", Label1.ClientID)
  75.             GetValues = GetValues.Replace("Label2", Label2.ClientID).Replace("Label3", Label3.ClientID).Replace("Label4", Label4.ClientID)
  76.             GetValues = GetValues.Replace("Label5", Label5.ClientID).Replace("Label6", Label6.ClientID)
  77.             Page.ClientScript.RegisterStartupScript(Me.GetType(), "GetValues", GetValues)
  78.         Else
  79.             Label1.Text = "Table is empty!"
  80.         End If
  81.  
Jan 7 '07 #8

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

Similar topics

2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
1
by: Eugfene | last post by:
I have the following function in a html file: function selectEdit(fileID, processCode, processID, fileName,fileDesc) { document.forms.recordID.value = fileID; document.forms.processor.value =...
5
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say...
13
by: no.mail.pls | last post by:
Hiya, How do i retreive fields with similar values from 2 tables? I tried to use (1) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like '%p.name%'"; but it retrieves nothing at...
11
by: Patricio | last post by:
Hello all I open a PopUp that receives two variables (values). This PopUp must return a value as well. How can I retrieve this value? Thanks a lot.
3
by: bazubwabo | last post by:
hi everybody , could u please help me to find out the error on my asp codes,i can't retrieve the inserted values. Here is below the code: <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <!DOCTYPE...
2
by: phpachu | last post by:
Hi, I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in...
4
by: Evanescent | last post by:
Hi Guys, I am trying to create a form which allows the users to retrieve records based on the values entered or chosen in the various combo boxes and textboxes, such as the customer's name, invoice...
4
by: Archanak | last post by:
Hi, I have table like this: select * from sampletest; | title ...
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
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
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...
0
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...
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...
0
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...

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.