Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill hta table with array data

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
Hello,

I need to fill an hta table with some data from an array. I found a sample html code that does something similar to what I need. However, the script language is javascript.

How can I convert the javascript to vbscript? The code is listed below:

<HTML>
</head>

<TITLE>Fill Table</TITLE>
<HTA:APPLICATION ID="Tablesample"
APPLICATIONNAME="Tablesample"
BORDER="thick"
BORDERSTYLE="normal"
CAPTION="yes"
ICON="myicon.ico"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="2.0"
WINDOWSTATE="normal">

<body>

<SCRIPT type="text/javascript">

function numberCells()
{
var count=1;
var oTable = document.getElementById('oTable');
var RowsLength = oTable.rows.length;
for (var i=0; i < RowsLength; i++)
{
var oCells = oTable.rows.item(i).cells;
var CellsLength = oCells.length;
for (var j=0; j < CellsLength; j++)
{
oCells.item(j).innerHTML = count++;
}
}
}
</SCRIPT>
<BODY onload="numberCells()">
<TABLE id=oTable border=1>
<TR><TH>&nbsp;</TH><TH>&nbsp;</TH><TH>&nbsp;</TH><TH>&nbsp;</TH></TR>
<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>
<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>
</TABLE>
</BODY>
</HTML>

Any help or direction will be greatly appreciated.


 
I don't quite see what you have against the existing script.

Code:
<SCRIPT type="text/vbscript">

Function Numbercells()

Count=1
Set objTable = Document.Getelementbyid("oTable")
Rowslength = objTable.Rows.Length

    For i=0 To Rowslength-1
    
        Set oCells = oTable.Rows.Item(i).Cells
        Cellslength = oCells.Length
        
        For j=0 To Cellslength-1
            oCells.Item(j).InnerHTML = Count
            Count=Count+1
        Next
    Next

End Function
</SCRIPT>

 
Ramou,
Thank you for the help!

What I really want to do with this code is as follows:

I have my data set in an array. I want to create an hta script to display my data
on the table with one fixed header row and fill the data in the array into the remaining
rows.

I want to display the table with six columns, one header row and ten rows below
the header row on the html page.

Then I want to fill the cells of the ten rows with the data in the array. The number of
fields is fixed at 6, but the number of rows can vary based on the records in the
array. However, I only want to display ten rows of the data in the array on the page
at one time with the ability to scroll down to the remaining of the records with a vertical
scroll bar.

My problem is how can I fill the cells with the data in the array instead of the
data set that is based on the number of the columns and rows set on the html page?

Below is my html code with the vbscript code that you translated from the javascript:

<html>
<head>
<TITLE>Fill Table</TITLE>

<HTA:APPLICATION ID="Tablesample"
APPLICATIONNAME="Tablesample"
BORDER="thick"
SCROLL="no"
BORDERSTYLE="normal"
CAPTION="yes"
ICON="myicon.ico"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="2.0"
WINDOWSTATE="normal">
/>

</head>

<style>

body
{
background-color: buttonface;
font-family: Helvetica;
font-size: 8pt;
margin-top: 25px;
margin-left: 25px;
margin-right: 25px;
margin-bottom: 25px;
}
</style>

<SCRIPT Type="text/vbscript">

Sub Window_Onload
window.reSizeTo 735, 375
self.Focus()
End Sub

Function Numbercells()
Count = 1
Set objTable = Document.Getelementbyid("oTable")
Rowslength = objTable.Rows.Length
For i = 0 To Rowslength - 1
Set oCells = oTable.Rows.Item(i).Cells
Cellslength = oCells.Length
For j = 0 To Cellslength - 1
oCells.Item(j).InnerHTML = Count
Count = Count + 1
Next
Next
End Function

</SCRIPT>

<body>
<table border=1 width=450 bgcolor="FFFFCC" style="table-layout:fixed;font-family:Arial;font-size:9pt;font-weight:normal;text-align:center;border-bottom:none">
<col width=35>
<col width=55>
<col width=100>
<col width=200>
<col width=65>
<col width=200>
<tr>
<td>No.</td>
<td>EUID</td>
<td>User Name</td>
<td>Full Name</td>
<td>Status</td>
<td>Email Address</td>
</tr>
</table>
<table id=oTable border=1 width=450 bgcolor="FFFFCC" style="table-layout:fixed;overflow:auto;font-family:Arial;font-size:9pt;font-weight:normal;text-align:center;">
<col width=35>
<col width=55>
<col width=100>
<col width=200>
<col width=65>
<col width=200>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</table>
<div style="font-size:15px; height:15px;"></div>
<input type="button" value="Fill Cells" onClick="Numbercells">
</body>
</html>

For example, I have my data in an array as below. How can I fill the second table with the data set
from the array instead?

arrData = Array("1", "XYZ001", "User1", "John Doe", "Active", "john.doe@mycompany.com")

Thanks again.
 
A 2 dimensional array might work
Code:
arrdata(1,1)="xyz001"
arrdata(1,2)="user1"
arrdata(1,3)="John Doe"
arrdata(1,4)="Active"
arrdata(1,5)="john.doe@mycompany.com"
arrdata(2,1)="abc123"
arrdata(2,2)="user2"
arrdata(2,3)="Jane Doe"
arrdata(2,4)="Active"
arrdata(2,5)="jane.doe@mycompany.com"
Then in your NumberCells function:

Code:
Function Numbercells()
    Set objTable = Document.Getelementbyid("oTable")
    Rowslength = objTable.Rows.Length
    For i = 0 To Rowslength - 1
        Set oCells = oTable.Rows.Item(i).Cells
        Cellslength = oCells.Length
        For j = 0 To Cellslength - 1
            oCells.Item(j).InnerHTML = arrdata(i,j)
        Next
    Next
End Function

Note this is air code and not tested or anything, just conceptualized.
 
withanh,

Thank you for help.

I already figured it out. Below is my code. I use a text file that contains the records with
the fields separated by a tab character as the data source for the data for my array. I read
this text file into the array and then load the data to the table.

<HTML>
<head>
<TITLE>Fill Table</TITLE>

<HTA:APPLICATION ID="Tablesample"
APPLICATIONNAME="Tablesample"
BORDER="thick"
SCROLL="no"
BORDERSTYLE="normal"
CAPTION="yes"
ICON="myicon.ico"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="2.0"
WINDOWSTATE="normal">
/>

</head>

<style>

body
{
background-color: buttonface;
font-family: Helvetica;
font-size: 8pt;
margin-top: 25px;
margin-left: 25px;
margin-right: 25px;
margin-bottom: 25px;
}
</style>

<SCRIPT type="text/vbscript">

Sub Window_Onload
window.reSizeTo 735, 375
self.Focus()
End Sub

Function Numbercells()
Count = 1
Set objTable = Document.Getelementbyid("oTable")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("User_List.txt",1)
inFile.SkipLine
Do Until inFile.AtEndOfStream
strLine = Replace(inFile.ReadLine,Chr(34),"")
If Len(strLine) Then
ReDim Preserve arrData(i)
arrData(i) = strLine
i = i + 1
End If
Loop
For i = 0 To UBound(arrData)
arrLine = Split(arrData(i),Chr(9))
Set oCells = oTable.Rows.Item(i).Cells
For j = 0 To UBound(arrLine)
If Len(arrLine(j)) Then
oCells.Item(j).InnerHTML = arrLine(j)
End If
Next
Next
End Function

</SCRIPT>

<body>
<table border=1 width=450 bgcolor="FFFFCC" style="table-layout:fixed;font-family:Arial;font-size:9pt;font-weight:normal;text-align:center;border-bottom:none">
<col width=35>
<col width=55>
<col width=100>
<col width=200>
<col width=65>
<col width=200>
<tr>
<td>No.</td>
<td>EUID</td>
<td>User Name</td>
<td>Full Name</td>
<td>Status</td>
<td>Email Address</td>
</tr>
</table>
<table id=oTable border=1 width=450 bgcolor="FFFFCC" style="table-layout:fixed;overflow:auto;font-family:Arial;font-size:9pt;font-weight:normal;text-align:center;">
<col width=35>
<col width=55>
<col width=100>
<col width=200>
<col width=65>
<col width=200>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</table>
<div style="font-size:15px; height:15px;"></div>
<input type="button" value="Fill Cells" onClick="Numbercells">
</body>
</html>


The only problem I have now is how to add the table rows dynamically based on the number of
records I have in my array.

I found a sample code to add the table rows, but it is written in javascript. I need help with
translating this code to vbscript so that it will align with the code I use in the hta script.
If I can get this code to work in vbscript, then I can modify it to work with the records in my array.
The code is listed below:

<html>
<head>
<title>My Application</title>
<HTA:APPLICATION ID="oMyApp"
APPLICATIONNAME="My Application"
BORDER="yes"
CAPTION="yes"
ICON="myicon.ico"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
WINDOWSTATE="normal">
</head>
<script type="text/javascript">
function addRow(content,morecontent)
{
if (!document.getElementsByTagName) return;
tabBody=document.getElementsByTagName("TBODY").item(0);
row=document.createElement("TR");
cell1 = document.createElement("TD");
cell2 = document.createElement("TD");
textnode1=document.createTextNode(content);
textnode2=document.createTextNode(morecontent);
cell1.appendChild(textnode1);
cell2.appendChild(textnode2);
row.appendChild(cell1);
row.appendChild(cell2);
tabBody.appendChild(row);
}
</script>
<body>
<table border='1' id='mytable'>
<tbody>
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</tbody>
</table>
<div style="font-size:15px; height:15px;"></div>
<button onClick='addRow("123","456");return false;'>
Add Row</button>
</body>
</html>


 
Remou,

Thank you for the direction!

But what I need help with now is to be able to add table rows dynamically based on the number of records in my array.

Could you help translate the code above into vbscript? I tried it, but it did work work. It works in javascript though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top