2011年3月30日 星期三

[Code] Dropdown list field in ALV

How to create dropdown list field in ALV grid report.
DATA DEFINITION
TYPE-POOLS: slis, abap, icon.
INCLUDE <icons>.

CONSTANTS: icon_wd_radio_button_empty TYPE icon_l2 VALUE '@SR@'.

DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
alv_container TYPE scrfname VALUE 'ALV_GRID_5000',
alv_grid TYPE REF TO cl_gui_alv_grid,
alv_custom_container TYPE REF TO cl_gui_custom_container,
alv_layout TYPE lvc_s_layo,
alv_field TYPE lvc_s_fcat,
alv_t_field TYPE lvc_t_fcat,
alv_exclude TYPE ui_functions,
alv_dropdown TYPE lvc_t_drop,
alv_variant TYPE disvariant.
INITIALIZATION:
* --------------
* INITIALIZATION
* --------------
INITIALIZATION.
alv_repid = sy-repid.
PERFORM alvdropdown.
Form - alvdropdown:
*&---------------------------------------------------------------------*
*& Form alvdropdown
*&---------------------------------------------------------------------*
FORM alvdropdown .
DATA: ls_dropdown TYPE lvc_s_drop.

* First listbox (handle '1').
ls_dropdown-handle = '1'.
ls_dropdown-value = gt_duty-duty = 'Sales'.
APPEND: ls_dropdown TO alv_dropdown, gt_duty.
ls_dropdown-value = gt_duty-duty = 'SD'.
APPEND: ls_dropdown TO alv_dropdown, gt_duty.
ls_dropdown-value = gt_duty-duty = 'PM'.
APPEND: ls_dropdown TO alv_dropdown, gt_duty.
ls_dropdown-value = gt_duty-duty = 'Shipping'.
APPEND: ls_dropdown TO alv_dropdown, gt_duty.
ls_dropdown-value = gt_duty-duty = 'Customer'.
APPEND: ls_dropdown TO alv_dropdown, gt_duty.
* Secend listbox (handle '2').
ls_dropdown-handle = '2'.
ls_dropdown-value = gt_reason-reason = '01.Order Cancel'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '02.Reture Cancel'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '03.Data Change(Typo)'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '04.Channel Change'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '05.Shipping Date Change'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '06.Price Change'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.
ls_dropdown-value = gt_reason-reason = '07.Others'.
APPEND: ls_dropdown TO alv_dropdown, gt_reason.

ENDFORM. " alvdropdown
Print
*&---------------------------------------------------------------------*
*& Form data_print
*&---------------------------------------------------------------------*
FORM data_print.

CREATE OBJECT alv_custom_container
EXPORTING container_name = alv_container.
CREATE OBJECT alv_grid
EXPORTING i_parent = alv_custom_container.

PERFORM alvfield.
*
CALL METHOD alv_grid->set_drop_down_table
EXPORTING
it_drop_down = alv_dropdown.
* Alv_layout
alv_layout-box_fname = 'SELEC'.
alv_layout-cwidth_opt = 'X'.
alv_layout-stylefname = 'CELLTAB'.
alv_layout-info_fname = 'ROWCOLOR'.
* ALV_Variant
alv_variant-report = alv_repid.
alv_variant-username = sy-uname.

CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
is_variant = alv_variant
i_save = 'A'
is_layout = alv_layout
it_toolbar_excluding = alv_exclude
CHANGING
it_fieldcatalog = alv_t_field
it_outtab = alv_itab[].

* Set editable cells to ready for input initially
CALL METHOD alv_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.

ENDFORM. " data_print

*&---------------------------------------------------------------------*
*& Form alvfield
*&---------------------------------------------------------------------*
FORM alvfield .
DATA: lv_tabix LIKE sy-tabix,
it_fieldcat TYPE slis_t_fieldcat_alv.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = alv_repid
i_internal_tabname = 'ALV_ITAB'
i_inclname = alv_repid
CHANGING
ct_fieldcat = it_fieldcat.

CALL FUNCTION 'LVC_TRANSFER_FROM_SLIS'
EXPORTING
it_fieldcat_alv = it_fieldcat
IMPORTING
et_fieldcat_lvc = alv_t_field
TABLES
it_data = alv_itab.

LOOP AT alv_t_field INTO alv_field.
lv_tabix = sy-tabix.
CASE alv_field-fieldname.
WHEN 'DUTY'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Duty'.
alv_field-edit = 'X'.
alv_field-drdn_hndl = '1'.
alv_field-outputlen = 8 .
WHEN 'REASON'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Reason'.
alv_field-edit = 'X'.
alv_field-drdn_hndl = '2'.
alv_field-outputlen = 21 .
ENDCASE.
MODIFY alv_t_field FROM alv_field INDEX lv_tabix.
ENDLOOP.

ENDFORM. " alvfield