Tips and tricks #11: Dynamic table columns

This article describes how to work with dynamic columns in your calculator

This page exists due to the efforts of the following people:

Timur

Timur

Created: 2018-08-07 02:26:40, Last updated: 2022-05-03 13:06:25
Creative Commons Attribution/Share-Alike License 3.0 (Unported)

This content is licensed under Creative Commons Attribution/Share-Alike License 3.0 (Unported). That means you may freely redistribute or modify this content under the same license conditions and must attribute the original author by placing a hyperlink from your site to this work https://planetcalc.com/7864/. Also, please do not modify any references to the original work (if any) contained in this content.

Tips and tricks series

This article may rely on knowledge you should get from previous articles, so you may want to check them first.

Using calculator editor UI, you can create an output table. However, it assumes that you do know table columns in advance. If you don't you need to add them programmatically. To make it easy, table output supports column cloning operation. So, the idea is - add columns that will serve as models for your dynamic columns, hide them from the final view, and use clone operation to add new columns unknown beforehand. Well, of course, it is assumed that new columns somehow depend on input data. Below is the example calculator, where the number of columns in the output table depends on the number of rows in the input table.

Here is the code snippet. Note that it is used in the Display function of calculator, which tailors the controls, not the Calculate function, which does the calculations:

resultTable.DisplayColumn("model",false);
sets.GetValue().forEach(function(item, i) {
    cloneColumn( i, item.label );
});

function cloneColumn( id, name ) {
    resultTable.CloneColumn( "model","set"+id, name);
    resultTable.DisplayColumn("set"+id,true);
}

Line 1 hides the model column from the view (note that to hide column from the chart, you should use DisplaySeries function).
Lines 2-4 use input object "sets" to get data and create new columns based on this data. "sets" data is array, so for each item in the array new column is created using cloneColumn function
Line 7 clones column by calling CloneColumn function on output object "resultTable", which represents, well, result table.
Line 8 shows the newly cloned column.

PLANETCALC, Combinations generator

Combinations generator

Sets

LabelSet
Items per page:

Combinations
 
The file is very large. Browser slowdown may occur during loading and creation.

URL copied to clipboard
PLANETCALC, Tips and tricks #11: Dynamic table columns

Comments