Using the Tabular Data Control


Besides the MSHTML DSO, another DSO in the Internet Explorer is the Tabular Data Control (TDC). The TDC enables you to read data in text format. Let's take a look at how that works by placing our employee data in a text file, data.txt. You start that file by indicating the name of each field and its type like this:

 NAME:String;ID:Int;HIRE_DATE:Date;DEPARTMENT:String;TITLE:String 

Then you can store the data for each record, field by field, like this:

(data.txt on the web site)
 NAME:String;ID:Int;HIRE_DATE:Date;DEPARTMENT:String;TITLE:String  Frank;2314;9-2-2003;Shipping;Packer  Martin;2315;9-2-2003;Packing;Programmer  Tom;2316;9-2-2003;Shipping;Packer  Henry;2317;9-2-2003;Shipping;Packer  Paula;2318;9-2-2003;Shipping;Packer 

To use the TDC, you have to refer to its class ID in the <OBJECT> element, using the CLASSID attribute. (Every type of ActiveX control has a unique class ID.) In this example, I'll create a TDC named dso1 . You then configure the TDC with <PARAM> elements in the <OBJECT> element. Here I'm setting the TDC DSO's DataURL property to data.txt, its FieldDelim propertywhich gives the character or characters you've used to delimit data in the data fileto ";" , setting the UseHeader property to "True" to indicate that the DSO can get field names from the first line of data.txt, and telling the DSO to sort the data with the ID field using the Sort property:

 <OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"      ID="dso1" WIDTH=0 HEIGHT=0>      <PARAM NAME="DataURL" VALUE="data.txt">      <PARAM NAME="FieldDelim" VALUE=";">      <PARAM NAME="UseHeader" VALUE="True">      <PARAM NAME="Sort" VALUE="ID">  </OBJECT> 

Now, as we did with the MSHTML DSO, I can bind the Name field of the current record in the TDC DSO to a <SPAN> element like this:

 Name: <SPAN DATASRC="#dso1" DATAFLD="NAME"></SPAN> 

And the rest is just the same as with the MSHTML control. You can use the TDC DSO's recordset property to gain access to the data, and methods such as MoveFirst and MoveNext to navigate as before:

(Listing 17-07.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the Tabular Data Control          </TITLE>      </HEAD>      <BODY>          <H1>              Using the Tabular Data Control           </H1>          <OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"              ID="dso1" WIDTH=0 HEIGHT=0>              <PARAM NAME="DataURL" VALUE="data.txt">              <PARAM NAME="FieldDelim" VALUE=";">              <PARAM NAME="UseHeader" VALUE="True">              <PARAM NAME="Sort" VALUE="ID">          </OBJECT>          Name: <SPAN DATASRC="#dso1" DATAFLD="NAME"></SPAN>          <BR>          ID: <SPAN DATASRC="#dso1" DATAFLD="ID"></SPAN>          <BR>          Department: <SELECT DATASRC="#dso1"              DATAFLD="DEPARTMENT" SIZE=1>              <OPTION VALUE="Shipping">Shipping              <OPTION VALUE="Packing">Packing              <OPTION VALUE="Accounting">Accounting              <OPTION VALUE="Billing">Billing          </SELECT>          <BR>          Title: <SPAN DATASRC="#dso1" DATAFLD="TITLE"></SPAN>          <BR>          Date hired: <SPAN DATASRC="#dso1" DATAFLD="HIRE_DATE"></SPAN><P>          <BR>          <BUTTON ONCLICK="dso1.recordset.MoveFirst()">&lt;&lt;</BUTTON>          <BUTTON ONCLICK="if (!dso1.recordset.BOF)              dso1.recordset.MovePrevious()">&lt;</BUTTON>          <BUTTON ONCLICK="if (!dso1.recordset.EOF)              dso1.recordset.MoveNext()">&gt;</BUTTON>          <BUTTON ONCLICK="dso1.recordset.MoveLast()">&gt;&gt;</BUTTON>      </BODY>  </HTML> 

When you run this code in the Internet Explorer, it looks just like the results you see in Figure 17.7even though we're using an entirely different DSO, and reading our data in an entirely different format.

You also can bind the TDC DSO to HTML tables, just as with the MSHTML DSO. Here's an example that shows how to do that:

(Listing 17-08.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the Tabular Data Control and a Table          </TITLE>      </HEAD>      <BODY>          <H1>              Using the Tabular Data Control and a Table          </H1>          <OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"              ID="dso1" WIDTH=0 HEIGHT=0>              <PARAM NAME="DataURL" VALUE="data.txt">              <PARAM NAME="FieldDelim" VALUE=";">              <PARAM NAME="UseHeader" VALUE="True">              <PARAM NAME="Sort" VALUE="ID">          </OBJECT>          <TABLE DATASRC="#dso1" CELLSPACING="10">              <THEAD>                  <TR>                      <TH>Name</TH>                      <TH>ID</TH>                      <TH>Department</TH>                      <TH>Title</TH>                      <TH>Date hired</TH>                  </TR>              </THEAD>               <TBODY>                   <TR>                          <TD><SPAN DATAFLD="NAME" DATAFORMATAS="HTML">                              </SPAN></TD>                          <TD><SPAN DATAFLD="ID" DATAFORMATAS="HTML">                              </SPAN></TD>                          <TD><SPAN DATAFLD="DEPARTMENT"                               DATAFORMATAS="HTML"></SPAN></TD>                          <TD><SPAN DATAFLD="TITLE"                               DATAFORMATAS="HTML"></SPAN></TD>                          <TD><SPAN DATAFLD="HIRE_DATE" DATAFORMATAS="HTML">                               </SPAN></TD>                     </TR>                </TBODY>          </TABLE>      </BODY>  </HTML> 

Just as with our navigation button example, when you run this code in the Internet Explorer, it looks just like the results we saw for the MSHTML DSO, which you can see in Figure 17.8. Although the results appear the same, we're using an entirely different DSO and reading our data in an entirely different format.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net