How to Sort or Sub-Total a table field in SMARTFORMS?

Sorting and Sub-totaling to table fields.

We will see how we can sort a given table within the Smart Form and calculate
totals based on a particular field as the sort criterion. In this recipe, we will use an example of
employees and their allowances and amounts.

We define a structure in the database ZST9_EMPLOYEES comprising three
fields PERNR, ALLOWANCE, and AMOUNT. We define a table type also based on this
structure, as shown in the following screenshot:




A table EMPLOYEE_TABLE (based on the defined dictionary table type) is included in the
TABLES tab of the Smart Form interface, and a corresponding work area WA_EMPLOYEE
in the global definition.

A table is then created on the Smart Form layout. The loop of the table is shown as follows:



Appropriate texts are created within the cells in order to print the employee number,
allowance, and amount values passed. A program is then created and the Smart Form is
called. A tabular output is generated.



We will see how sorting along with totaling may be so that the total of each
employee's allowance is printed at the end of each employee's details.


For subtotaling and sorting, proceed as follows:

1. Double-click the defined table node in the left-hand pane. In the right-hand pane, on
the Data tab, within Sort Criteria, select the Event on Sort End checkbox. Also, enter
PERNR as the field name.




2. This will add a PERNR Event on Sort End node.





3. We define a subtotal variable in the global definition. This is a temporary storage
variable for totals of each employee's allowances.



4. On the Calculations tab of the table, we enter the values shown in the
following screenshot:





5. Right-click the PERNR Event on Sort End node to create a table line. Use the same
line type used for the rows of the table (containing three cells).

6. For the second and third cell, we define texts outputting SUBTOTAL and VARIABLE
and SUBTOTAL respectively.

7. Also, a line of code is added after the text that clears the SUBTOTAL variable.





8. The final state will look like the one shown in following screenshot:




The settings we did in the Smart Form will output the subtotal of the allowances of each
employee as shown. At the end of each employee, the total allowances are calculated
and printed.




The entries made on the Calculation tab of the table totals all the amounts of a particular
PERNR field (the SORT criterion) and stores in the SUBTOTAL variable. The output will then be
generated at the EVENT on SORT end. After that, we clear the old value of SUBTOTAL so that
the value of the next employee may be calculated.

No comments:

Post a Comment