tree structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • plork

    #1

    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
  • Grant Wagner

    #2
    Re: tree structure

    plork wrote:
    [color=blue]
    > 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[/color]

    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.ro ws[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 <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • plork

      #3
      Re: tree structure

      Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<413DEE3A. C593CF06@agrico reunited.com>.. .[color=blue]
      > plork wrote:
      >[color=green]
      > > 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[/color]
      >
      > 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.ro ws[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.[/color]



      function getRecordset(sq l)
      {
      var dbConnectionStr ing = "DRIVER={SQ L
      SERVER};SERVER= name;DATABASE=n ame;UID=sa;PWD= password"
      var sDBConnection = new ActiveXObject(" ADODB.Connectio n");
      sDBConnection.O pen (dbConnectionSt ring);

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

      return rs;

      rs.Close;
      }

      var rs = getRecordset("S ELECT * 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

      Comment

      • plork

        #4
        Re: tree structure

        Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<413DEE3A. C593CF06@agrico reunited.com>.. .[color=blue]
        > plork wrote:
        >[color=green]
        > > 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[/color]
        >
        > 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.ro ws[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.[/color]

        function getRecordset(sq l)
        {
        var dbConnectionStr ing = "DRIVER={SQ L
        SERVER};SERVER= name;DATABASE=n ame;UID=sa;PWD= password"
        var sDBConnection = new ActiveXObject(" ADODB.Connectio n");
        sDBConnection.O pen (dbConnectionSt ring);

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

        return rs;

        rs.Close;
        }

        var rs = getRecordset("S ELECT * 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'

        Comment

        • Grant Wagner

          #5
          Re: tree structure

          plork wrote:
          [color=blue]
          > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<413DEE3A. C593CF06@agrico reunited.com>.. .[color=green]
          > > plork wrote:
          > >[color=darkred]
          > > > 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[/color]
          > >
          > > 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.ro ws[i].columns[j] +
          > > "'"
          > > );
          > > }
          > > Response.write( " ]");
          > > }
          > > %>
          > > ];
          > > </script>[/color]
          >
          > function getRecordset(sq l)
          > {
          > var dbConnectionStr ing = "DRIVER={SQ L
          > SERVER};SERVER= name;DATABASE=n ame;UID=sa;PWD= password"
          > var sDBConnection = new ActiveXObject(" ADODB.Connectio n");
          > sDBConnection.O pen (dbConnectionSt ring);
          >
          > var rs = new ActiveXObject(" ADODB.Recordset ");
          > rs.CursorLocati on = 3;
          > rs.CursorType = 3;
          > rs.LockType = 1;
          > rs.Open(sql, sDBConnection);
          >
          > return rs;
          >
          > rs.Close;[/color]

          This rs.Close will never execute. You are returning from your function before it ever gets a chance to run.
          [color=blue]
          > }
          >
          > var rs = getRecordset("S ELECT * 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[/color]

          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 <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          Working...