Dynamic Columns

Dynamic Columns

SaveToDB products allow showing, hiding, and naming data columns depending on query parameters.

Suppose you have a table with data for all clients.

However, a set of columns is specific for each client or, at least, columns have particular names.

You can configure mappings between clients and column sets to show only the specific columns and hide others when a user changes the client.

For example, a data table (s14.data) has columns:

idclient_idid1id2id3string1string2int1int2float1float2
 

The mapping table (s14.view_aliases) has the following settings:

client_idtable_namecolumn_namealiasis_active
1s14.datafloat1sales
1s14.dataid1state
1s14.datastring1product
2s14.datafloat1sales
2s14.datastring1region
2s14.datastring2manager

To configure the dynamic columns, let's add the mapping table using the DynamicColumns event type in the xls.handlers table:

IDTABLE_SCHEMATABLE_NAMECOLUMN_NAMEEVENT_NAMEHANDLER_SCHEMAHANDLER_NAMEHANDLER_TYPEHANDLER_CODE
 s14dataDynamicColumnss14dynamic_columnsCODE<SQL code>

Where the SQL code is

SELECT
    column_name, alias, is_active
FROM
    s14.view_aliases
WHERE
    client_id = @client_id AND table_name = 's14.data'

As a result, users will see specific column names for each client.

You can use objects of other types instead of the inline SQL as well. Just return the required mappings depending on the parameter value.

The dynamic column handler must return three columns:

  1. Source column name
  2. Column alias
  3. Visibility flag

The visibility flag can have values:

  • NULL to leave the column visible as is
  • 0 to hide the column
  • 1 to show the column