ABAP Code Sample For ALV Grid
ABAP Code Sample For ALV Grid
ABAP Code Sample For ALV Grid
The code submitted below enables the display of ALV GRID with additional
options such as:
1) Coloring of individual cells.
2) Making the cell contents invisible.
3) Layout selection at the selection screen level before displaying
the grid.
Summary:
The subroutines
INIT_VARIANT, VARIANT_SELECT,
VARIANT_FILL
I have attached the screen shots of the above mentioned operations in the ALV
GRID. Please do review the content and provide feedback.
report zc9_alvgrid_final_now .
*----------------------------------------------------------------------*PROGRAM ON ALV-GRID.
*----------------------------------------------------------------------*----------------------------------------------------------------------*TABLE DECLARATION.
*----------------------------------------------------------------------*tables:zc9_employee, zc9_empspec.
type-pools: slis.
"ALV Declarations
data: celcol1 type slis_specialcol_alv.
data: syprindex type i value 1.
*----------------------------------------------------------------------*INTERNAL TABLE DECLARATION.
*----------------------------------------------------------------------data: begin of itab_alv1 OCCURS 0,
emp_id(5) TYPE C,
emp_name(20) TYPE C,
certified(3) TYPE C,
celcol type slis_t_specialcol_alv,
selected(1) type c,
end of itab_alv1.
data: begin of itab_alv3 OCCURS 0,
emp_id(5) TYPE C,
emp_name(20) TYPE C,
certified(3) TYPE C,
end of itab_alv3.
data: mulrowtab1 like line of itab_alv1.
data: mulrowtab like table of itab_alv1.
data: wa_emp_name like zc9_employee-emp_name.
* LAYOUT DECLARATION.
*----------------------------------------------------------------------gd_layout-zebra = 'X'.
gd_layout-no_input
= 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-box_fieldname = 'SELECTED'.
gd_layout-box_tabname
= 'ITAB_ALV1'.
gd_layout-totals_text
= 'Totals'(201).
gd_layout-coltab_fieldname = 'CELCOL'.
gd_layout-f2code
= 'DISP'. "Sets fcode for when double
*
"click(press f2)
call function 'REUSE_ALV_FIELDCATALOG_MERGE'
exporting
i_program_name
= gd_repid
i_internal_tabname
= 'ITAB_ALV3'
i_inclname
= gd_repid
changing
ct_fieldcat
= fieldcatalog
.
*----------------------------------------------------------------------*FIELD-CATALOG DECLARATION.
*----------------------------------------------------------------------loop at fieldcatalog into fieldcatalog1.
case fieldcatalog1-fieldname.
when 'EMP_ID'.
fieldcatalog1-seltext_m
fieldcatalog1-col_pos
fieldcatalog1-outputlen
fieldcatalog1-emphasize
fieldcatalog1-key
fieldcatalog1-do_sum
fieldcatalog1-sp_group
= 'EMP-ID'.
= 0.
= 10.
= 'C2'.
= 'X'.
= 'X'.
= 'A'.
when 'EMP_NAME'.
fieldcatalog1-fieldname
fieldcatalog1-seltext_m
fieldcatalog1-col_pos
fieldcatalog1-emphasize
fieldcatalog1-hotspot
fieldcatalog1-key
fieldcatalog1-outputlen
fieldcatalog1-sp_group
= 'EMP_NAME'.
= 'EMPLOYEE_NAME'.
= 1.
= 'C2'.
= 'X'.
= 'X'.
= 20.
= 'B'.
when 'CERTIFIED'.
fieldcatalog1-fieldname
= 'CERTIFIED'.
fieldcatalog1-seltext_m
= 'CERTIFIED'.
fieldcatalog1-col_pos
= 2.
fieldcatalog1-emphasize
= 'C2'.
fieldcatalog1-key
= 'X'.
fieldcatalog1-sp_group
= 'C'.
endcase.
modify fieldcatalog from fieldcatalog1.
endloop.
*----------------------------------------------------------------------*SORTING FIELD CATALOG FIELD.
*----------------------------------------------------------------------ls_sort-spos = 1.
ls_sort-fieldname = 'EMP_ID'.
ls_sort-up = 'X'.
ls_sort-subtot = 'X'.
append ls_sort to lt_sort.
*----------------------------------------------------------------------*POPULATING THE INTERNAL TABLE.
*----------------------------------------------------------------------ITAB_ALV1-EMP_ID = '15530'.
ITAB_ALV1-EMP_NAME = 'THILAKJI'.
ITAB_ALV1-CERTIFIED = 'YES'.
APPEND ITAB_ALV1.
ITAB_ALV1-EMP_ID = '15531'.
ITAB_ALV1-EMP_NAME = 'MATHEWS'.
ITAB_ALV1-CERTIFIED = 'NO'.
APPEND ITAB_ALV1.
ITAB_ALV1-EMP_ID = '15532'.
ITAB_ALV1-EMP_NAME = 'JAISI'.
ITAB_ALV1-CERTIFIED = 'YES'.
APPEND ITAB_ALV1.
ITAB_ALV1-EMP_ID = '15533'.
ITAB_ALV1-EMP_NAME = 'ADAMS'.
ITAB_ALV1-CERTIFIED = 'YES'.
APPEND ITAB_ALV1.
ITAB_ALV1-EMP_ID = '15534'.
ITAB_ALV1-EMP_NAME = 'PAUL'.
ITAB_ALV1-CERTIFIED = 'NO'.
APPEND ITAB_ALV1.
************************************************************************
*
FORM TO MAKE THE CELL CONTENTS INVISIBLE.
PERFORM INVISIBLE_CELL_CONTENTS.
*
************************************************************************
*----------------------------------------------------------------------*EXCLUDE-DECLARATION.
*----------------------------------------------------------------------clear wa_exclude.
wa_exclude-fcode = '&VEXCEL'.
append wa_exclude to i_exclude.
*----------------------------------------------------------------------* FUNCTION MODULE CALL-REUSE-ALV-GRID-DISPLAY.
*----------------------------------------------------------------------call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program
= gd_repid
i_callback_top_of_page
= 'TOP-OF-PAGE2' "see FORM
i_callback_user_command = 'USER_COMMAND'
i_grid_title
= 'ALV GRID-EMPLOYEE'
is_layout
it_fieldcat
it_sort
it_excluding
i_callback_pf_status_set
i_save
=
=
=
=
=
=
gd_layout
fieldcatalog[]
lt_sort
i_exclude
'SET_PF_STATUS'
'A'
"see FORM
is_variant
tables
t_outtab
exceptions
program_error
others
= newvar
= itab_alv1
= 1
= 2.
*-------------------------------------------------------------------*
* Form TOP-OF-PAGE
*
*-------------------------------------------------------------------*
* ALV Report Header
*
*-------------------------------------------------------------------*
form top-of-page2.
data: header type slis_t_listheader,
wa type slis_listheader,
infield like wa-info,
nline type i,
nlinechar(2) type c.
*TITLE AREA
wa-typ = 'H'.
wa-info = 'EMPLOYEE-DETAILS'.
append wa to header.
*BELOW AREA.
wa-typ = 'S'.
wa-key = 'DATE'.
concatenate sy-datum+6(2) '.' sy-datum+4(2) '.' sy-datum(4) into wa-info
.
append wa to header.
*NEXT LINE.
wa-typ = 'S'.
describe table itab_alv1 lines nline.
nlinechar = nline.
wa-key = 'TABLE DETAILS'.
concatenate 'NO.of.Lines:' nlinechar into wa-info separated by space.
append wa to header.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = header
i_logo
= 'ALVLOGO'.
endform.
*------------------------------------------------------------------*
*
FORM SET_PF_STATUS
*
*------------------------------------------------------------------*
form set_pf_status using rt_extab type slis_t_extab.
set pf-status 'NEWSTATUS123'.
endform.
*------------------------------------------------------------------*
*
FORM USER_COMMAND
*
*------------------------------------------------------------------*
*
--> R_UCOMM
*
*
--> RS_SELFIELD
*
*------------------------------------------------------------------*
form user_command using r_ucomm like sy-ucomm
rs_selfield type slis_selfield.
when 'DISP'.
Check field clicked on within ALVgrid report
if rs_selfield-fieldname = 'EMP_ID'.
Read data table, using index of row user clicked on
read table itab_alv1 into wa index rs_selfield-tabindex.
ind = rs_selfield-tabindex.
message i025(zmc) with rs_selfield-fieldname .
elseif rs_selfield-fieldname = 'EMP_NAME'.
read table itab_alv1 into wa index rs_selfield-tabindex.
ind = rs_selfield-tabindex.
message i028(zmc) with rs_selfield-fieldname wa-emp_name .
endif.
when 'EXIT'.
MESSAGE I026(ZMC).
leave program.
when 'CHAN'.
READ TABLE itab_alv1 INTO wa INDEX rs_selfield-tabindex.
read table itab_alv1 into wa index rs_selfield-tabindex.
call screen 1551.
tempvar = 1.
r_ucomm = '&F03'.
when '&F03'.
* IF TEMPVAR = 1.
leave program.
endcase.
clear r_ucomm.
endform.
*&---------------------------------------------------------------------*
*&
Module USER_COMMAND_1551 INPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
module user_command_1551 input.
data: syprtabix type i.
case ok-code.
when 'QUIT'.
set screen 0.
leave screen.
clear itab_alv1.
sy-tabix = 0.
when 'UPDA'.
loop at itab_alv1 where emp_id = wa-emp_id.
syprtabix = sy-tabix.
endloop.
modify itab_alv1 from wa index syprtabix transporting emp_id emp_name
certified.
message i027(zmc).
leave screen.
endcase.
endmodule.
" USER_COMMAND_1551 INPUT
*&---------------------------------------------------------------------*
*&
Module STATUS_1551 OUTPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
module status_1551 output.
set pf-status 'ZNEWSTATUS'.
* SET TITLEBAR 'xxx'.
endmodule.
" STATUS_1551
OUTPUT
vartype
var_save
w_exit
wx_variant
not_found
= 1
program_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.
if w_exit is initial.
vartype-variant = wx_variant-variant.
form_varpar = wx_variant-variant.
endif.
endform.
*&---------------------------------------------------------------------*
*&
Form init_variant
*&---------------------------------------------------------------------*
form init_variant.
clear: vartype.
gd_repid
= sy-repid.
vartype-report
= sy-repid.
vartype-username
= sy-uname.
var_save
= 'A'. "All types
endform.
*&---------------------------------------------------------------------*
*&
Form variant_default
*&---------------------------------------------------------------------*
form variant_default using p_variant.
wx_variant = vartype.
if not p_variant is initial.
wx_variant-variant = p_variant.
endif.
call function 'LVC_VARIANT_DEFAULT_GET'
exporting
i_save
= var_save
changing
cs_variant
= wx_variant
exceptions
wrong_input
= 1
not_found
= 2
program_error = 3
others
= 4.
case sy-subrc.
when 0.
p_variant = wx_variant-variant.
when 2.
clear: p_variant.
endcase.
endform.
*&---------------------------------------------------------------------*
*&
Form variant_fill
*&---------------------------------------------------------------------*
form variant_fill.
clear: vartype.
if LAYOUT is initial.
vartype-variant = 'STANDARD'.
vartype-report = sy-repid.
else.
vartype-variant = LAYOUT.
vartype-report = sy-repid.
call function 'LVC_VARIANT_EXISTENCE_CHECK'
exporting
i_save
= var_save
changing
cs_variant = vartype
exceptions
others
= 01.
if sy-subrc ne 0.
message i000(zz) with text-g02.
endif.
endif.
endform.
*&---------------------------------------------------------------------*
*&
Module STATUS_1234 OUTPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
module status_1234 output.
*
*
endmodule.
" STATUS_1234 OUTPUT
*&---------------------------------------------------------------------*
*&
Module USER_COMMAND_1234 INPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
module user_command_1234 input.
sort mulrowtab.
describe table mulrowtab lines maxlines.
case ok-code1.
when 'OUT1'.
set screen 0.
leave screen.
when 'PUSH'.
clear mulrowtab1.
if mulrowindex <= maxlines.
read table mulrowtab into mulrowtab1 index mulrowindex.
mulrowindex = mulrowindex + 1.
else.
message i030(zmc).
endif.
endcase.
endmodule.
" USER_COMMAND_1234
INPUT
SCREEN SHOTS
Description:
This screen shot displays the ALV GRID with each cells having different color i.e.,
coloring of Individual ALV GRID cells.
The cell contents are made invisible.
Description:
This screen shot displays the layout selection option that could be done before the
display of the grid.