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!

Dynamic Table creation issue

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I am trying to create a table based on data from a dictionary object where certain values create a third column.

Here is an example of the table I am trying to achieve, where the Security group values are in a third column.

field | value | Security Group
==================================
name | John | SecGp 1
-------------------------------------------------------------
Job | jobless | SecGP 2
-------------------------------------------------------------
Tel | 0207123456 | SecGP 3
-------------------------------------------------------------
Mobile | 0743433845 |
-------------------------------------------------------------
Department | IT |
----------------------------------------------------

I've been trying to fathom it out. Below is my code. It puts the values in the field column, appending to the bottom of the table.



Code:
Function buildCommitTable()
Set oCmtDict=window.DialogArguments 'Retrieves dictionary object from collateForm() fuction.

table=document.getElementById("cmtTable").hasChildNodes
Set oTable 		= document.createElement("TABLE")
Set oTHead 		= document.createElement("<THEAD CLASS='tHead'>")
Set oTBody0		= document.createElement("TBODY")
Set oTBody1		= document.createElement("TBODY")
Set oTFoot		= document.createElement("TFOOT")
Set oCaption	= document.createElement("CAPTION")

Dim heading(2)
heading(0) = "Field"
heading(1) = "Value"
heading(2) = "Group Membership"

oTable.id="cmtTable"
oTable.appendChild(oTHead)
oTable.appendChild(oTBody0)
oTable.appendChild(oTBody1)
oTable.appendChild(oTFoot)
oTable.appendChild(oCaption)

oTable.border=1

Set oRow = document.createElement("TR")
oTHead.appendChild(oRow)

For Each h In heading
	Set oCell = document.createElement("TH")
	oCell.innerText = h
	oRow.appendChild(oCell)
Next

'This section populates the table
For Each i In oCmtDict
	Set oRow = document.createElement("<TR CLASS='rTable'>")
	oTBody0.appendChild(oRow)
	'For some reason, will not invert with <> or Not??
	If Left(i,2) = "sg" Or _
	   Left(i,2) = "dg" Or _
 	   Left(i,2) = "pg" Then
    	'Column 3 = Security Group 
		Set oCell = document.createElement("<TD CLASS='cSecGroup'>")
		oCell.innerhtml = oCmtDict(i)
		oRow.appendChild(oCell)
	Else
		'Column 1
		Set oCell = document.createElement("<TD CLASS='cField'>")
   		oCell.innerhtml = i
    	oRow.appendChild(oCell)
   		
   		'Column 2 = Group Name
   		Set oCell = document.createElement("<TD CLASS='cValue'>")
   		oCell.innerhtml = oCmtDict(i)
    	oRow.appendChild(oCell) 
	End If
Next

Set oCmtDict = Nothing
Set buildCommitTable = oTableContainer.appendChild(oTable)
End Function

The items I want in the Security Group column begin with sg, dg, or pg.

Any help would be greatly Appreciated.

Many thanks

Woter
 
what does the contents of your dictionary look like?
 
If the key begins with sg or dg or pg, it creates one column filling the value. If the key does not begin with sg or dg or pg, it creates two columns filling the value. The logic is incorrect. _And_, it will never create anything with three columns.
 
'so
dg, SecGp 1
dg, SecGP 2
dg, SecGp-3
name, John
job, jobless


i presume the dictionary only contains the information of one 'person'.
perhaps this would be more logical and would result in only 2 columns,,,which would match your current use of the dictionary as a key and string value

(not the best way but this will at least not change any upstream code/logic)

dg, SecGp 1
dg, SecGP 2
dg, SecGp-3
name, John
job, jobless
strGroups = ""
For Each aKey In dicPerson
If Left(aKey, 2) = "dg" Then
strGroups = strGroups & ";" & dicPerson.Item(aKey)
End If
Next
'
dicPerson.Add "Groups", strGroups
'
For Each aKey In dicPerson
If Left(aKey, 2) <> "dg" Then
Set oCell = document.createElement("<TD CLASS='cField'>")
oCell.innerhtml = i
oRow.appendChild(oCell)

'Column 2 = Group Name
Set oCell = document.createElement("<TD CLASS='cValue'>")
oCell.innerhtml = oCmtDict(i)
oRow.appendChild(oCell)
End If
Next

the method i suggest does not work outright, and will not result in a table as you presented. further i dont like it as it is far too static. ultimately if you name your dictionary key values correctly these would be able to drive the dynamic creation of your fields/columns as well, so no static code about groups etc would be needed, anyway
 
Hi.

Thank you for your replies.

The dictionary contains keys with the field name. e.g. firstname, secondname, telephone, sgx, dgx etc. the items contain the value for the given field.

i'm in a car at the moment. I'll have a detailed look at your suggestion later.

Thanks again.
 
if you want to maintain your table format (which doesnt look right to me but thats just a matter of taste), something like this might work

Set dicGroups = CreateObject("Scripting.Dictionary")
'dicGroups.CompareMode = 1?
strGroups = ""
For Each aKey In dicPerson
If Left(aKey, 2) = "dg" Then
If Not dicGroups.Exists(dicPerson.Item(aKey)) Then
dicGroups.Add dicPerson.Item(aKey), "hmm"
End If
End If
Next
'
i = 0
For Each aKey In dicPerson
If Left(aKey, 2) <> "dg" Then
Set oCell = document.createElement("<TD CLASS='cField'>")
oCell.innerhtml = i
oRow.appendChild(oCell)

'Column 2 = value
Set oCell = document.createElement("<TD CLASS='cValue'>")
oCell.innerhtml = oCmtDict(i)
oRow.appendChild(oCell)
'chuck in one of the groups
If dicGroups.Count >= i Then
Set oCell = document.createElement("<TD CLASS='cGroup'>")
oCell.innerhtml = dicGroups.Item(i)
oRow.appendChild(oCell)
i = i + 1
End If
End If
Next
'we have more groups than other fields? should this occur?
If i < dicGroups.Count Then
For j = i To dicGroups.Count Then
Set oCell = document.createElement("<TD CLASS='cGroup'>")
oCell.innerhtml = dicGroups.Item(j)
oRow.appendChild(oCell)
Next
End If

'that is all pretty ugly and im sure there is a >= missing here or there and perhaps the dicGroups.Item(i) isnt the right syntax for a dictionary, but i hope you get the idea
'ultimately i would say the dictionary object you are being passed just isnt in a nice format
 
Hi mrmovie,

I've had a chance to play with your code. At the moment, it results in the same output as my original. All though I have only implemented the "dg" part I have sg and pg in the table. I believe this should not be the case.

Maybe a little background:
This code is part of an HTA for our 2nd line Helpdesk to create new employee Active Directory user accounts, without using ADUC MMC's and elevated rights. The HTA passes most of the details from an intranet form the employee's manager has completed. One user may belong to many AD security groups that we have divided up into:
[ul]
[li]security groups (sg)[/li]
[li]printer groups (pg)[/li]
[li]distribution groups (dg)[/li]
[/ul]


The 2nd liner checks / completes details such as name, office, job title on a main form. Each of the above groups has it's own child form (showmodaldialog) consisting of a dynamic table listing all the security / distribution groups under each catagory. The 2nd liner selects which groups to add the user to. There is a final summary form with a 'commit' button where all the collected information is then commited to AD.

The information is held in various dictionary objects passed to and from parent / child using showmodaldialog() and window.arguments().

Now that I hope you have a fuller picture, when you mention the dictionary object isn't in a nice format. May I ask for suggested improvments that might make it work a little easier.

Many many thanks.

Woter

 
Hi Woter,
with regards the dictionary that is passed to your Function. Its function solely appear to be the creation of a table, as you are using a key and string value logically this would lead to table consisting of 2 columns and n number of rows (dictionary.count).
so, the process of converting what is more or less a two dimensional array into a 3 column table is flawed, or problematic at best.
my advice would be to change your table to a 2 col one, or perhaps have two tables, one for the details and a seperate one for the groups.
specifically with passing the group information it might be better to have one key and have a delimited value for the all the groups, or have one key and have an array of the groups as its value, or passed the groups to the function as a seperate parameter arrGroups or dicGroups or delimited string
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top