Toggle whether a toggle column is hidden or visible in a table?

I have a table that has a bunch of columns in it with a tab strip above which when clicked tells an expression what the first part of the SQL query should be ie what columns should be selected. Due to the way this is set up, other aspects of my window which are just subtotals of the table have to have their own filtering expressions and what not to effectively do a sum({table.data}, column).

Is there a way such that I can just query all the columns, regardless of what the tab strip is selected, and the tab strip just affects what columns are show and what are hidden? It would greatly increase the maintainability of this window which has become expressions on top of expressions in order to create queries.

Using Ignition 7.9.1.

One way you could do it is to make use of the table’s “columnAttributesData.” It is a dataset, so you will have to use a little bit of scripting to get it to work. Each row of this dataset pertains to a column of the table’s currently loaded data.

One of the columns in this dataset is labelled “hidden” and is a boolean. Toggling this will hide the associated column.

I don’t know what your naming scheme is for the tab strip, but a basic script could look something like this:

Place this in the “mouseClicked” section of scripting on your tabstrip.

Both of the sets of code below will only hide one column at a time. To my knowledge you cannot have multiple tabs selected in a tab strip. If this is possible, let me know and i’ll do another example code for hiding multiple columns at the same time.

The following piece of code assumes you are following the standard tab naming scheme of “Tab ###” and that your tabs are in the same order as the columns in your table.(I’m talking about the tab name, not display name.) If this is not the case, look at the section of code under this one.

tab  = event.source.selectedTab
window = system.gui.getWindow(tablewindownamehere)

	if tab != -1 #if a tab is selected, change the associated column to be hidden
		columnData = window.rootContainer.getComponent(tableNameHere).columnAttributesData    #just point to your table's "columnAttributesData". I've written it this way because I normally do calls this way in functions
		
		tmp = tab.split()     #this splits the tab and the tab number. This assumes tab naming scheme is "tab ###"
		tmp = tmp.pop(1)      #pull the number from the tab
		tabNum = int(tmp - 1)     #make the number an int so we can use it and subtract one since the typical tab naming scheme starts at 1 instead of 0
		
		if tabNum <0:     #quick safety check to make sure we dont try to use a negative number
			tabNum = 1
		
		newColumnData = system.dataset.setValue(columnData, tabNum, "Hidden", 1 )	#set the selected column to be hidden
		
		for row in range(newColumnData.getRowCount()):	#cycle through the column list and show all the other columns. This is to undo any previous column hiding
			if row == tabNum:	# we dont want to overwrite the change we just did
				pass
			else:
				newColumnData = system.dataset.setValue(newColumnData, row, "Hidden", 0)	#set the column to be visible
		
		window.rootContainer.getComponent(tableNameHere).columnAttributsData = newColumnData	#write the changed dataset to the table so it will hide the column

The following piece of code assumes that the tab names are the same as that of the column names in your table. This I would recommend because it will still work correctly even if the column order in the table or tab strip is changed.

tab  = event.source.selectedTab
window = system.gui.getWindow(tablewindownamehere)

	if tab != -1 #if a tab is selected, change the associated column to be hidden
		columnData = window.rootContainer.getComponent(tableNameHere).columnAttributesData   #just point to your table's "columnAttributesData". I've written it this way because I normally do calls this way in functions
		columnName = tab
		columnList = []	#initilize a blank column list 
		
		for row in range(columnData.getRowCount()):	#place all the column names into a list so we can pull the column index later
			value = coumnData.getValueAt(row,"name")	#pull the column name. "name" is the first column in the "columnAttributesData". Don't change this
			columnList.append(value)	#add the column name to the list.
		
		columnNum = columnList.index(columnName)	#gives us the row number of the column we want to hide
					
		newColumnData = system.dataset.setValue(columnData, columnNum, "Hidden", 1 )	#set the selected column to be hidden
		
		for row in range(newColumnData.getRowCount()):	#cycle through the column list and show all the other columns. This is to undo any previous column hiding
			if row == columnNum:	# we dont want to overwrite the change we just did
				pass
			else:
				newColumnData = system.dataset.setValue(newColumnData, row, "Hidden", 0)	#set the column to be visible
		
		window.rootContainer.getComponent(tableNameHere).columnAttributsData = newColumnData	#write the changed dataset to the table so it will hide the column

Either of these pieces of code should work. I apologize for any errors in the code as I quickly threw this together as a basic example. Feel free to change it to your liking.

1 Like