# Power Table column width binding

**URL:** <https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [May 4, 2017, 7:24pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213 "2017-05-04T19:24:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 4, 2017, 7:24pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/1 "2017-05-04T19:24:58Z")

</div>

Dear all:  
I have two power tables shown on screen, one above and one below. The data shown in both is similar (as in they share the same column names).  
What I would like to be able to do is to resize the columns of one table and the other one should also resize the same column so to match both widths in both tables.  
Hope I made myself clear enough.  
Thanks in advance for your suggestions.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [May 4, 2017, 8:30pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/2 "2017-05-04T20:30:39Z")

</div>

You can put this script in the Initialize extension function of one of your power tables.

```auto
	table2 = self.parent.getComponent("Power Table 2")
	self.table.setColumnModel(table2.table.getColumnModel())

```

However this does make column re-sizing awkward on the second table. You will have to try it to see if that is a problem or not.

A workaround would be to hide the header on the second table since they have the same columns. That way users would have to use the 1st table to resize columns

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 5, 2017, 12:09pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/3 "2017-05-05T12:09:41Z")

</div>

Thanks JGJohnson.  
I’m not sure what the underlying Java objects do in this case, but apparently doing this ties both tables together so I only have to resize one to resize the other. Unfortunately once they are tied the second table won’t responde to resizing on its own columns and, as you mentioned, I should only resize the columns of the first table afterwards.  
Furthermore, now that both tables are tied I don’t know how to untie them!  
Kind of works but not exactly as I wanted it to. It would be nice to resize any of the tables and “copy” the columns sizes to the other table. I’ll look more into it and report any findings.  
Thanks.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 5, 2017, 3:32pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/4 "2017-05-05T15:32:41Z")

</div>

> [@maidamar](#):
>
> Furthermore, now that both tables are tied I don't know how to untie them!

Well, you've discarded the column model for the 2nd table when you replaced it with the first table's model. You'll need to experiment with table dataset refreshes and/or column properties dataset refreshes to see if you can regenerate it.

> [@maidamar](#):
>
> It would be nice to resize any of the tables and "copy" the columns sizes to the other table.

Consider manipulating the column properties dataset at runtime -- possibly by unidirectional bindings from the other table via intermediate custom properties.

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 5, 2017, 3:51pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/5 "2017-05-05T15:51:32Z")

</div>

> [@pturmel](#):
>
> Consider manipulating the column properties dataset at runtime -- possibly by unidirectional bindings from the other table via intermediate custom properties.

Would you hint me in the proper direction as to how to achieve this? I could not find the column width property via scripting. Should I be accesing a property in the underlying Java table? There seem to be a lot of hidden properties for the Power Tables!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 6, 2017, 12:50am UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/6 "2017-05-06T00:50:41Z")

</div>

Every table has a column attributes dataset that is expected to have a row for each column displayed. This dataset is created and updated by the Table Customizer. The table customizer doesn’t show a width setting for columns, but it ends up in this dataset, at the far right. Manually adjust those values by editing the column attributes dataset directly, instead of in the customizer. Adjusting column widths in preview mode updates this dataset, too. You can also programmatically create/edit this dataset to change the appearance of a table on the fly.

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 8, 2017, 12:48pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/7 "2017-05-08T12:48:35Z")

</div>

Are you referring to the columnAttributesData? That dataset has the following columns:

```auto
#Script running from a button on the same screen as the PowerTable
cad = event.source.parent.getComponent('PowerTable').columnAttributesData
print cad.getColumnNames()

>> [name, dateFormat, editable, filterable, hidden, horizontalAlignment, label, locale, numberFormat, prefix, sortable, suffix, treatAsBoolean, verticalAlignment, wrapText]

```

> [@pturmel](#):
>
> The table customizer doesn't show a width setting for columns, but it ends up in this dataset, at the far right.

Could not find this column you mention.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 8, 2017, 2:01pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/8 "2017-05-08T14:01:51Z")

</div>

> [@maidamar](#):
>
> Could not find this column you mention.

Hmm. It's in mine. It might only show up if you drag column widths in the designer. But you can add it yourself.

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 8, 2017, 3:20pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/9 "2017-05-08T15:20:29Z")

</div>

If I add a column to the “Column Attributes Data” property I get a

```auto
java.lang.IllegalArgumentException: No such property: width

```

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [May 11, 2017, 8:07pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/10 "2017-05-11T20:07:50Z")

</div>

Ok, I think I found a way to do what I wanted. It is not exactly the result I hoped to achieve but it’s close enough.

What I do is create a context menu on each of the tables that calls a function defined in the container of both tables (but could be anywhere basically).

```auto
def onPopupTrigger(self, rowIndex, colIndex, colName, value, event):
	"""
	(...)
	"""
	
	def updateColumns(event):
		self.parent.setColumnWidths(self)
		
	menuItemNames = ['Copy widths to other tables'] #Create menu with elements
	menuItemFunctions = [updateColumns] #Assign functions
	menu = system.gui.createPopupMenu(menuItemNames, menuItemFunctions)	#Create menu with above items
	menu.show(event) #Show menu on right click

```

Then, the function that does the magic defined as a _Custom Method_ in the container of the other tables:

```auto
def setColumnWidths(self, table):
	"""
	(...)
	"""
	
	jtable = table.getTable() #javax.swing.JTable
	data = table.viewDataset #viewDataset to replicate
	colNames = list(data.getColumnNames())	#List with column names
	
	#Tables to receive the column widths
	tables = [
		self.getComponent('ptTop'),
		self.getComponent('ptMiddle'),
		self.getComponent('ptBottom')
		]
	
	#Create dataset with column widths from received table
	headers = ['Column','Width']
	values = []
	for col in colNames:
		values.append([colNames.index(col), jtable.getColumn(col).width]) 
	colDataset = system.dataset.toDataSet(headers, values)
	
	#Update the width of the columns in the other tables
	for tbl in tables:
		if tbl != table:	#Only update the other tables
			for row in xrange(colDataset.rowCount):
				tabla.setColumnWidth( colDataset.getValueAt(row, 'Column'), colDataset.getValueAt(row, 'Width') )

```

Not sure if it’s the optimum solutions but it works for now.  
Hope it helps someone else!

---

<div class="post-metadata">

**Author:** ![acasada1](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@acasada1](https://forum.inductiveautomation.com/u/acasada1)\
**Post date:** [September 5, 2017, 3:22pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/11 "2017-09-05T15:22:32Z")

</div>

Shouldn’t the last line of `setColumnWidths` reference `tbl.setColumnWidth`? `tabla` is not referenced anywhere else.

---

<div class="post-metadata">

**Author:** ![maidamar](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@maidamar](https://forum.inductiveautomation.com/u/maidamar)\
**Post date:** [September 7, 2017, 11:04pm UTC](https://forum.inductiveautomation.com/t/power-table-column-width-binding/14213/12 "2017-09-07T23:04:38Z")

</div>

You are right. Sorry, there was a mistranslation from my code.  
The last **`for`** I have it programmed as:

```auto
#Actualizo el ancho de las columnas de las otras tablas
	for tabla in tablas:
		if table != tabla:	#Solo actualizo el resto de las tablas
			for row in xrange(colDataset.rowCount):
				tabla.setColumnWidth( colDataset.getValueAt(row, 'Columna'), colDataset.getValueAt(row, 'Ancho') )ho') )

```
