Jump to content
We've recently updated our Privacy Statement, available here. ×

Show/Hide columns in Data Table View


Padmini
Go to solution Solved by Alain Martens,

Recommended Posts

Hello,

I have a question regarding dynamic column selection for a Table view.

I have got many columns in  a table, I want to group them, for example in  n categories. Category 1- col1,col4,col5... Category2 - col2,col3,col6... Category3 - col7,col8,col9..etc

By default, I want to show only col1,col3, col9 (in order)

I already have the order in which columns should be displayed. This order need to be maintained regardless of the order in which the column selection is made by the user.

What is the simplest way I can achieve this?

Thanks.

Link to comment
Share on other sites

Posted (edited)

I have created a table like this-

Categories Columns
Category1 COL1
Category1 COL4
Category1 COL5
Category2 COL2
Category2 COL3
Category2 COL6
Category3 COL7
Category3 COL8
Category3 COL9

Added Hierarchy - Categories and Columns

Added this Hierarchy as Filter in Text Area :

image.png.962b18d0545dc7e52a905e007ed9269b.png

Then, I have created a data function that takes the unique concatenation of filtered columns (from Hierarchy Filter) and passed this to a document property.

image.png.793f15fd06b4fc3b67455c4fb97858af.png

 

The following Iron Python Script is triggered every time the column selection is made from the filter.

from Spotfire.Dxp.Application.Visuals import TablePlot, VisualContent
from Spotfire.Dxp.Data import DataPropertyClass

#get underlying data table
dt=myDataTable.As[VisualContent]()
cols = dt.Data.DataTableReference.Columns

#remove all columns
dt.TableColumns.Clear()

#get document property
selection = Document.Data.Properties.GetProperty(DataPropertyClass.Document, "UniqueConcateColumns").Value
print(selection)

if selection is not None:
	selection_list = selection.split(", ")

	print selection_list

	#get columns (any table is the same. In this case we are using t1)
	cols = dt.Data.DataTableReference.Columns

	for col in selection_list:
	  print col
	  dt.TableColumns.Add(cols[col])

else:
	dt.TableColumns.Clear()

This works fine for adding and removing the columns.

But How can I control the order in which this columns are added to the data table view?

Edited by Padmini
Link to comment
Share on other sites

  • Solution

Hi Padmini, 

You can control the order of the columns by changing the order in the list object before you add them to you Table Visual (in the for loop). 
In your case, suppose the objective is that you need to order the columns alphabetically, then you can add this line in your IronPython script. 
You can of course sort columns in another way, but that would require additional logic to be built in at this part of your script. 
Let me know if this helps. 

Thanks,
Alain

if selection is not None:
	selection_list = selection.split(", ")

        # This will sort the columns alphabetically. 
        selection_list.sort()

	print selection_list
Link to comment
Share on other sites

Posted (edited)
OrderedList=[...] # Define columns in ordered list

# Create a dictionary to map each element of OrderedList to its index
index_map = {value: index for index, value in enumerate(OrderedList)}

if selection is not None:
	selection_list = selection.split(", ")

	# Sort list based on the indices of elements in OrderedList
	sorted_list = sorted(selection_list, key=lambda x: index_map[x])
	print sorted_list

	cols = dt.Data.DataTableReference.Columns

	for col in sorted_list:
	  print col
	  dt.TableColumns.Add(cols[col])

Hello Alain, Thanks for the solution.

I was able to arrange the columns in a particular order using above script. Thank you.

Edited by Padmini
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...