ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

ABAP ALV Report with Dynamic Column Selection

ABAP

Goal -- WPM

Ready
Exercise Algorithm Area
1REPORT z_alv_dynamic_columns.
2
3TYPES:
4BEGIN OF ts_sales_data,
5order_id TYPE vbeln,
6cust_name TYPE name1,
7order_date TYPE datum,
8net_value TYPE netwr,
9currency TYPE waers,
10END OF ts_sales_data.
11
12TYPES:
13tt_sales_data TYPE STANDARD TABLE OF ts_sales_data WITH EMPTY KEY.
14
15DATA:
16gt_sales_data TYPE tt_sales_data.
17go_alv TYPE REF TO cl_gui_alv_grid.
18go_custom_container TYPE REF TO cl_gui_custom_container.
19gs_layout TYPE lvc_s_layo.
20gt_fieldcat TYPE lvc_t_fcat.
21gs_fcat TYPE lvc_s_fcat.
22gt_variant TYPE disvariant.
23gv_repid TYPE syrepid VALUE sy-repid.
24
25CONSTANTS:
26gc_container_name TYPE scrfname VALUE 'ALV_CONTAINER'.
27
28SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
29SELECT-OPTIONS:
30s_order_id FOR gt_sales_data-order_id,
31s_cust_name FOR gt_sales_data-cust_name,
32s_order_date FOR gt_sales_data-order_date.
33SELECTION-SCREEN END OF BLOCK b1.
34
35START-OF-SELECTION.
36PERFORM fetch_sales_data.
37PERFORM display_alv_report.
38
39*&---------------------------------------------------------------------*
40*& Form FETCH_SALES_DATA
41*&---------------------------------------------------------------------*
42FORM fetch_sales_data.
43" Simulate fetching sales data from a database.
44" In a real application, this would be an Open SQL query.
45gt_sales_data = VALUE #( (
46order_id = '0000000001' cust_name = 'Customer A' order_date = '20230115' net_value = '100.50' currency = 'USD'
47) (
48order_id = '0000000002' cust_name = 'Customer B' order_date = '20230116' net_value = '250.75' currency = 'EUR'
49) (
50order_id = '0000000003' cust_name = 'Customer A' order_date = '20230117' net_value = '150.00' currency = 'USD'
51) (
52order_id = '0000000004' cust_name = 'Customer C' order_date = '20230118' net_value = '300.20' currency = 'GBP'
53) ).
54
55" Apply selection screen criteria.
56DELETE gt_sales_data WHERE order_id NOT IN s_order_id.
57DELETE gt_sales_data WHERE cust_name NOT IN s_cust_name.
58DELETE gt_sales_data WHERE order_date NOT IN s_order_date.
59
60" Handle case where no data is found.
61IF gt_sales_data IS INITIAL.
62MESSAGE 'No sales data found for the given criteria.' TYPE 'I'.
63LEAVE LIST-SCREEN.
64ENDIF.
65ENDFORM.
66
67*&---------------------------------------------------------------------*
68*& Form DISPLAY_ALV_REPORT
69*&---------------------------------------------------------------------*
70FORM display_alv_report.
71" Create ALV grid control.
72CREATE OBJECT go_custom_container
73EXPORTING
74container_name = gc_container_name
75EXCEPTIONS
76cntl_error = 1
77cntl_system_error = 2
78OTHERS = 3.
79
80IF sy-subrc <> 0.
81MESSAGE 'Error creating ALV container.' TYPE 'E'.
82RETURN.
83ENDIF.
84
85CREATE OBJECT go_alv
86EXPORTING
87i_parent = go_custom_container
88EXCEPTIONS
89error = 1
90OTHERS = 2.
91
92IF sy-subrc <> 0.
93MESSAGE 'Error creating ALV grid.' TYPE 'E'.
94RETURN.
95ENDIF.
96
97" Build field catalog dynamically.
98CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
99EXPORTING
100i_structure_name = 'TS_SALES_DATA'
101i_client_economize = abap_true
102CHANGING
103ct_fieldcat = gt_fieldcat
104EXCEPTIONS
105inconsistent_interface = 1
106program_error = 2
107OTHERS = 3.
108
109" Set layout options for column selection.
110gs_layout-colwidth_optimize = abap_true.
111gs_layout-cwidth_all_width = abap_true.
112
113" Enable column chooser.
114gs_layout-box_tab = abap_true.
115
116" Set variant name for saving user settings.
117gt_variant-report = gv_repid.
118
119" Display the ALV grid.
120CALL METHOD go_alv->set_table_for_first_display
121EXPORTING
122is_layout = gs_layout
123it_fieldcatalog = gt_fieldcat
124is_variant = gt_variant
125i_save = 'A' " Allow saving of variants
126CHANGING
127it_outtab = gt_sales_data
128EXCEPTIONS
129invalid_parameter_combination = 1
130program_error = 2
131too_many_lines = 3
132OTHERS = 4.
133
134IF sy-subrc <> 0.
135MESSAGE 'Error displaying ALV grid.' TYPE 'E'.
136ENDIF.
137
138ENDFORM.
Algorithm description viewbox

ABAP ALV Report with Dynamic Column Selection

Algorithm description:

This ABAP program generates an ALV (ABAP List Viewer) report that allows users to dynamically select and arrange columns. It first fetches sample sales data based on selection screen criteria. Then, it dynamically builds the field catalog for the ALV grid. The `set_table_for_first_display` method is used with layout options that enable the column chooser functionality, allowing users to pick which fields to see and in what order. User-defined layouts can be saved using the 'A' option for `i_save`.

Algorithm explanation:

ALV reports are a standard way to display tabular data in SAP. This program leverages the `cl_gui_alv_grid` class for a rich user interface. The key to dynamic column selection lies in the field catalog (`gt_fieldcat`) and layout settings (`gs_layout`). `LVC_FIELDCATALOG_MERGE` automatically generates the field catalog from the structure. Setting `gs_layout-box_tab = abap_true` enables the column chooser. The `i_save = 'A'` parameter in `set_table_for_first_display` allows users to save their chosen layouts as variants. The time complexity for fetching data is O(N) where N is the number of records selected from the database. The ALV display itself is optimized by the SAP GUI framework. Space complexity is O(M) for the field catalog and O(N) for the output table, where M is the number of fields.

Pseudocode:

1. Define a structure for sales data.
2. Define a standard internal table type for sales data.
3. Declare an internal table variable for sales data.
4. Declare ALV control objects and related structures.
5. Define a selection screen for sales order ID, customer name, and order date.
6. In the `FETCH_SALES_DATA` form:
   a. Simulate fetching sales data (or use Open SQL).
   b. Apply selection screen criteria to filter the data.
   c. If no data is found, display a message and exit.
7. In the `DISPLAY_ALV_REPORT` form:
   a. Create a custom container for the ALV grid.
   b. Create an ALV grid object, parenting it to the container.
   c. Dynamically generate the field catalog using `LVC_FIELDCATALOG_MERGE`.
   d. Configure layout options to enable column optimization and the column chooser.
   e. Set a variant name for saving user settings.
   f. Call `set_table_for_first_display` with the data, field catalog, layout, and variant settings, allowing layout saving.
   g. Handle any errors during ALV display.