Vous êtes sur la page 1sur 21

Change layout format in ALV

Layout format in ALV defined in structure IS_LAYOUT as importing structure of ALV


REUSE_ALV_LIST_DISPLAY.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
......
is_layout = wa_layout
......
EXCEPTIONS
program_error = 1
OTHERS = 2.


Commonly used field:

- IS_LAYOUT-ZEBRA: X=striped (zebra) pattern
- IS_LAYOUT-BOX_FIELDNAME: insert column checkbox for each row to select row.
How to inset checkbox:
1. insert new field with type C in internal table ALV data to record selection indicator (X:selected).
Example:

DATA: BEGIN OF t_alv OCCURS 1,
field1,
field2,
...
BOX,
END Of T_alv.

2. set IS_LAYOUT-BOX_FIELDNAME = fieldname in internal table ALV data.
Example:

IS_LAYOUT-BOX_FIELDNAME = 'BOX'.

3. At run time, t_alv-box will filled with 'X' for selected data.

CheckBox can be initiated in these ways:
1: selected, can not be changed
0: not selected, can not be changed
X: selected, can be changed
SPACE (blank):not selected, can be changed
-: disable check box

- IS_LAYOUT-COL_FIELDNAME
Cells can be colored individually using a color code which is contained in a column of the internal output table for the
record containing the cell.
Assign the name of the field to this parameter.
The internal output table field must be of type SLIS_T_SPECIALCOL_ALV.
Principle: the color code field is entered for the record containing the cells to be colored. The field contains an internal
table with the above structure, containing the field names of the cells to be colored and the color code. The cell
coordinates are determined by the record position containing the color code and the column information in the color
table.
The record structure of the internal color table of type SLIS_T_SPECIALCOL_ALV is as follows:
Color table-FIELDNAME = field name of the cell to be colored
Color table-COLOR-COL = color number (1 - 9)
Color table-COLOR-INT = bold (0 = off, 1 = on)
Color table-COLOR-INV = inverse (0 = off, 1 = on)
Color table-NOKEYCOL = ignore key coloring ('X' = yes, ' ' = no)
If the parameter color table-FIELDNAME is not filled, the coloring applies to all fields, so the entire record is colored.
----------------------------------------------------------------------------------------------------------------
Tabstrip in selection screen
To define a tabstrip area with tab pages in selection screen, use the following statements in your selection screen
definition:

SELECTION-SCREEN: BEGIN OF TABBED BLOCK FOR LINES,
TAB () USER-COMMAND
[DEFAULT [PROGRAM ] SCREEN ],
TAB () USER-COMMAND
[DEFAULT [PROGRAM ] SCREEN ],
...
END OF BLOCK .



This defines a tabstrip control with size . The tab pages , ... are assigned to the tab area. defines the width of the tab
title. You must assign a function code area to each tab title.

For each tab title, the system automatically creates a character field in the ABAP program with the same name. In
ititialization, you can assign a text to the field. This then appears as the title of the corresponding tab page on the
selection screen.

You must assign a subscreen to each tab title. This will be displayed in the tab area when the user chooses that title.
You can assign one of the following as a subscreen:



1. A subscreen screen defined using the Screen Painter.
The dialog modules containing its flow logic must be defined in the current ABAP program.

2. A selection screen subscreen, defined in an ABAP program.
User actions will trigger the AT SELECTION-SCREEN event at least twice firstly for the "included" selection screen
(we can use it for validation), then for the selection screen on which it appears.

This is simple program with three tab title.
Tab1 demonstrating input validation.
Don't forget to create subscreen 0300 for Tab3.

REPORT ZAALGAL0007.
*http://abap-gallery.blogspot.com

TABLES: sscrfields.

* SUBSCREEN 1 For Tabstrip BUTTON1

SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p1(10) TYPE c,
p2(10) TYPE c,
p3(10) TYPE c.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN END OF SCREEN 100.

* SUBSCREEN 2 For Tabstrip BUTTON2

SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
PARAMETERS: q1(10) TYPE c OBLIGATORY,
q2(10) TYPE c.
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN END OF SCREEN 200.

* Go to tcode SE51 Create screen no 0300
* with screen type = subscreen

* STANDARD SELECTION SCREEN

PARAMETERS: h1(10) TYPE c.

SELECTION-SCREEN: BEGIN OF TABBED BLOCK mytab FOR 10 LINES,
TAB (20) button1 USER-COMMAND push1
DEFAULT SCREEN 100,
TAB (20) button2 USER-COMMAND push2
DEFAULT SCREEN 200,
TAB (20) button3 USER-COMMAND push3
DEFAULT SCREEN 300,
END OF BLOCK mytab.

INITIALIZATION.
button1 = 'This is sel-screen 100'.
button2 = 'This is sel-screen 200'.
button3 = 'This is screen 300'.
mytab-prog = sy-repid.
mytab-dynnr = 100.
mytab-activetab = 'BUTTON1'.

AT SELECTION-SCREEN.
CASE sy-dynnr.
WHEN 1000.
WHEN 100.
IF p1 IS INITIAL.
sscrfields-ucomm = 'PUSH1'.
MESSAGE I888(sabapdocu) WITH 'Make an entry in P1'.
ENDIF.
WHEN 200.
MESSAGE s888(sabapdocu) WITH 'Validation on' sy-dynnr.
ENDCASE.


START-OF-SELECTION.
WRITE: / 'P1:', p1,'Q1:', q1,
/ 'P2:', p2,'Q2:', q2,
/ 'P3:', p3.


Field catalog in ALV
Field catalog containing descriptions of the list output fields. You can use fields of the catalog to determine the number
format and column properties of the list to be displayed.

The field catalog contains more than 60 fields, some of which are only used internally. The field catalog is defined in
the Data Dictionary through table type LVC_T_FCAT.

We can get field description by calling function module REUSE_ALV_FIELDCATALOG_MERGE. You can see the
sample in my post about Simple ALV Report.

In general case, we only use few field to describe output field.
To make it easier I use this form:


FORM f_alv_fieldcatg USING
fu_types "internal table name
fu_fname "Field name of internal table field
fu_reftb "reference table name
fu_refld "reference table field
fu_noout "X= No out
fu_outln "out
fu_fltxt "output length
fu_dosum "X=Do sum (total)
fu_hotsp "X=hotspot on
fu_just. "Justification:L,R,C

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-no_out = fu_noout.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
t_fieldcat-just = fu_just.
APPEND t_fieldcat.
CLEAR t_fieldcat.
ENDFORM. " f_alv_fieldcatg

We can use it in two way:
1. refer to data dictionary:

PERFORM f_alv_fieldcatg USING 'T_REPORT' :
'EQUNR' 'EQUI' 'EQUNR' '' '' '' '' 'X' '',
'SPART' 'VBAK' 'SPART' '' '' '' '' '' ''.

2. or define yourself

PERFORM f_alv_fieldcatg USING 'T_REPORT' :
'FIELD1' '' '' '' '20' 'Text Field1' '' '' ''.


and for field with type number, I use this:

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_number
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_number USING fu_types
fu_fname
fu_noout
fu_outln
fu_no_sign
fu_no_zero
fu_fltxt
fu_dosum
fu_exponent
fu_decimals_out.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-no_zero = fu_no_zero.
t_fieldcat-no_sign = fu_no_sign.
t_fieldcat-exponent = fu_exponent.
t_fieldcat-decimals_out = fu_decimals_out.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-datatype = 'FLTP'.
APPEND t_fieldcat.
CLEAR t_fieldcat.


ENDFORM. " f_alv_fieldcatg_number


For field currency, there are two options, fixed currency or variable currency.

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_curr
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_curr USING fu_types
fu_fname
fu_reftb
fu_refld
fu_noout
fu_outln
fu_fltxt
fu_dosum
fu_hotsp
fu_cfield.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
t_fieldcat-cfieldname = fu_cfield.
t_fieldcat-ctabname = fu_types.
t_fieldcat-no_zero = 'X'.
APPEND t_fieldcat.
CLEAR t_fieldcat.

ENDFORM. " F_FIELDCATG_CURR

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_curr
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_curr_fixed USING fu_types
fu_fname
fu_reftb
fu_refld
fu_noout
fu_outln
fu_fltxt
fu_dosum
fu_hotsp
fu_curr.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
* t_fieldcat-cfieldname = fu_cfield.
* t_fieldcat-ctabname = fu_types.
t_fieldcat-datatype = 'CURR'.
t_fieldcat-currency = fu_curr.
t_fieldcat-no_zero = 'X'.
APPEND t_fieldcat.
CLEAR t_fieldcat.

ENDFORM. " F_FIELDCATG_CURR


Dynamic Table Maintenance
It is a good sample of how to create dynamic table maintenance by utilized SAP function module
"STC1_FULLSCREEN_TABLE_CONTROL".
I copy this code from ITToolbox

You can use it to replace funtion of SM31 with additional benefit.
1. Because it is written in as customer program we can add additional feature, for example change history.
2. There is no need to create screen maintenance for all table because screen will automatically generated.

But, remember, this is only a demonstration of how to use STC1_FULLSCREEN_TABLE_CONTROL. Use it
carefully and only for customer table (Z*).


Because there is difficulty in write "<" and ">" in blogger, I replace "<" with "{" and ">" with "}" in this sample code.
Copy this sample code, then replace all "{" with "<" then replace all "}" with ">".

REPORT ZAALGAL002 NO STANDARD PAGE HEADING.

TYPE-POOLS: rsds.

DATA: is_x030l TYPE x030l,
it_dfies TYPE TABLE OF dfies,
is_dfies TYPE dfies,
it_fdiff TYPE TABLE OF field_dif,
is_fdiff TYPE field_dif.

DATA: w_selid TYPE rsdynsel-selid,
it_tables TYPE TABLE OF rsdstabs,
is_tables TYPE rsdstabs,
it_fields TYPE TABLE OF rsdsfields,
it_expr TYPE rsds_texpr,
it_ranges TYPE rsds_trange,
it_where TYPE rsds_twhere,
is_where TYPE rsds_where,
w_active TYPE i.

DATA: it_content TYPE REF TO data,
it_modif TYPE REF TO data,
it_fcat TYPE lvc_t_fcat.

DATA: w_okcode TYPE sy-ucomm.


FIELD-SYMBOLS: {itab} TYPE STANDARD TABLE,
{ntab} TYPE STANDARD TABLE.


* Macros
DEFINE table_error.
message e398(00) with 'Table' p_table &1.
END-OF-DEFINITION.

DEFINE fixed_val.
is_fdiff-fieldname = is_dfies-fieldname.
is_fdiff-fixed_val = &1.
is_fdiff-no_input = 'X'.
append is_fdiff to it_fdiff.
END-OF-DEFINITION.


* Selection screen
SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME.
PARAMETERS: p_table TYPE tabname OBLIGATORY "table
MEMORY ID dtb
MATCHCODE OBJECT dd_dbtb_16.
SELECTION-SCREEN: BEGIN OF LINE,
PUSHBUTTON 33(20) selopt USER-COMMAND sel,
COMMENT 55(15) selcnt,
END OF LINE.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_rows TYPE i. "rows
SELECTION-SCREEN: END OF BLOCK b01,
SKIP,
BEGIN OF BLOCK b02 WITH FRAME.
PARAMETERS: p_displ TYPE c AS CHECKBOX. "display
SELECTION-SCREEN: END OF BLOCK b02.

* Initialization
INITIALIZATION.
MOVE '@4G@ Filter records' TO selopt.

* PBO
AT SELECTION-SCREEN OUTPUT.
IF w_active IS INITIAL.
CLEAR: selcnt.
ELSE.
WRITE w_active TO selcnt LEFT-JUSTIFIED.
ENDIF.

* PAI
AT SELECTION-SCREEN.
IF p_table NE is_x030l-tabname.
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_table
IMPORTING
x030l_wa = is_x030l
TABLES
dfies_tab = it_dfies
EXCEPTIONS
OTHERS = 1.
IF is_x030l IS INITIAL.
table_error 'does not exist or is not active'.
ELSEIF is_x030l-tabtype NE 'T'.
table_error 'is not selectable'.
ELSEIF is_x030l-align NE 0.
table_error 'has alignment - cannot continue'.
ENDIF.

* Default values for system fields
REFRESH: it_fdiff.
is_fdiff-tabname = p_table.
LOOP AT it_dfies INTO is_dfies.
IF is_dfies-datatype = 'CLNT'.
fixed_val sy-mandt.
ELSEIF is_dfies-rollname = 'ERDAT'
OR is_dfies-rollname = 'ERSDA'
OR is_dfies-rollname = 'AEDAT'
OR is_dfies-rollname = 'LAEDA'.
fixed_val sy-datum.
ELSEIF is_dfies-rollname = 'ERTIM'
OR is_dfies-rollname = 'AETIM'.
fixed_val sy-uzeit.
ELSEIF is_dfies-rollname = 'ERNAM'
OR is_dfies-rollname = 'AENAM'.
fixed_val sy-uname.
ENDIF.
ENDLOOP.

* Prepare free selection on table
REFRESH it_tables.
is_tables-prim_tab = p_table.
APPEND is_tables TO it_tables.

CLEAR: w_selid.
ENDIF.

IF sy-ucomm = 'SEL'.
IF w_selid IS INITIAL.
* Init free selection dialog
CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
expressions = it_expr
IMPORTING
selection_id = w_selid
expressions = it_expr
TABLES
tables_tab = it_tables
EXCEPTIONS
OTHERS = 1.
ENDIF.

* Display free selection dialog
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = w_selid
title = 'Selection'
status = 1
as_window = 'X'
IMPORTING
expressions = it_expr
field_ranges = it_ranges
number_of_active_fields = w_active
TABLES
fields_tab = it_fields
EXCEPTIONS
OTHERS = 1.
ENDIF.


* Start of processing
START-OF-SELECTION.

PERFORM f_create_table USING p_table.

PERFORM f_select_table.

PERFORM f_display_table.


*---------------------------------------------------------------------*
* FORM f_create_table *
*---------------------------------------------------------------------*
FORM f_create_table USING in_tabname.

FIELD-SYMBOLS: {fcat> TYPE lvc_s_fcat.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = in_tabname
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
* Complete field catalog
LOOP AT it_fcat ASSIGNING {fcat>.
{fcat>-tabname = in_tabname.
ENDLOOP.
CALL FUNCTION 'LVC_FIELDCAT_COMPLETE'
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 1.
ELSE.
WRITE: 'Error building field catalog'.
STOP.
ENDIF.

* Create dynamic table for data
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_content.
IF sy-subrc = 0.
ASSIGN it_content->* TO {itab}.
ELSE.
WRITE: 'Error creating internal table'.
STOP.
ENDIF.

* Create dynamic table for modif
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_modif.
IF sy-subrc = 0.
ASSIGN it_modif->* TO {ntab}.
ELSE.
WRITE: 'Error creating internal table'.
STOP.
ENDIF.

ENDFORM.


*---------------------------------------------------------------------*
* FORM f_select_table *
*---------------------------------------------------------------------*
FORM f_select_table.

IF w_active = 0.
SELECT * FROM (p_table)
INTO CORRESPONDING FIELDS OF TABLE {itab}
UP TO p_rows ROWS.
ELSE.
* Selection with parameters
CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
EXPORTING
field_ranges = it_ranges
IMPORTING
where_clauses = it_where.
READ TABLE it_where INTO is_where WITH KEY tablename = p_table.

SELECT * FROM (p_table)
INTO CORRESPONDING FIELDS OF TABLE {itab}
UP TO p_rows ROWS
WHERE (is_where-where_tab).
ENDIF.

IF sy-dbcnt = 0.
WRITE: 'No record selected'.
STOP.
ENDIF.
ENDFORM.


*---------------------------------------------------------------------*
* FORM f_display_table *
*---------------------------------------------------------------------*
FORM f_display_table.
DATA: l_answer TYPE c,
l_eflag TYPE c.

CLEAR: w_okcode.
REFRESH: {ntab}.
* Display table contents
CALL FUNCTION 'STC1_FULLSCREEN_TABLE_CONTROL'
EXPORTING
header = p_table
tabname = p_table
display_only = p_displ
endless = 'X'
no_button = space
IMPORTING
okcode = w_okcode
TABLES
nametab = it_dfies
table = {itab}
fielddif = it_fdiff
modif_table = {ntab}
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
IF p_displ IS INITIAL AND w_okcode = 'SAVE'.
* Confirm update
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = p_table
text_question = 'Do you want to update table ?'
default_button = '2'
display_cancel_button = ' '
IMPORTING
answer = l_answer
EXCEPTIONS
OTHERS = 1.
IF l_answer = '1'.
* Apply modifications
IF NOT {ntab}[] IS INITIAL.
PERFORM f_add_system USING space.
MODIFY (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
l_eflag = 'X'.
ENDIF.
ENDIF.
* Apply deletions
IF l_eflag IS INITIAL.
REFRESH: {ntab}.
CALL FUNCTION 'STC1_GET_DATA'
TABLES
deleted_data = {ntab}
EXCEPTIONS
OTHERS = 1.
IF NOT {ntab}[] IS INITIAL.
DELETE (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
ROLLBACK WORK.
l_eflag = 'X'.
ENDIF.
ENDIF.
ENDIF.
* Apply creations
IF l_eflag IS INITIAL.
REFRESH: {ntab}.
CALL FUNCTION 'STC1_GET_DATA'
TABLES
new_data = {ntab}
EXCEPTIONS
OTHERS = 1.
IF NOT {ntab}[] IS INITIAL.
PERFORM f_add_system USING 'X'.
INSERT (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
ROLLBACK WORK.
l_eflag = 'X'.
ENDIF.
ENDIF.
ENDIF.
IF l_eflag IS INITIAL.
COMMIT WORK.
MESSAGE s261(53).
ELSE.
MESSAGE s075(3i).
PERFORM f_select_table.
ENDIF.
ENDIF.
* Display table again
PERFORM f_display_table.
ENDIF.
ENDIF.

ENDFORM.


*---------------------------------------------------------------------*
* FORM f_add_system *
*---------------------------------------------------------------------*
FORM f_add_system USING new TYPE c.

FIELD-SYMBOLS: {irec} TYPE ANY,
{upd} TYPE ANY.

LOOP AT it_fdiff INTO is_fdiff.
READ TABLE it_dfies INTO is_dfies
WITH KEY fieldname = is_fdiff-fieldname.
LOOP AT {ntab} ASSIGNING {irec}.
ASSIGN COMPONENT is_fdiff-fieldname OF STRUCTURE {irec} TO {upd}.
IF is_dfies-datatype = 'CLNT'.
{upd} = sy-mandt.
ELSE.
CASE is_dfies-rollname.
WHEN 'AENAM'.
{upd} = sy-uname.
WHEN 'AEDAT' OR 'LAEDA'.
{upd} = sy-datum.
WHEN 'AETIM'.
{upd} = sy-uzeit.
WHEN OTHERS.
ENDCASE.
ENDIF.
ENDLOOP.
ENDLOOP.

ENDFORM.


Performance Tuning - Operation on Internal Table
In development server you may not aware of performance tuning on operation on internal table, but it could become a
problem when you are working on internal table containing more 10 thousand rows.

These are tips to improve your program performance.

READ Table WITH Criteria
By default, read command on internal table will read it sequentially. The binary search algorithm helps faster search of
a value in an internal table. But you must sort it before use binary search. Binary search repeatedly divides the search
interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to
the lower half, otherwise the search is narrowed to the upper half.

SORT TABLE BY field1.

READ TABLE table1 WITH KEY field1 = criteria1 BINARY SEARCH.
Do try it for internal table containing more than 10 thousand rows, you will find it significantly improve performance
tuning.

You can apply binary search method to improve performance on nested loop.
Nested loop:


LOOP AT inttab1.
LOOP AT inttab2 WHERE intab2-field1 = intab1-field1.
ENDLOOP.
ENDLOOP.



Replace above code with additional binary search.


SORT inttab2 BY field1.
LOOP AT inttab1.
READ TABLE inttab2 WITH KEY field1 = inttab1-field1 BINARY SEARCH.
CHECK sy-subrc = 0.
LOOP AT inttab2 FROM sy-tabix.
IF inttab2-field1 NE inttab1-field1.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.


Add toolbar button on selection screen

We can add new toolbar (maximal 5) in ABAP report to make program more interactive to user on selection screen.

Do following step.

1. Declare work area sscrfields.

TABLES: sscrfields.

2. Define text displayed in button in initialization event.

INITIALIZATION.
MOVE 'This is button 1' TO sscrfields-functxt_0n." n = 1 up to 5

3. Activate toolbar in selection screen.

SELECTION-SCREEN FUNCTION KEY n.

4. Check user command in AT selection screen

AT SELECTION-SCREEN.
IF sy-ucomm = 'FC0n'." n = 1 up to 5
....
ENDIF.

This is complete sample:

REPORT ZAALGAL0005 .

TABLES: sscrfields.

DATA: d_butt1(4).

PARAMETERS: p_grpa1(10) MODIF ID A,
p_grpa2(10) MODIF ID A,
p_grpb1(10) MODIF ID B.

SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.

INITIALIZATION.
MOVE 'This is button 1' TO sscrfields-functxt_01.
MOVE 'Toggle 1' TO sscrfields-functxt_02.
d_butt1 = 'NO'.

AT SELECTION-SCREEN.
IF sy-ucomm = 'FC01'.
d_butt1 = 'YES'.
sscrfields-ucomm = 'ONLI'.
ELSEIF sy-ucomm = 'FC02'.
IF sscrfields-functxt_02 = 'Toggle 1'.
sscrfields-functxt_02 = 'Toggle 2'.
ELSE.
sscrfields-functxt_02 = 'Toggle 1'.
ENDIF.
ENDIF.

START-OF-SELECTION.
WRITE d_butt1.


Help Value Request (F4)
This sample program demonstrate how to create "Help Value Request". It will appear when user pressing F4 (help) on
an input field to request list of available value.

To do this we are using function module F4IF_INT_TABLE_VALUE_REQUEST. It provide help value request with
following feature:
1. single / multiple choice.
2. update screen without PBO, so it can be used to update more than one field on one request.

Here is the code:

REPORT ZAALGAL0003 .

TABLES: usr02.

parameters: p_bname LIKE usr02-bname,
p_class LIKE usr02-class.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bname.
PERFORM f_valuerequest_vbeln.
*&---------------------------------------------------------------------*
*& Form f_valuerequest_vbeln
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_valuerequest_vbeln.

DATA: BEGIN OF t_data OCCURS 1,
data(20),
END OF t_data.

DATA: lwa_dfies TYPE dfies.

data h_field_wa LIKe dfies.
data h_field_tab like dfies occurs 0 with header line.
data h_dselc like dselc occurs 0 with header line.

SELECT * FROM usr02.
t_data = usr02-bname. APPEND t_data.
t_data = usr02-class. APPEND t_data.
ENDSELECT.

PERFORM f_fieldinfo_get USING 'USR02'
'BNAME'
CHANGING h_field_wa.
APPEND h_field_wa TO h_field_tab.
PERFORM f_fieldinfo_get USING 'USR02'
'CLASS'
CHANGING h_field_wa.
APPEND h_field_wa TO h_field_tab.

h_dselc-fldname = 'BNAME'.
h_dselc-dyfldname = 'P_BNAME'.
APPEND h_dselc.
h_dselc-fldname = 'CLASS'.
h_dselc-dyfldname = 'P_CLASS'.
APPEND h_dselc.

DATA: ld_repid LIKE sy-repid.
ld_repid = sy-repid.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'P_BNAME'
dynpprog = ld_repid
dynpnr = '1000'
dynprofield = 'P_BNAME'
* multiple_choice = ''
* value_org = 'S'
TABLES
value_tab = t_data
field_tab = h_field_tab
* return_tab = return_tab
DYNPFLD_MAPPING = h_dselc
EXCEPTIONS
OTHERS = 0.

ENDFORM. " f_valuerequest_vbeln
*&---------------------------------------------------------------------*
*& Form f_fieldinfo_get
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0079 text
* -->P_0080 text
* <--P_H_FIELD_WA text
*----------------------------------------------------------------------*
FORM f_fieldinfo_get USING fu_tabname
fu_fieldname
CHANGING fwa_field_tab.


CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
TABNAME = fu_tabname
FIELDNAME = fu_fieldname
LFIELDNAME = fu_fieldname
IMPORTING
DFIES_WA = fwa_field_tab
EXCEPTIONS
NOT_FOUND = 1
INTERNAL_ERROR = 2
OTHERS = 3
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


ENDFORM. " f_fieldinfo_get


ABAP sample code provided by SAP
SAP provide a alot of sample code.

For demo about selection screen validation, dynamic selection screen, find them in DEMO_SEL_SCREEN*
For demo about select data, dynamic condition, find them in in DEMO_SELECT*
Demo about write output to list can be found in program DEMO_LIST*

You can find also demo about object oriented programming, tree control, drap & drop menu (contect menu)

To find all the demos, find program including word *DEMO* in program name.

Enjoy exploring SAP demo :)


Event in ABAP Report
Event in ABAP report determine process flow of a program. The events are triggered depended on the way the output
is generated. They begin after event keyword and end when the next event reached.

Event keyword:
INITIALIZATION.
Occurs when report initialized.
We can use it to check user authorization or prepare output for selection screen.



AT SELECTION-SCREEN OUTPUT :
Occurs each time selection screen about to generated.
We can use it to modify selection screen, for example hide / unhide parameter.

AT SELECTION-SCREEN.
Occurs each user command in selection screen. we can use it to perform checking on user input.

START-OF-SELECTION
Occurs after the standard selection screen has been processed.,
data is read in this event.

END-OF-SELECTION
Occurs after start-of-selection.

TOP-OF-PAGE
Occurs when a new page starts.
Use it for write report header.

END-OF-PAGE
Occurs when a page ends.
Use it for write report footer.

AT LINE-SELECTION
Occurs when the user double-click on report.

AT USER-COMMAND
Occurs when the user push toolbar button.

This is program to demonstrate how to use event properly.

REPORT ZAALGAL0008
LINE-COUNT 10(1).

*http://abap-gallery.blogspot.com

TABLES: sflight.

DATA: BEGIN OF t_report OCCURS 3,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
END OF t_report.

*begin selection screen
PARAMETERS p_datum LIKE sy-datum.
PARAMETERS p_check AS CHECKBOX.
*end selection screen

INITIALIZATION.
*begin initialization
MOVE sy-datum TO p_datum.
*end initialization

AT SELECTION-SCREEN.
*begin at selection-screen
MESSAGE I888(sabapdocu) WITH 'At selection-screen'.
IF p_check = 'X'.
MESSAGE E888(sabapdocu) WITH 'Clear checkbox'.
ENDIF.
*end at selection-screen

AT SELECTION-SCREEN OUTPUT.
*begin at selection-screen output
MESSAGE I888(sabapdocu) WITH 'At selection-screen output'.
*end at selection-screen output

START-OF-SELECTION.
*begin start-of-selection.
MESSAGE I888(sabapdocu) WITH 'start-of-selection'.
SELECT * FROM sflight.
MOVE sflight-carrid TO t_report-carrid.
MOVE sflight-connid TO t_report-connid.
APPEND t_report.
ENDSELECT.
*end start-of-selection.

END-OF-SELECTION.
*begin end-of-selection.
MESSAGE I888(sabapdocu) WITH 'end-of-selection'.
FORMAT COLOR col_normal.
DO 30 TIMES.
LOOP AT t_report.
WRITE / t_report-carrid.
WRITE t_report-connid.
ENDLOOP.
ENDDO.
*end end-of-selection.

TOP-OF-PAGE.
FORMAT COLOR col_heading.
WRITE 'This is header'.

END-OF-PAGE.
FORMAT COLOR col_total.
WRITE 'This is footer'.

AT LINE-SELECTION.
WRITE: / 'Cursor Row:', sy-curow.
WRITE: / 'Cursor Col:', sy-cucol.

Vous aimerez peut-être aussi