Adding columns to table from dynamic data

Hello!

I´ve got a script that get data from the query and depending on multiselect filter get more columns...


with this build in the script

   # Build queryBrutal part dynamically from assetData and descriptions
	    queryBrutal = ", ".join(["Asset.{} AS \"{}\"".format(field, field) for field in descrips])
	

and {queryBrutal} in the query

I´m getting more data but the table doesn´t show more columns.

I would like to know how to add columns while I adding data dinamicaly?

this is how I tried to update the table component, but gives a component error in the table
image

image

   table_component = self.parent.getChild("TabContainer").getChild("TablaInventario")
	        if table_component is not None:
	            table_component.props.data = dataset
	            
	            # Manually update the columns property based on the dataset
	            column_headers = dataset.getColumnNames()
	            new_columns = []
	            for header in column_headers:
	                # Create column configurations for each new column
	                new_columns.append({"field": header, "header": header})
	            
	            # Set the columns property to the new columns
	            table_component.props.columns = new_columns

Bind the columns property to your dataset then build the column configuration from this.
A dataset even contains data type information so you can configure the columns accordingly.

But frankly I'd approach the whole thing differently.
Query the whole table, configure the columns however you want, then switch each column's visible property based on whatever you use to select columns in your UI.
This avoids the need to rebuild your query dynamically, and also the need to re-query your db every time the user decides he wants to hide or show a column.

2 Likes

Yes, this idea seem much more straightforward.

I will try your first solution and If don´t get any output try the second one.

Thanks Pascal!

Why not just go for the second one directly ? It's simpler et faster to implement.

edit: It appears some French snuck its way into that post. Can you spot it ?

2 Likes

Yes, you right, let you know later how it´s going, thanks

Ok, I added a binded Expression to the visible property of each column to the dropdown I´m using like:

if(indexOf({.../Flex_Nivel_Expand/Dropdown_1.props.value}, 'Ubicacion') >= 0, true, false)

image

You don't need the if (expression, true, false), expression is enough.

2 Likes