I was facing issues in creating a table with Cell Variant at runtime, after some trial and errors I was able to add cell variants in the table dynamically. The table contains a column, cell of this column can have a dropdown or a TextView depending on the variant key passed in code. Hope this code will help you also.
Step 1: Create a table by using “NEW_TABLE” method of class CL_WD_TABLE. This table has 5 rows and is bound to context node “CN_TABLE”.
Step 2: You can give the table header using SET_HEADER method of class CL_WD_TABLE.
Step 3: Add the table into the root UI element container.
Step 4: Once the table is added into the container, it is time to add the column into this table. Below is the code for creating new table column:
Step 5: You can set the header of the column using SET_HEADER method of class CL_WD_TABLE_COLUMN.
Step 6: Now we will have to create two standard cell, first standard cell will contain a TextView, while the other standard cell will contain a dropdown. Variant key for first cell will be ‘NAPPL’ and for other cell variant key will be ‘SELECT’.
Step 7: Once both standard cells are created, these needs to be assigned to the table column. Method “ADD_CELL_VARIANT” is used for the same.
Step 8: Once cell variants are added to the column, it’s time to create cell editors and bind them to standard table cells.
Step 9: Bind the Cell Variant property of the column to context node, so that it can select the variant according to the data passed.