tree structure

Discussion in 'Javascript' started by plork, Sep 7, 2004.

  1. plork

    plork Guest

    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
     
    plork, Sep 7, 2004
    #1
    1. Advertising

  2. plork

    Grant Wagner Guest

    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.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 <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Sep 7, 2004
    #2
    1. Advertising

  3. plork

    plork Guest

    Grant Wagner <> wrote in message news:<>...
    > 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.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
     
    plork, Sep 8, 2004
    #3
  4. plork

    plork Guest

    Grant Wagner <> wrote in message news:<>...
    > 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.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'
     
    plork, Sep 8, 2004
    #4
  5. plork

    Grant Wagner Guest

    plork wrote:

    > Grant Wagner <> wrote in message news:<>...
    > > 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.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.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Sep 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,133
  2. sharan
    Replies:
    4
    Views:
    693
    CBFalconer
    Oct 30, 2007
  3. sharan
    Replies:
    2
    Views:
    835
    SM Ryan
    Oct 31, 2007
  4. sharan
    Replies:
    1
    Views:
    693
    CBFalconer
    Oct 30, 2007
  5. anne001
    Replies:
    1
    Views:
    238
Loading...

Share This Page