Sorting Data by Using the Tabular Data Control
Microsoft® Windows® 2000 Scripting Guide
One limitation of WMI and the WMI Query Language is that you cannot sort by a particular item. For example, when you retrieve information about the print queues installed on a computer, the returned data is always sorted by print queue name. There is no way to sort by the number of items currently in the queue or by the number of jobs that have been printed.
One way to work around this limitation is to save the data to a comma-separated-values file and then use the tabular data control to display the data. Using the tabular data control, you can specify any sort order you want. This is done by including the SortColumn parameter and specifying the column name (as it appears in the text file header) as the parameter value. For example, adding this line of code to Listing 17.11 sorts the service information by service state:
<PARAM NAME="SortColumn" VALUE="Service State">
To sort by multiple columns (for example, sorting first by service state and then by display name), set the value to the appropriate column names, separating the names with semicolons:
<PARAM NAME="SortColumn" VALUE="Service State;Display Name">
By default, data is sorted in ascending order (A through Z, 0 through 9). To sort in descending order (Z through A, 9 through 0), prefix a minus sign (-) to the column name. For example:
<PARAM NAME="SortColumn" VALUE="-Service State">
To ensure that data is sorted correctly, you can also specify the field type within the text-file header. If you do not specify the field type, all data will be sorted as if it consists of text strings. That means numeric data will be sorted like this:
1
11
2
27
3
Field types recognized by the tabular data control are shown in Table 17.6.
Table 17.6 Field Types Recognized by the Tabular Data Control
Field Type |
Description |
---|---|
String |
Text data. This is the default value if a field type is not specified. |
Date |
Date values. The Date type can be optionally followed by a space and the letters D, M, and Y in any order. These indicate how the date has been formatted. For example, if the date is formatted Day/Month/Year (22/10/2001), you use DMY as the optional parameter. |
Boolean |
True/False or Yes/No values. For True, you can use any of the following: Yes True -1 1 Any nonzero number For False, you can use any of the following: No False 0 |
Int |
Integer (positive or negative) value. |
Float |
Number (positive or negative) values containing a decimal point. The decimal separator used by the float type is determined by the Language property of the operating system. |
To specify field types within the text file header, append a colon and the field type to each field name. For example:
Service Name:Text,Local Service:Boolean,Install Date:Date MDY
Scripting Steps
Listing 17.12 contains a Web page that displays script output by using the tabular data control. In this example, the output is sorted by Service State. To carry out this task, the Web page must include the following:
The starting <HTML> and <BODY> tags.
An <OBJECT> tag used to insert the tabular data control. You must specify an id for the control as well as the CLASSID
clsid:333C7BC4-460F-11D0-BC04-0080C7055A83.
In addition, specify the following parameters:
DataURL, along with the path to the comma-separated-values file.
UseHeader, indicating that the first row in the comma-separated-values file contains header information.
TextQualifier, indicating that the comma is used to separate items within each row of the text file.
SortColumn, which specifies the datafld to be sorted on (Service State).
An <H2> tag used to provide a heading for the page.
A table, with the datasrc set to #serviceList, the id assigned to the tabular data control. This means that table will derive its data from the tabular data control.
A <THEAD> and <TR> tag to mark the first row in the table. Three <TD> tags are used to indicate individual columns in the table:
Computer
Service
Status
A <TBODY> and <TROW> tag used to delineate the data columns. The three columns must specify a datafld used in the header row of the text file:
System Name
Display Name
Service State
Ending tags for the table, body, and HTML.
Listing 17.12 Displaying Sorted Data by Using the Tabular Data Control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|