tree structure

P

plork

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
 
G

Grant Wagner

plork said:
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.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.
 
P

plork

Grant Wagner said:
plork said:
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.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=password"
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
 
P

plork

Grant Wagner said:
plork said:
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.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=password"
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'
 
G

Grant Wagner

plork said:
Grant Wagner said:
plork said:
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.columns[j] +
"'"
);
}
Response.write(" ]");
}
%>
];
</script>


function getRecordset(sql)
{
var dbConnectionString = "DRIVER={SQL
SERVER};SERVER=name;DATABASE=name;UID=sa;PWD=password"
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-us/ado270/htm/mdobjodbrecpme.asp />

Just quickly browsing through the API reveals that the GetRows() method is probably what you want <url:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.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-us/ado270/htm/mdmthgetrowsx.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top