Applying sorting and subtotaling to table fields

In this recipe, 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.

Getting ready

For this recipe, 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:

Getting ready

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:

Getting ready

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.

Getting ready

In this recipe, 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.

How to do it...

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.
    How to do it...
  2. This will add a PERNR Event on Sort End node.
    How to do it...
  3. We define a subtotal variable in the global definition. This is a temporary storage variable for totals of each employee's allowances.
    How to do it...
  4. On the Calculations tab of the table, we enter the values shown in the following screenshot:
    How to do it...
  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.
    How to do it...
  8. The final state will look like the one shown in following screenshot:
    How to do it...

How it works...

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.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.118.90