473,385 Members | 1,564 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,385 software developers and data experts.

Javascript Array Question

I have a DataTable that I created in C# that looks like.

Column1 - Column2 - Column3
1 - a - b
2 - c - d
3 - e - f

Now if I want to get the value of column 3 with id 3 I simply need to
do something like this (pseudo-code):

string value = dt.rows[3]["Column3"].ToString();

Is there a javascript object that mimics this same functionality? I am
going to use my C# DataTable to create a javascript object that I can
use for client-side functionality before posting back to the server.

Thanks in advance,

- will

Dec 22 '06 #1
8 1493
ja********@willasrari.com wrote:
I have a DataTable that I created in C# that looks like.

Column1 - Column2 - Column3
1 - a - b
2 - c - d
3 - e - f

Now if I want to get the value of column 3 with id 3 I simply need to
do something like this (pseudo-code):

string value = dt.rows[3]["Column3"].ToString();

Is there a javascript object that mimics this same functionality? I am
going to use my C# DataTable to create a javascript object that I can
use for client-side functionality before posting back to the server.

Thanks in advance,

- will
Using row and cell indexes you can try:

myTable.rows[3].cells[2].innerHTML - extracts all of the HTML in the
referenced table cell. If it is only text, innerHTML will contain the
text only.

Dec 22 '06 #2
Thank you so much. I will give this a try.

- will

Dec 22 '06 #3
Nice! At least I now have proof-of-concept.

Dec 22 '06 #4
Lee
be*******@gmail.com said:
>
ja********@willasrari.com wrote:
>I have a DataTable that I created in C# that looks like.

Column1 - Column2 - Column3
1 - a - b
2 - c - d
3 - e - f

Now if I want to get the value of column 3 with id 3 I simply need to
do something like this (pseudo-code):

string value = dt.rows[3]["Column3"].ToString();

Is there a javascript object that mimics this same functionality? I am
going to use my C# DataTable to create a javascript object that I can
use for client-side functionality before posting back to the server.

Thanks in advance,

- will

Using row and cell indexes you can try:

myTable.rows[3].cells[2].innerHTML - extracts all of the HTML in the
referenced table cell. If it is only text, innerHTML will contain the
text only.
Unless you plan to display the data on the page in a table, that's
a really horrible way to emulate a 2-dimensional array.

myArray= [ [ "a", "b" ],
[ "c", "d" ],
[ "e", "f" ]
];
alert(myArray[1][0]);

Note that Javascript arrays are indexed beginning at zero, so
the alert() above would display "c".
--

Dec 22 '06 #5
Lee is correct. You need only a javascript Array if you plan to just
store data for processing etc. The method I suggested is only relevant
if you want to display and then access the data straight from the
generated HTML.

Dec 22 '06 #6
You are both right actually.

The array is ideal but I figured I could just get the value from the
table since the table will be displayed on the page and the information
is static. I rolled my own JavaScript functionality to drag child
nodes and place them in a different parent. It's used for grouping.
The table lets me retrieve values for calculations and restrict
dropping in certain parent nodes.

Pretty fun stuff actually!

I actually do have one more question. I created a 4th colum and
included an HTML hidden field input. This is programmatically created
in my databound asp.net Repeater web control. So when viewing the HTML
source you would see:

<tr>
<td><input type="hidden" id='itemId_46' value='1' /></td>
<td>1</td>
<td>0.2</td>
<td>0.3</td>
</tr>

<tr>
<td><input type="hidden" id='itemId_55' value='2' /></td>
<td>2</td>
<td>0.201</td>
<td>0.303</td>
</tr>

Now this is where it kind of gets tricky. I figure there will be
anywhere from 20 - 60 items listed at any given time. In the initial
post I wrote that the far left column had values of 1, 2, 3, etc...
This won't always be the case as some days the customer will edit
groupings with items starting at 61 - 120. So the far left column
should really show 61 - 120. And even if starting at 61 it's not
certain that the id's will be sequential (i.e. 61, 63, 76, 88, 101,
120, etc...).

My question has now grown to this: Is it possible to get the
row[index] based on finding the element itemId_55? I get the 55 from
the onDragStartEvent() and could simply concatenate onto
document.getElementById('itemId_" + id).value; I only ask because I
don't want to have to perform a for (i = 0; i < etc.....) loop each
time I want to retrieve values.

I understand that this may sound confusing and I would be willing to
clarify any points of this reply.

I'm really starting to get into JavaScript. It's a wonder why I
haven't used it earlier in my career.

Thanks again!

Dec 22 '06 #7

ja********@willasrari.com wrote:
>
<tr>
<td><input type="hidden" id='itemId_46' value='1' /></td>
<td>1</td>
<td>0.2</td>
<td>0.3</td>
</tr>

<tr>
<td><input type="hidden" id='itemId_55' value='2' /></td>
<td>2</td>
<td>0.201</td>
<td>0.303</td>
</tr>
My question has now grown to this: Is it possible to get the
row[index] based on finding the element itemId_55?
Every TR element has a property rowIndex reflecting its index value in
the table.rows collection. Similarly, every TD element has a property
cellIndex reflecting its cell index in the table.rows[N].cells
collection.

So this is the code you need:

hiddenElement = document.getElementById("itemId_55");
rowIndex = hiddenElement.parentNode.parentNode.rowIndex;

where hiddenElement is the hidden <INPUTelement whose position we are
seeking.
hiddenElement.parentNode --points to the parent TD element
hiddenElement.parentNode.parentNode --points to the parent TR element

from here:

hiddenElement.parentNode.parentNode.rowIndex yields the rowIndex
through which we can access the row from the table.rows collection.

or here is a one line code for accessing the rowIndex:

document.getElementById("itemId_55").parentNode.pa rentNode.rowIndex;

Dec 23 '06 #8
You have been a life-saver! I think what you just showed me is (at the
time of writing) critical path for obtaining the optimal functionality
I am seeking from this application!

Thanks again!

- will

Dec 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
4
by: PiedmontBiz | last post by:
Greetings I have written an online survey with 110 questions. I use javascript to cycle thru all the questions which are displayed in an iframe. I keep track of the question number using a...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
5
by: nescio | last post by:
hello, i have made an application in php so that people can make, on the fly, a form. when they submit the form there is a javascript formvalidation. because we do not know how many fields...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
3
by: karmenkrile | last post by:
In html i have some question, and the answers are radio buttons ... the names of variables in html are array ... like question, question ... etc. and every question has multiple value, depending...
5
by: Sam the Cat | last post by:
Hey All, I am writing some COM code in Python to control photoshop. Several functions of PS require an "Array" argument. In the examples of VBscript or javascript the Array type is used. I...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.