ALV Reports Part 5: Layout, Sort, Filter, Totals and Variants
Control how an ALV opens: layout options, default sort with subtotals, pre-set filters, totals-only views, and user layout variants (DISVARIANT, i save, default variant on the selection screen with F4 help).
Explanation
📌 KEY POINTS
✅ Default sort and subtotals come from the sort table (lvc_t_sort / slis_t_sortinfo_alv) with subtot = 'X'.
✅ Filters can be pre-set with lvc_t_filt; totals need do_sum in the catalog or a sort with subtotal.
✅ Layout variants (DISVARIANT) let users save column order, sorts and filters; i_save = 'A' allows user-specific and global.
⚠️ Hard-coding a sort the users constantly change is worse than letting them save a default variant.
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Parts 1–4 displayed data. This lesson controls the initial view and lets users keep their own views.\n\n━━━━━━━━━━━━━━━━━━━━\n▶ 1. LAYOUT (LVC_S_LAYO / SLIS_LAYOUT_ALV)\n• zebra, cwidth_opt — readability.
- totals_bef — show totals above the data.
- no_totline — hide the total line.
- sel_mode — selection mode.
- grid_title — title above the grid.
━━━━━━━━━━━━━━━━━━━━
▶ 2. SORT AND SUBTOTALS
lvc_s_sort: spos (sequence), fieldname, up/down, subtot = 'X'. Example: sort by CARRID with subtotal, then CONNID. Columns with do_sum show subtotals at each group change.
━━━━━━━━━━━━━━━━━━━━
▶ 3. FILTERS
lvc_s_filt: fieldname, sign, option, low, high (like a range). Users see the filter icon and can remove it.
━━━━━━━━━━━━━━━━━━━━
▶ 4. LAYOUT VARIANTS
- DISVARIANT with report = sy-repid (and handle if one program has several ALVs).
- i_save: ' ' no saving, 'U' user-specific, 'X' global only, 'A' both.
- A selection-screen parameter p_vari lets users choose a layout before running: F4 via REUSE_ALV_VARIANT_F4 (or LVC_VARIANT_F4), default via REUSE_ALV_VARIANT_DEFAULT_GET in INITIALIZATION.
━━━━━━━━━━━━━━━━━━━━
▶ 5. PASSING THEM TO THE GRID
- REUSE_ALV_GRID_DISPLAY: it_sort, it_filter, is_variant, i_save.
- CL_GUI_ALV_GRID set_table_for_first_display: it_sort, it_filter, is_variant, i_save.
- SALV: get_sorts( ), get_filters( ), get_layout( ).
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Run the code example: data sorted by airline with subtotals.
- Change the layout, save it as default, run again.
- Choose a variant on the selection screen with F4.
✅ CHECKPOINT: your ALVs open with a sensible default view, and users can save and choose their own layouts.
Code example
REPORT zalv_layout_variants. DATA: gt_out TYPE STANDARD TABLE OF sflight, gt_fcat TYPE lvc_t_fcat, gs_layout TYPE lvc_s_layo, gt_sort TYPE lvc_t_sort, gs_sort TYPE lvc_s_sort, gt_filter TYPE lvc_t_filt, gs_filter TYPE lvc_s_filt, gs_variant TYPE disvariant.FIELD-SYMBOLS <ls_fcat> TYPE lvc_s_fcat. * ===== Selection screen: layout variant with F4 and default =====PARAMETERS p_vari TYPE disvariant-variant. INITIALIZATION. gs_variant-report = sy-repid. CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET' EXPORTING i_save = 'A' CHANGING cs_variant = gs_variant EXCEPTIONS OTHERS = 1. IF sy-subrc = 0. p_vari = gs_variant-variant. " user's default layout ENDIF. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari. DATA ls_selected TYPE disvariant. gs_variant-report = sy-repid. CALL FUNCTION 'REUSE_ALV_VARIANT_F4' EXPORTING is_variant = gs_variant i_save = 'A' IMPORTING es_variant = ls_selected EXCEPTIONS OTHERS = 1. IF sy-subrc = 0. p_vari = ls_selected-variant. ENDIF. START-OF-SELECTION. SELECT * FROM sflight INTO TABLE gt_out. CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' EXPORTING i_structure_name = 'SFLIGHT' CHANGING ct_fieldcat = gt_fcat. LOOP AT gt_fcat ASSIGNING <ls_fcat> WHERE fieldname = 'SEATSOCC' OR fieldname = 'PAYMENTSUM'. <ls_fcat>-do_sum = 'X'. " totals + subtotals ENDLOOP. * ===== Layout ===== gs_layout-zebra = 'X'. gs_layout-cwidth_opt = 'X'. gs_layout-totals_bef = 'X'. " totals on top * ===== Default sort with subtotal per airline ===== gs_sort-spos = 1. gs_sort-fieldname = 'CARRID'. gs_sort-up = 'X'. gs_sort-subtot = 'X'. APPEND gs_sort TO gt_sort. CLEAR gs_sort. gs_sort-spos = 2. gs_sort-fieldname = 'CONNID'. gs_sort-up = 'X'. APPEND gs_sort TO gt_sort. * ===== Pre-set filter: only future flights ===== gs_filter-fieldname = 'FLDATE'. gs_filter-sign = 'I'. gs_filter-option = 'GE'. gs_filter-low = sy-datum. APPEND gs_filter TO gt_filter. * ===== Variant chosen on the selection screen ===== gs_variant-report = sy-repid. gs_variant-variant = p_vari. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = gs_layout it_fieldcat_lvc = gt_fcat it_sort_lvc = gt_sort it_filter_lvc = gt_filter is_variant = gs_variant i_save = 'A' TABLES t_outtab = gt_out EXCEPTIONS OTHERS = 1.Real project scenario
A sales manager asked for "the report sorted by region with subtotals, but my team wants it by customer". The developer first added a radio button for each sort, and requests for new sort options kept coming.
Instead, the team enabled layout variants with i_save = 'A', created a global default layout by region, and let each user save their own. The p_vari parameter with F4 on the selection screen let users pick a layout before running. Change requests for sorting stopped.
Common mistakes
- Hard-coding sorts and filters users want to change.
- i_save = ' ' so layouts cannot be saved.
- Missing report in DISVARIANT (variants not found).
- Expecting subtotals without do_sum on the value columns.
- Two ALVs in one program sharing the same variant key (use handle).
Best practices
- Provide a sensible default sort and let users save variants.
- Offer a layout parameter with F4 and default on the selection screen.
- Use handle in DISVARIANT when a program has several grids.
- Mark value columns with do_sum.
- Keep pre-set filters visible and removable.
Interview angle
Questions: "How do you show subtotals in ALV?", "How do layout variants work?", "How do you provide a variant on the selection screen?"
Strong answer: sort table with subtot = 'X' plus do_sum on value columns; filters via lvc_t_filt; DISVARIANT with report/handle and i_save ('U','X','A'); selection-screen parameter with REUSE_ALV_VARIANT_F4 and default from REUSE_ALV_VARIANT_DEFAULT_GET; passed as is_variant to the display.
Practice this lesson
Quick check
1. An ALV lists invoices in USD, EUR and GBP with do_sum on the amount. What makes ALV show separate totals per currency?
2. Which importing parameter of REUSE_ALV_GRID_DISPLAY is used to pass sort and subtotal criteria for the displayed list?
3. In the ALV Grid layout structure (such as LVC_S_LAYO), which field is set to enable alternating background shading between rows?
Interview questions
1. What is a layout variant?
Answer it in your own words first, then compare.
2. A sales ALV shows one grand total for invoices in USD, EUR and GBP. Why is that wrong and how do you fix it?
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- ALV Reports Part 1: ALV Basics with REUSE_ALV_GRID_DISPLAY
- ALV Reports Part 10: Authorization and Sensitive Data in ALV Reports
- ALV Reports Part 2: Quick ALV with CL_SALV_TABLE
- ALV Reports Part 3: OO ALV with CL_GUI_ALV_GRID and a Custom Container
- ALV Reports Part 4: The Field Catalog in Depth
- ALV Reports Part 6: Hotspot and Double-Click Drill-Down
- ALV Reports Part 7: Colors, Icons and Traffic Lights
- ALV Reports Part 8: Toolbar Buttons, PF-STATUS and User Commands