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

tree structure

I'm trying to code a tree structure using javascript, the nodes of the
tree are generated from a sql table. Has anyone some code for this?

Cheers
Jul 23 '05 #1
4 4790
plork wrote:
I'm trying to code a tree structure using javascript, the nodes of the
tree are generated from a sql table. Has anyone some code for this?

Cheers


I'm not sure what you mean by "tree structure". Presumably you want a
table-oriented object to store data retrieved from the results of an SQL
query?

<script type="text/javascript">
var rows = [
<%
for (var i = 0; i < numberOfRows; i++) {
Response.write(i > 0 ? ",\n[" : "[");
for (var j = 0; j < numberOfColumns; j++) {
Response.write(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>

Some possible issues with the above code (well, aside from the fact that
it is ASP psuedogibberish intended to give you the idea of how to populate
a client-side Array of Arrays from server-side code):
1) If the query results contain a single quote or new line character, it
would generate client-side JavaScript that contains a syntax error. This
could be fixed by inserting '\n' for newline characters and escaping
single quotes.
2) You lose your column names, to retain them, you could store an Object
or Array of objects in each row[] Array entry, but the code gets a bit
more complex.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #2
Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<41***************@agricoreunited.com>...
plork wrote:
I'm trying to code a tree structure using javascript, the nodes of the
tree are generated from a sql table. Has anyone some code for this?

Cheers


I'm not sure what you mean by "tree structure". Presumably you want a
table-oriented object to store data retrieved from the results of an SQL
query?

<script type="text/javascript">
var rows = [
<%
for (var i = 0; i < numberOfRows; i++) {
Response.write(i > 0 ? ",\n[" : "[");
for (var j = 0; j < numberOfColumns; j++) {
Response.write(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>

Some possible issues with the above code (well, aside from the fact that
it is ASP psuedogibberish intended to give you the idea of how to populate
a client-side Array of Arrays from server-side code):
1) If the query results contain a single quote or new line character, it
would generate client-side JavaScript that contains a syntax error. This
could be fixed by inserting '\n' for newline characters and escaping
single quotes.
2) You lose your column names, to retain them, you could store an Object
or Array of objects in each row[] Array entry, but the code gets a bit
more complex.


function getRecordset(sql)
{
var dbConnectionString = "DRIVER={SQL
SERVER};SERVER=name;DATABASE=name;UID=sa;PWD=passw ord"
var sDBConnection = new ActiveXObject("ADODB.Connection");
sDBConnection.Open (dbConnectionString);

var rs = new ActiveXObject("ADODB.Recordset");
rs.CursorLocation = 3;
rs.CursorType = 3;
rs.LockType = 1;
rs.Open(sql, sDBConnection);

return rs;

rs.Close;
}

var rs = getRecordset("SELECT * FROM mytable");

my rs is like this

id name
1 node 1
2 node 2
how do i use this in your for loop code

many thanks
Jul 23 '05 #3
Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<41***************@agricoreunited.com>...
plork wrote:
I'm trying to code a tree structure using javascript, the nodes of the
tree are generated from a sql table. Has anyone some code for this?

Cheers


I'm not sure what you mean by "tree structure". Presumably you want a
table-oriented object to store data retrieved from the results of an SQL
query?

<script type="text/javascript">
var rows = [
<%
for (var i = 0; i < numberOfRows; i++) {
Response.write(i > 0 ? ",\n[" : "[");
for (var j = 0; j < numberOfColumns; j++) {
Response.write(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>

Some possible issues with the above code (well, aside from the fact that
it is ASP psuedogibberish intended to give you the idea of how to populate
a client-side Array of Arrays from server-side code):
1) If the query results contain a single quote or new line character, it
would generate client-side JavaScript that contains a syntax error. This
could be fixed by inserting '\n' for newline characters and escaping
single quotes.
2) You lose your column names, to retain them, you could store an Object
or Array of objects in each row[] Array entry, but the code gets a bit
more complex.


function getRecordset(sql)
{
var dbConnectionString = "DRIVER={SQL
SERVER};SERVER=name;DATABASE=name;UID=sa;PWD=passw ord"
var sDBConnection = new ActiveXObject("ADODB.Connection");
sDBConnection.Open (dbConnectionString);

var rs = new ActiveXObject("ADODB.Recordset");
rs.CursorLocation = 3;
rs.CursorType = 3;
rs.LockType = 1;
rs.Open(sql, sDBConnection);

return rs;

rs.Close;
}

var rs = getRecordset("SELECT * FROM mytable");

mytable is like this
id name
1 node 1
2 node 2

how do i use your for loop to return the array with the field 'name'
Jul 23 '05 #4
plork wrote:
Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<41***************@agricoreunited.com>...
plork wrote:
I'm trying to code a tree structure using javascript, the nodes of the
tree are generated from a sql table. Has anyone some code for this?

Cheers
I'm not sure what you mean by "tree structure". Presumably you want a
table-oriented object to store data retrieved from the results of an SQL
query?

<script type="text/javascript">
var rows = [
<%
for (var i = 0; i < numberOfRows; i++) {
Response.write(i > 0 ? ",\n[" : "[");
for (var j = 0; j < numberOfColumns; j++) {
Response.write(
(j > 0 ? ", " : "") +
"'" +
queryResults.rows[i].columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>


function getRecordset(sql)
{
var dbConnectionString = "DRIVER={SQL
SERVER};SERVER=name;DATABASE=name;UID=sa;PWD=passw ord"
var sDBConnection = new ActiveXObject("ADODB.Connection");
sDBConnection.Open (dbConnectionString);

var rs = new ActiveXObject("ADODB.Recordset");
rs.CursorLocation = 3;
rs.CursorType = 3;
rs.LockType = 1;
rs.Open(sql, sDBConnection);

return rs;

rs.Close;


This rs.Close will never execute. You are returning from your function before it ever gets a chance to run.
}

var rs = getRecordset("SELECT * FROM mytable");

my rs is like this

id name
1 node 1
2 node 2

how do i use this in your for loop code


I have no idea what the properties and methods available on a RecordSet are. You have to determine that, then
you can use that information to generate the output you desire. You can obtain information about the
properties and methods available on the RecordSet object here: <url:
http://msdn.microsoft.com/library/en...jodbrecpme.asp />

Just quickly browsing through the API reveals that the GetRows() method is probably what you want <url:
http://msdn.microsoft.com/library/en...mthgetrows.asp />. It returns a two-dimensional array to
JScript, which is a perfect candidate for a nested loop to output the client-side stuff (or an HTML <table>
or any other construct you want). <url: http://msdn.microsoft.com/library/en...thgetrowsx.asp
/> provides a VB example of how to use GetRows() and a nested loop to Debug.Print the table data separated by
vbCrLf characters. It is, for all practical purposes, identical to the psuedocode I posted (with the
exception that I wrapped my output in characters that browsers could process as client-side JavaScript).

Doing something lik retrieving a set of records from a database and outputting them in an HTML <table> is one
of the simplest types of things to do using server-side technology. It would be a good idea if you did some
research and figured out how to do this before going on to something more complex like outputting a
client-side JavaScript Array of Arrays containing the data.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

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

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
9
by: Steve Edwards | last post by:
Hi, I have a class which contains a single ptr to another instance which is considered its parent, and an array of ptrs to its children class Nodes{ .... Nodes *mSuper;
1
by: hmoundekar | last post by:
actually i want to generate a tree structure on client side, the application is simple web based, and need to display the data extracted from the database to client in tree structure. but the problem...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
4
by: Anni V | last post by:
Hi I request you to kindly help me with the code for a reusable tree structure component that displays a tree view as displayed in the attachment. Consider a table named : Employee with the...
3
by: rymonator | last post by:
Hello, I was wondering how I would convert an existing structure to a tree. What I want is basically a family tree. What I have is the following: array ( array(id => 123, p1 => 567, p2 => 765),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
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
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...

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.