Padmini Posted May 2 Share Posted May 2 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 More sharing options...
Padmini Posted May 2 Author Share Posted May 2 (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 : 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. 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 May 2 by Padmini Link to comment Share on other sites More sharing options...
Solution Alain Martens Posted May 8 Solution Share Posted May 8 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 More sharing options...
Padmini Posted May 9 Author Share Posted May 9 (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 May 9 by Padmini Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now