SAP ABAP - Get Selected Rows ALV Grid
*&---------------------------------------------------------------------*
*& Report ZALVGRID
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALVGRID.
TABLES : ekko.
TYPES :
BEGIN OF t_ekko,
ebeln TYPE ebeln,
bukrs TYPE ekko-bukrs,
bstyp TYPE ekko-bstyp,
bsart TYPE ekko-bsart,
aedat TYPE ekko-aedat,
ernam TYPE ekko-ernam,
bedat TYPE ekko-bedat,
lifnr TYPE ekko-lifnr,
name1 TYPE lfa1-name1,
snote TYPE string,
END OF t_ekko.
DATA :
it_ekko TYPE TABLE OF t_ekko,
wa_ekko TYPE t_ekko.
DATA:
ob_cust TYPE REF TO cl_gui_custom_container,
ob_grid TYPE REF TO cl_gui_alv_grid,
ok_code TYPE sy-ucomm.
DATA:
is_varnt LIKE disvariant,
is_lyout TYPE lvc_s_layo,
is_print TYPE lvc_s_prnt.
DATA:
it_fcatl TYPE lvc_t_fcat, "----- For Field Catalog 1 ALV -----
wa_fcatl LIKE LINE OF it_fcatl,
it_excld TYPE ui_functions.
DATA: vi_fclen TYPE i,
vs_fcfld TYPE string.
DATA: index_rows TYPE lvc_t_row,
index LIKE LINE OF index_rows.
SELECT-OPTIONS :
s_ebeln FOR ekko-ebeln,
s_aedat FOR ekko-aedat,
s_lifnr FOR ekko-lifnr.
START-OF-SELECTION.
PERFORM f_getdata.
CALL SCREEN 9000.
FORM f_getdata.
REFRESH it_ekko.
SELECT
ekko~ebeln
ekko~bukrs
ekko~bstyp
ekko~bsart
ekko~aedat
ekko~ernam
ekko~bedat
ekko~lifnr
lfa1~name1
FROM ekko INNER JOIN lfa1
ON ekko~lifnr = lfa1~lifnr
INTO CORRESPONDING FIELDS OF TABLE it_ekko
WHERE ekko~ebeln IN s_ebeln AND ekko~aedat IN s_aedat
AND ekko~lifnr IN s_lifnr AND ekko~bstyp = 'F' AND ekko~loekz = ''.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'PF900'.
SET TITLEBAR 'TL900'.
PERFORM f_inital.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
CASE sy-ucomm.
WHEN 'BACK' OR 'EXIT' OR 'CNCL'.
LEAVE TO SCREEN 0.
WHEN 'CHANGE'.
IF ekko-bedat IS NOT INITIAL.
CLEAR index_rows. REFRESH index_rows.
CALL METHOD ob_grid->get_selected_rows
IMPORTING
et_index_rows = index_rows.
IF index_rows[] IS INITIAL.
MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
ELSE.
LOOP AT index_rows INTO index.
CLEAR wa_ekko.
READ TABLE it_ekko INTO wa_ekko INDEX index-index.
IF sy-subrc = 0.
CLEAR wa_ekko-bedat.
wa_ekko-bedat = ekko-bedat.
wa_ekko-snote = 'Document date updated'.
MODIFY it_ekko INDEX index-index FROM wa_ekko
TRANSPORTING bedat snote. "WHERE ebeln = wa_ekko-ebeln.
UPDATE ekko SET bedat = ekko-bedat
WHERE ebeln = wa_ekko-ebeln.
COMMIT WORK AND WAIT.
ENDIF.
ENDLOOP.
ENDIF.
ELSE.
MESSAGE 'Please input document date' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.
WHEN 'SALL'.
WHEN 'DALL'.
ENDCASE.
ENDMODULE.
FORM f_inital.
CHECK ok_code <> 'EXIT'.
IF ob_cust IS INITIAL.
CREATE OBJECT ob_cust
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT ob_grid
EXPORTING
i_parent = ob_cust.
ENDIF.
PERFORM to_varnt.
PERFORM to_lyout.
PERFORM f_fieldcat.
PERFORM to_excld CHANGING it_excld.
PERFORM alv_displ_out.
CALL METHOD ob_grid->refresh_table_display.
ENDFORM.
*----------------------------------------------------------------------*
FORM to_varnt .
CLEAR is_varnt.
is_varnt-report = sy-repid.
is_varnt-username = sy-uname.
ENDFORM.
*----------------------------------------------------------------------*
FORM to_lyout .
CLEAR is_lyout.
is_lyout-sel_mode = 'D'.
is_lyout-zebra = 'X'.
is_lyout-cwidth_opt = 'X'.
is_lyout-no_rowins = 'X'.
is_lyout-stylefname = 'CSTYL'.
is_lyout-info_fname = 'CLINE'.
is_lyout-ctab_fname = 'CCELL'.
ENDFORM.
FORM f_fieldcat.
REFRESH it_fcatl.
PERFORM alv_field_cat "NOUT ICON NSUM DSUM EDIT
USING :
* ' XX ' 1 5 'ICONS' '' '' '' '' 'Status',
' X ' 1 30 'EBELN' '' '' '' '' 'Purchasing Doc',
' X ' 2 30 'BUKRS' '' '' '' '' 'Company Code',
' X ' 3 30 'BSTYP' '' '' '' '' 'Doc. Category',
' X ' 4 30 'BSART' '' '' '' '' 'Document Type',
' X ' 5 30 'AEDAT' '' '' '' '' 'Create On',
' X ' 6 30 'ERNAM' '' '' '' '' 'Create By',
' X ' 7 30 'BEDAT' '' '' '' '' 'Document Date',
' X ' 8 30 'LIFNR' '' '' '' '' 'Vendor',
' X ' 9 30 'NAME1' '' '' '' '' 'Vendor Name',
' X ' 10 30 'SNOTE' '' '' '' '' 'Notes'.
ENDFORM.
FORM alv_field_cat USING p_fset p_cpos p_olen p_fild p_rtab p_rfld p_ftyp p_qcif p_text.
CLEAR wa_fcatl.
wa_fcatl-no_out = p_fset+0(1). "01
wa_fcatl-icon = p_fset+1(1). "02
wa_fcatl-no_sum = p_fset+2(1). "03
wa_fcatl-do_sum = p_fset+3(1). "04
wa_fcatl-edit = p_fset+4(1). "05
IF p_olen > 2.
wa_fcatl-outputlen = p_olen. "06
ENDIF.
vs_fcfld = p_fild.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-fieldname = vs_fcfld. "07
vs_fcfld = p_rfld.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-ref_field = vs_fcfld. "09
vs_fcfld = p_rtab.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-ref_table = vs_fcfld. "08
vs_fcfld = p_qcif.
TRANSLATE vs_fcfld TO UPPER CASE.
IF p_ftyp = 'C'.
wa_fcatl-cfieldname = vs_fcfld. "10
ELSEIF p_ftyp = 'Q'.
wa_fcatl-qfieldname = vs_fcfld. "11
ENDIF.
vi_fclen = strlen( p_text ).
IF vi_fclen > 10.
wa_fcatl-scrtext_l = p_text.
ELSE.
wa_fcatl-scrtext_m = p_text.
wa_fcatl-scrtext_s = p_text.
ENDIF.
wa_fcatl-col_pos = p_cpos.
APPEND wa_fcatl TO it_fcatl.
ENDFORM.
FORM alv_displ_out.
CALL METHOD ob_grid->set_table_for_first_display
EXPORTING
is_variant = is_varnt
i_save = 'A'
i_default = 'X'
is_layout = is_lyout
is_print = is_print
it_toolbar_excluding = it_excld
CHANGING
it_outtab = it_ekko
it_fieldcatalog = it_fcatl
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ENDFORM.
FORM to_excld CHANGING pt_exclude TYPE ui_functions.
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_check.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_refresh.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_sum.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_subtot.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_print.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_graph.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_info.
APPEND ls_exclude TO pt_exclude.
ENDFORM.
*& Report ZALVGRID
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALVGRID.
TABLES : ekko.
TYPES :
BEGIN OF t_ekko,
ebeln TYPE ebeln,
bukrs TYPE ekko-bukrs,
bstyp TYPE ekko-bstyp,
bsart TYPE ekko-bsart,
aedat TYPE ekko-aedat,
ernam TYPE ekko-ernam,
bedat TYPE ekko-bedat,
lifnr TYPE ekko-lifnr,
name1 TYPE lfa1-name1,
snote TYPE string,
END OF t_ekko.
DATA :
it_ekko TYPE TABLE OF t_ekko,
wa_ekko TYPE t_ekko.
DATA:
ob_cust TYPE REF TO cl_gui_custom_container,
ob_grid TYPE REF TO cl_gui_alv_grid,
ok_code TYPE sy-ucomm.
DATA:
is_varnt LIKE disvariant,
is_lyout TYPE lvc_s_layo,
is_print TYPE lvc_s_prnt.
DATA:
it_fcatl TYPE lvc_t_fcat, "----- For Field Catalog 1 ALV -----
wa_fcatl LIKE LINE OF it_fcatl,
it_excld TYPE ui_functions.
DATA: vi_fclen TYPE i,
vs_fcfld TYPE string.
DATA: index_rows TYPE lvc_t_row,
index LIKE LINE OF index_rows.
SELECT-OPTIONS :
s_ebeln FOR ekko-ebeln,
s_aedat FOR ekko-aedat,
s_lifnr FOR ekko-lifnr.
START-OF-SELECTION.
PERFORM f_getdata.
CALL SCREEN 9000.
FORM f_getdata.
REFRESH it_ekko.
SELECT
ekko~ebeln
ekko~bukrs
ekko~bstyp
ekko~bsart
ekko~aedat
ekko~ernam
ekko~bedat
ekko~lifnr
lfa1~name1
FROM ekko INNER JOIN lfa1
ON ekko~lifnr = lfa1~lifnr
INTO CORRESPONDING FIELDS OF TABLE it_ekko
WHERE ekko~ebeln IN s_ebeln AND ekko~aedat IN s_aedat
AND ekko~lifnr IN s_lifnr AND ekko~bstyp = 'F' AND ekko~loekz = ''.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'PF900'.
SET TITLEBAR 'TL900'.
PERFORM f_inital.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
CASE sy-ucomm.
WHEN 'BACK' OR 'EXIT' OR 'CNCL'.
LEAVE TO SCREEN 0.
WHEN 'CHANGE'.
IF ekko-bedat IS NOT INITIAL.
CLEAR index_rows. REFRESH index_rows.
CALL METHOD ob_grid->get_selected_rows
IMPORTING
et_index_rows = index_rows.
IF index_rows[] IS INITIAL.
MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
ELSE.
LOOP AT index_rows INTO index.
CLEAR wa_ekko.
READ TABLE it_ekko INTO wa_ekko INDEX index-index.
IF sy-subrc = 0.
CLEAR wa_ekko-bedat.
wa_ekko-bedat = ekko-bedat.
wa_ekko-snote = 'Document date updated'.
MODIFY it_ekko INDEX index-index FROM wa_ekko
TRANSPORTING bedat snote. "WHERE ebeln = wa_ekko-ebeln.
UPDATE ekko SET bedat = ekko-bedat
WHERE ebeln = wa_ekko-ebeln.
COMMIT WORK AND WAIT.
ENDIF.
ENDLOOP.
ENDIF.
ELSE.
MESSAGE 'Please input document date' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.
WHEN 'SALL'.
WHEN 'DALL'.
ENDCASE.
ENDMODULE.
FORM f_inital.
CHECK ok_code <> 'EXIT'.
IF ob_cust IS INITIAL.
CREATE OBJECT ob_cust
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT ob_grid
EXPORTING
i_parent = ob_cust.
ENDIF.
PERFORM to_varnt.
PERFORM to_lyout.
PERFORM f_fieldcat.
PERFORM to_excld CHANGING it_excld.
PERFORM alv_displ_out.
CALL METHOD ob_grid->refresh_table_display.
ENDFORM.
*----------------------------------------------------------------------*
FORM to_varnt .
CLEAR is_varnt.
is_varnt-report = sy-repid.
is_varnt-username = sy-uname.
ENDFORM.
*----------------------------------------------------------------------*
FORM to_lyout .
CLEAR is_lyout.
is_lyout-sel_mode = 'D'.
is_lyout-zebra = 'X'.
is_lyout-cwidth_opt = 'X'.
is_lyout-no_rowins = 'X'.
is_lyout-stylefname = 'CSTYL'.
is_lyout-info_fname = 'CLINE'.
is_lyout-ctab_fname = 'CCELL'.
ENDFORM.
FORM f_fieldcat.
REFRESH it_fcatl.
PERFORM alv_field_cat "NOUT ICON NSUM DSUM EDIT
USING :
* ' XX ' 1 5 'ICONS' '' '' '' '' 'Status',
' X ' 1 30 'EBELN' '' '' '' '' 'Purchasing Doc',
' X ' 2 30 'BUKRS' '' '' '' '' 'Company Code',
' X ' 3 30 'BSTYP' '' '' '' '' 'Doc. Category',
' X ' 4 30 'BSART' '' '' '' '' 'Document Type',
' X ' 5 30 'AEDAT' '' '' '' '' 'Create On',
' X ' 6 30 'ERNAM' '' '' '' '' 'Create By',
' X ' 7 30 'BEDAT' '' '' '' '' 'Document Date',
' X ' 8 30 'LIFNR' '' '' '' '' 'Vendor',
' X ' 9 30 'NAME1' '' '' '' '' 'Vendor Name',
' X ' 10 30 'SNOTE' '' '' '' '' 'Notes'.
ENDFORM.
FORM alv_field_cat USING p_fset p_cpos p_olen p_fild p_rtab p_rfld p_ftyp p_qcif p_text.
CLEAR wa_fcatl.
wa_fcatl-no_out = p_fset+0(1). "01
wa_fcatl-icon = p_fset+1(1). "02
wa_fcatl-no_sum = p_fset+2(1). "03
wa_fcatl-do_sum = p_fset+3(1). "04
wa_fcatl-edit = p_fset+4(1). "05
IF p_olen > 2.
wa_fcatl-outputlen = p_olen. "06
ENDIF.
vs_fcfld = p_fild.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-fieldname = vs_fcfld. "07
vs_fcfld = p_rfld.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-ref_field = vs_fcfld. "09
vs_fcfld = p_rtab.
TRANSLATE vs_fcfld TO UPPER CASE.
wa_fcatl-ref_table = vs_fcfld. "08
vs_fcfld = p_qcif.
TRANSLATE vs_fcfld TO UPPER CASE.
IF p_ftyp = 'C'.
wa_fcatl-cfieldname = vs_fcfld. "10
ELSEIF p_ftyp = 'Q'.
wa_fcatl-qfieldname = vs_fcfld. "11
ENDIF.
vi_fclen = strlen( p_text ).
IF vi_fclen > 10.
wa_fcatl-scrtext_l = p_text.
ELSE.
wa_fcatl-scrtext_m = p_text.
wa_fcatl-scrtext_s = p_text.
ENDIF.
wa_fcatl-col_pos = p_cpos.
APPEND wa_fcatl TO it_fcatl.
ENDFORM.
FORM alv_displ_out.
CALL METHOD ob_grid->set_table_for_first_display
EXPORTING
is_variant = is_varnt
i_save = 'A'
i_default = 'X'
is_layout = is_lyout
is_print = is_print
it_toolbar_excluding = it_excld
CHANGING
it_outtab = it_ekko
it_fieldcatalog = it_fcatl
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ENDFORM.
FORM to_excld CHANGING pt_exclude TYPE ui_functions.
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_check.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_refresh.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_sum.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_subtot.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_print.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_graph.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_info.
APPEND ls_exclude TO pt_exclude.
ENDFORM.
Comments
Post a Comment