顯示具有 ALV 標籤的文章。 顯示所有文章
顯示具有 ALV 標籤的文章。 顯示所有文章

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

[Code] RadioButton in ALV

How to create radio button 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.
Handler
*---------------------------------------------------------------------*
* CLASS alv_event_handler DEFINITION
*---------------------------------------------------------------------*
CLASS alv_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING
e_row_id
e_column_id
es_row_no.

ENDCLASS. "alv_event_handler

*---------------------------------------------------------------------*
* CLASS alv_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS alv_event_handler IMPLEMENTATION.
* For Radio Button
METHOD handle_hotspot_click.
DATA fieldname TYPE lvc_fname.
DATA index LIKE sy-tabix.
DATA alv_s_itab LIKE alv_itab.
DATA ls_stable TYPE lvc_s_stbl.

fieldname = e_column_id-fieldname.
index = es_row_no-row_id.
READ TABLE alv_itab INDEX index INTO alv_s_itab.

CASE fieldname.
WHEN 'SHIPY'.
IF alv_s_itab-shipy = icon_wd_radio_button_empty.
alv_s_itab-shipy = icon_radiobutton.
alv_s_itab-shipn = icon_wd_radio_button_empty.
ENDIF.
WHEN 'SHIPN'.
IF alv_s_itab-shipn = icon_wd_radio_button_empty.
alv_s_itab-shipn = icon_radiobutton.
alv_s_itab-shipy = icon_wd_radio_button_empty.
ENDIF.
ENDCASE.
MODIFY alv_itab FROM alv_s_itab INDEX index.

CLEAR ls_stable.
ls_stable-col = 'X'.
CALL METHOD alv_grid->refresh_table_display
EXPORTING
is_stable = ls_stable.
ENDMETHOD. "handle_hotspot_click

ENDCLASS. "alv_event_handler IMPLEMENTATION
Event - INITIALIZATION:
* --------------
* INITIALIZATION
* --------------
INITIALIZATION.
alv_repid = sy-repid.
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.
PERFORM alvevent.
* 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'
it_toolbar_excluding = alv_exclude
is_layout = alv_layout
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 'SHIPY'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Shipped'.
alv_field-outputlen = 6.
alv_field-icon = 'X'.
alv_field-hotspot = 'X'.
WHEN 'SHIPN'.
alv_field-scrtext_s = alv_field-scrtext_m =
alv_field-scrtext_l =
alv_field-reptext = 'Non-ship'.
alv_field-outputlen = 6.
alv_field-icon = 'X'.
alv_field-hotspot = 'X'.
ENDCASE.
MODIFY alv_t_field FROM alv_field INDEX lv_tabix.
ENDLOOP.

ENDFORM. " alvfield

*&---------------------------------------------------------------------*
*& Form alvevent
*&---------------------------------------------------------------------*
FORM alvevent .
IF alv_t_events IS INITIAL.
CREATE OBJECT alv_t_events.
ENDIF.

SET HANDLER alv_t_events->handle_hotspot_click FOR alv_grid.
ENDFORM. " alvevent

[Link] Dropdowns in ALV

Simple code for creating dropdown lists for columns in ALV grid outputREPORT zalv_dropdowns

[Link] Radio Buttons in ALV GRID REPORT

Usage of Radio buttons in ALV Report output

[Code] Double Click

雙擊在一行中的任一欄位上:

FORM user_command USING i_ucomm     TYPE syucomm
                         is_selfield TYPE slis_selfield.    "#EC CALLED
  DATA: lv_datum LIKE sy-datum.

  CASE i_ucomm.
    WHEN '&IC1'.             " double click
      READ TABLE gt_itab INDEX is_selfield-tabindex.
      SET PARAMETER ID 'VF'   FIELD gt_itab-vbeln.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
  ENDCASE.

ENDFORM.                               " USER_COMMAND

判斷雙擊在哪一個欄位上:

CASE I_UCOMM.
  WHEN '&IC1'.             " double click
    CASE IS_SELFIELD-SEL_TAB_FIELD.
      WHEN 'GT_ITAB-VGBEL'.
        IF IS_SELFIELD-VALUE IS INITIAL.
        ELSE.
          SET PARAMETER ID 'AUN' FIELD IS_SELFIELD-VALUE.
          CALL TRANSACTION 'VA02' AND SKIP FIRST SCREEN.
        ENDIF.
    ENDCASE.
ENDCASE.

Non-ALV Use Event - AT LINE-SELECTION

DATA: gv_field(20),
      gv_value(18).

* -----------------
* AT LINE-SELECTION
* -----------------
AT LINE-SELECTION.
  GET CURSOR FIELD gv_field VALUE gv_value.
  CASE gv_field.
    WHEN 'GT_ITAB-VBELN'.
      SET PARAMETER ID 'VF' FIELD gv_value.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
    WHEN 'GT_ITAB-VGBEL'.
      SET PARAMETER ID 'VL' FIELD gv_value.
      CALL TRANSACTION 'VL03N' AND SKIP FIRST SCREEN.
  ENDCASE.

------------------------------------------------------
※ Reference HIDE:

Basic form

  HIDE f.

Effect

  The contents of f related to the current output line are stored. If this line is selected,
  f is filled automatically with the stored value.

常用 Transation

VA02:
SET PARAMETER ID 'AUN' FIELD IS_SELFIELD-VALUE.
CALL TRANSACTION 'VA02' AND SKIP FIRST SCREEN.
VA43:
SET PARAMETER ID 'KTN' FIELD IS_SELFIELD-VALUE.
CALL TRANSACTION 'VA43' AND SKIP FIRST SCREEN.
VL03N:
SET PARAMETER ID 'VL'   FIELD IS_SELFIELD-VALUE.
CALL TRANSACTION 'VL03N' AND SKIP FIRST SCREEN.
VF03:
SET PARAMETER ID 'VF'   FIELD gt_itab-vbeln.
CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
VB23:
SET PARAMETER ID 'VK1' FIELD gv_knuma_ag.
CALL TRANSACTION 'VB23' AND SKIP FIRST SCREEN.
FB03:
SET PARAMETER ID 'BLN'  FIELD GV_BELNR.
SET PARAMETER ID 'BUK'  FIELD '1000'.
SET PARAMETER ID 'GJR'  FIELD P_MJAHR.
CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
FK02:
SET PARAMETER ID 'LIF' FIELD V_LIFNR.
SET PARAMETER ID 'BUK' FIELD P_BUKRS.
CALL TRANSACTION 'FK02'.
FK03:
SET PARAMETER ID 'LIF' FIELD V_LIFNR.
SET PARAMETER ID 'BUK' FIELD P_BUKRS.
CALL TRANSACTION 'FK03'.
FK05:
SET PARAMETER ID 'LIF' FIELD V_LIFNR.
SET PARAMETER ID 'BUK' FIELD P_BUKRS.
CALL TRANSACTION 'FK05' AND SKIP FIRST SCREEN.
FK01:
SET PARAMETER ID 'LIF' FIELD V_LIFNR.
SET PARAMETER ID 'BUK' FIELD P_BUKRS.
SET PARAMETER ID 'KGK' FIELD V_KOTKK.
CALL TRANSACTION 'FK01' AND SKIP FIRST SCREEN.
MB03:
SET PARAMETER ID 'MBN'  FIELD is_selfield-value.
SET PARAMETER ID 'MJA'  FIELD gt_itab-budat(4).
CALL TRANSACTION 'MB03' AND SKIP FIRST SCREEN.
ME03:
SET PARAMETER ID 'MAT' FIELD MATNR.
SET PARAMETER ID 'WRK' FIELD WERKS.
CALL TRANSACTION 'ME03' AND SKIP FIRST SCREEN.
ME13:
SET PARAMETER ID 'INF' FIELD XEINE-INFNR.
SET PARAMETER ID 'MAT' FIELD XEINE-MATNR.
SET PARAMETER ID 'LIF' FIELD XEINE-LIFNR.
SET PARAMETER ID 'ESO' FIELD XEINE-ESOKZ.
SET PARAMETER ID 'EKO' FIELD XEINE-EKORG.
SET PARAMETER ID 'WRK' FIELD XEINE-WERKS.
SET PARAMETER ID 'EIB' FIELD SPACE.
CALL TRANSACTION 'ME13' AND SKIP FIRST SCREEN.
ME15:
SET PARAMETER ID 'INF' FIELD XEINE-INFNR.
SET PARAMETER ID 'MAT' FIELD XEINE-MATNR.
SET PARAMETER ID 'LIF' FIELD XEINE-LIFNR.
SET PARAMETER ID 'ESO' FIELD XEINE-ESOKZ.
SET PARAMETER ID 'EKO' FIELD XEINE-EKORG.
SET PARAMETER ID 'WRK' FIELD XEINE-WERKS.
SET PARAMETER ID 'EIB' FIELD 'L'.
CALL TRANSACTION 'ME15' AND SKIP FIRST SCREEN.
ME23:
SET PARAMETER ID 'BES' FIELD EBELN.
CALL TRANSACTION 'ME23' AND SKIP FIRST SCREEN.
ME23N:
READ TABLE gt_itab INDEX is_selfield-tabindex.
SET PARAMETER ID 'BES'   FIELD is_selfield-value.
CALL FUNCTION 'ME_DISPLAY_PURCHASE_DOCUMENT'
  EXPORTING
    i_ebeln gt_itab-ebeln
    i_ebelp gt_itab-ebelp
    i_enjoy 'X'
  EXCEPTIONS
    OTHERS  1.   
ME33:
SET PARAMETER ID 'VRT' FIELD EBELN.
CALL TRANSACTION 'ME33' AND SKIP FIRST SCREEN.
ME43:
SET PARAMETER ID 'ANF' FIELD HIDK-EBELN.
CALL TRANSACTION 'ME43' AND SKIP FIRST SCREEN.
MIR4:
SET PARAMETER ID 'RBN'  FIELD is_selfield-value.
SET PARAMETER ID 'GJR'  FIELD gt_itab-wadat_ist(4).
CALL TRANSACTION 'MIR4' AND SKIP FIRST SCREEN.
MM03:
SET PARAMETER ID 'MXX' FIELD 'KD'. " Table T132: Status definition
SET PARAMETER ID 'WRK' FIELD p_werks.
SET PARAMETER ID 'MAT' FIELD KABT-MATNR.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
MMBE:
SET PARAMETER ID 'MAT' FIELD LIST-MATNR.
SET PARAMETER ID 'WRK' FIELD LIST-WERKS.
SET PARAMETER ID 'LAG' FIELD ''.
CALL TRANSACTION 'MMBE' AND SKIP FIRST SCREEN.
KOB1:
SET PARAMETER ID 'ANR' FIELD V_AUFNR.
SET PARAMETER ID 'KAG' FIELD V_KAGRU.
CALL TRANSACTION 'KOB1'. "AND SKIP FIRST SCREEN.
KO88:
SET PARAMETER ID 'ANR' FIELD V_AUFNR.
CALL TRANSACTION 'KO88'.
CO02:
SET PARAMETER ID 'ANR' FIELD V_AUFNR.
CALL TRANSACTION 'CO02' AND SKIP FIRST SCREEN.
CS03:
SET PARAMETER ID 'MAT' FIELD gt_itab-idnrk.
SET PARAMETER ID 'WRK' FIELD p_werks.
SET PARAMETER ID 'CSV' FIELD gt_itab-stlan.
CALL TRANSACTION 'CS03' AND SKIP FIRST SCREEN.

[Code] Quick Reference for ALV_LIST report.

快速建立 ALV_LIST 報表
以下分為幾個段落,可以快速的建立 ALV_LIST 的報表。

Define

TYPE-POOLS: slis.

* Global variables for handling ALV functionality
CONSTANTS: alv_status   TYPE slis_formname VALUE 'SET_STATUS',
           alv_ucommd   TYPE slis_formname VALUE 'USER_COMMAND'.
DATA:      alv_repid    LIKE sy-repid,
           alv_field    TYPE slis_fieldcat_alv,
           alv_event    TYPE slis_alv_event,
           alv_layout   TYPE slis_layout_alv,
           alv_sort     TYPE slis_sortinfo_alv,
           alv_print    TYPE slis_print_alv,
           alv_t_field  TYPE slis_t_fieldcat_alv,
           alv_t_events TYPE slis_t_event,
           alv_t_sort   TYPE slis_t_sortinfo_alv.

INITIALIZATION

*----------------
* INITIALIZATION
*----------------
INITIALIZATION.
  alv_repid = sy-repid.

Data_Print

報表顯示的資料請放在 gt_itab 的 Internal table 裡面。
FORM data_print.
  PERFORM alvevent.
  PERFORM alvfield.
  PERFORM alvsort.

  alv_layout-colwidth_optimize = 'X'.
  alv_layout-get_selinfos      = 'X'.
  alv_layout-zebra             = 'X'.
*  alv_layout-numc_sum          = 'X'
*  alv_layout-box_fieldname     = 'SELEC'.
*  alv_layout-box_tabname       = 'GT_VBRK'.
*  alv_layout-f2code            = '&F2C'.
*  alv_layout-no_totalline      = 'X'.
*  alv_layout-group_change_edit = 'X'.

  alv_print-no_print_listinfos = 'X'.
  alv_print-no_print_selinfos  = 'X'.
  alv_print-no_coverpage       = 'X'.

  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      i_callback_program       = alv_repid
      i_callback_pf_status_set = alv_status
      i_callback_user_command  = alv_ucommd
      it_fieldcat              = alv_t_field
      i_save                   = 'A'
      it_events                = alv_t_events
      is_layout                = alv_layout
      it_sort                  = alv_t_sort
      is_print                 = alv_print
    TABLES
      t_outtab                 = gt_itab
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  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.

ALV_EVENT

*&---------------------------------------------------------------------*
*&      Form  alvevent
*&---------------------------------------------------------------------*
*       ALV Event
*----------------------------------------------------------------------*
FORM alvevent .
  CLEAR alv_event.
  alv_event-name = slis_ev_top_of_page.
  alv_event-form = 'LIST_HEADER'.
  APPEND alv_event TO alv_t_events.
ENDFORM.                    "alvevent

ALV_FIELDS

Fields 也有 Function 可以直接建立,只是手動建立的自主性較高。
*&---------------------------------------------------------------------*
*&      Form  alvfield
*&---------------------------------------------------------------------*
*       ALV Fields
*----------------------------------------------------------------------*
FORM alvfield .
  DATA: l_count TYPE i.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'FKDAT'.
  alv_field-seltext_s     = 'DATE'.
  alv_field-key           = 'X'.             " define field as key
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'FKIMG'.
  alv_field-seltext_s     = 'Quantity'.
  alv_field-decimals_out  = '0'.
  alv_field-do_sum        = 'X'.
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'AMOU0'.
  alv_field-seltext_s     = 'Amount'.
  alv_field-currency      = 'TWD'.
  " alv_field-cfieldname    = 'WAERK'.
  APPEND alv_field TO alv_t_field.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'VRKME'.
  alv_field-seltext_s     = 'UoM'.
  alv_field-ref_tabname   'T006'.      " Reference Table
  alv_field-ref_fieldname 'MSEHI'.     " Reference Field
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'VKBUR'.
  alv_field-seltext_s     = 'OFFICE'.
  alv_field-no_out        = 'X'.          " No Output direct
  APPEND alv_field TO alv_t_field.
*
* .... Enter ALL of your fields at here.

ENDFORM.                    "alvfield
用 Function 建立的範例:
DATA: lv_tabix LIKE sy-tabix.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
  EXPORTING
    i_program_name         = alv_repid
    i_internal_tabname     = 'GT_ITAB'
    i_inclname             = alv_repid
  CHANGING
    ct_fieldcat            = alv_t_field
  EXCEPTIONS
    inconsistent_interface = 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.

LOOP AT alv_t_field INTO alv_field.
  lv_tabix = sy-tabix.
  CASE alv_field-fieldname.
    WHEN 'FKDAT'.
      alv_field-seltext_s     = 'DATE'.
      alv_field-ddictxt       = 'S'.
    WHEN 'FKIMG'.
      alv_field-seltext_s     = 'Quantity'.
      alv_field-ddictxt       = 'S'.
      alv_field-decimals_out  = '0'.
      alv_field-do_sum        = 'X'.
    WHEN 'AMOU0'.
      alv_field-seltext_s     = 'Amount'.
      alv_field-ddictxt       = 'S'.
      alv_field-currency      = 'TWD'.
    WHEN 'VKBUR'.
      alv_field-seltext_s     = 'OFFICE'.
      alv_field-ddictxt       = 'S'.
      alv_field-no_out        = 'X'.          " No Output direct
  ENDCASE.
  MODIFY alv_t_field FROM alv_field INDEX lv_tabix.
ENDLOOP.

ALV_SORT

*&---------------------------------------------------------------------*
*&      Form  alvsort
*&---------------------------------------------------------------------*
*       ALV Sort
*----------------------------------------------------------------------*
FORM alvsort .
  DATA lv_counter TYPE i.

  CLEAR alv_sort.
  lv_counter         = lv_counter + 1.
  alv_sort-spos      = lv_counter.
  alv_sort-fieldname = 'KUNSP'.
  alv_sort-tabname   = 'GT_ITAB'.
  alv_sort-subtot    = 'X'.
  alv_sort-up        = 'X'.
  alv_sort-group     = 'UL'.       " UL : Split by Line,
                                   " *  : Split by Page.
  APPEND alv_sort TO alv_t_sort.

  CLEAR alv_sort.
  lv_counter         = lv_counter + 1.
  alv_sort-spos      = lv_counter.
  alv_sort-fieldname = 'FKDAT'.
  alv_sort-tabname   = 'GT_ITAB'.
  alv_sort-up        = 'X'.
  alv_sort-group     = ' '.
  APPEND alv_sort TO alv_t_sort.

ENDFORM.                    "alvsort

Set_Status

*&---------------------------------------------------------------------*
*&      Form  set_status
*&---------------------------------------------------------------------*
*       Set Status
*----------------------------------------------------------------------*
FORM set_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD' EXCLUDING rt_extab.
ENDFORM.                    "set_status

User_Command

*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM user_command USING i_ucomm     TYPE syucomm
                         is_selfield TYPE slis_selfield.    "#EC CALLED
  DATA: lv_datum LIKE sy-datum.

  CASE i_ucomm.
    WHEN '&IC1'.             " double click
      READ TABLE gt_itab INDEX is_selfield-tabindex.
      SET PARAMETER ID 'VF'   FIELD gt_itab-vbeln.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
  ENDCASE.

ENDFORM.                               " USER_COMMAND

List_Header

*&---------------------------------------------------------------------*
*&      Form  list_header
*&---------------------------------------------------------------------*
*       List Header
*----------------------------------------------------------------------*
FORM list_header.
  DATA: lv_linsz LIKE sy-linsz.
  lv_linsz = sy-linsz - 20.
  WRITE:   /001(sy-linsz)  'XXX sheet'  CENTERED,
           /001            'Date :'  ,sy-datum.
  WRITE AT: lv_linsz       'Page No :'  ,(4) sy-pagno.
  ULINE.
ENDFORM.                    "list_header

[Manual] ALV Grid Control (BC-SRV-ALV)

The ALV Grid Control (ALV = SAP List Viewer) is a flexible tool for displaying lists.

[Link] Adding subtotals at the end of an ALV List

The normal ALV list functionality provides for subtotals within the list and one grand total at the end of the report. If it is required to display the subtotals not within the list, but at the end of report the standard ALV functionality does not provide any way to do this. The tip described here caters to this special requirement.

[ABAP] Fix_column make the scroll horizontally disable~!!

Fix_column is an attribute of alv field.
• Fix_column (fix column): This parameter is used to fix certain columns in the output. All columns to be fixed must have this flag, starting from the left. If a column without this flag is output, only the columns to the left of this column are fixed. The user can change the column fixing interactively.
Value set: SPACE, X.
X = column fixed (does not scroll horizontally).

[Manual] ALV Grid Control - Netweaver 7

The ALV Grid Control (ALV = SAP List Viewer) is a flexible tool for displaying lists. The tool provides common list operations as generic functions and can be enhanced by self-defined options. This allows you to use the ALV Grid Control in a large range of application programs.

[Code] ALV Colorful

The example code for how to fill color to alv list columns, lines and fields.

Columns

1. Only with your alv_field define.
*
  CLEAR alv_field.
  l_count = l_count + 1.
  alv_field-col_pos       = l_count.
  alv_field-fieldname     = 'XBLNR'.
  alv_field-EMPHASIZE     = 'X'.
  APPEND alv_field TO alv_t_field.
*
* EMPHASIZE:
*     SPACE, 'X' or 'Cxyz' (x:'1'-'9'; y,z: '0'=off '1'=on)
*
* If the field is set to 'X', the ALV uses a pre-defined color for highlighting the column. If the 
* character field begins with 'C' (color code), the remaining numbers have the following meaning:
* 
*     x: color number 
*     y: intensified display on/off
*     y: inverse display on/off
*

Lines

1. In your ALV Internal Table definition, you neet add a field names color.
        [......]
        color TYPE slis_t_specialcol_alv,
      END OF gt_itab.
2. Your can define a internal table with names linecolor.
DATA: linecolor TYPE slis_specialcol_alv.
3. Calculate color in loop at gt_itab like below.
    [......]
    IF gt_itab-profb < 0.
      CLEAR: linecolor.
      linecolor-color-col = 6.
      APPEND linecolor TO gt_itab-color.
    ENDIF.
*   Update
    MODIFY gt_itab.
  ENDLOOP.
4. Your need to set layout like below.
  alv_layout-coltab_fieldname  = 'COLOR'.

Fields

All same the Lines, except for step 3 to assign fieldname
    [......]
    IF gt_itab-profb < 0.
      CLEAR: linecolor.
      linecolor-fieldname = 'PROFB'.
      linecolor-color-col = 6.
      APPEND linecolor TO gt_itab-color.
    ENDIF.
*   Update
    MODIFY gt_itab.
  ENDLOOP.

Color mix

When you assign a row's color append to color field, further assign color to same row's field, then this field's color will mixed to unexpectedly color.

[ABAP] Add Button to ALV Toolbar

Add Button to ALV Toolbar with REUSE_ALV_LIST_DISPLAY
How to add button to ALV toolbar using REUSE_ALV_LIST_DISPLAY?
In the program which calls ALV using REUSE_ALV_LIST_DISPLAY,
I have to add a new button.
I saw the demo program BCALV_GRID_08, which is written using ABAP-Controls.
In that example, the button is added using TOOLBAR event of cl_gui_alv_grid.
Could you help me to implement the same logic using REUSE_ALV_LIST_DISPLAY parameters.
you should copy the 'STANDARD' GUI status from program SAPLKKBL using transaction SE90 -->Programming SubObjects--> Gui Status. 
( SE90 --> 程式庫 --> 程式子物件 --> Gui 狀態) 
Or use SE41 ( Thanks Eunice )
Execute this transaction to get to next screen. select status using checkbox. click on GUI Status --> Copy.
Enter your Z program name and the name you what for this status - you can keep it as 'STANDARD' to be simple.
Then you can edit the new status to add or delete buttons. This will also bring in the standard SAP ALV functionality such as sorting/subtotaling etc...

[ABAP] SAP List Viewer(ALV) 簡介與範例程式之一

Author : Fred Wang (http://fredwang.blogspot.com) Date : 2006/08/25
另外, SAP提供很好的範例程式, 直接供你複製, 我就 ALV技術的 sample programs 整理如下(七類) :
1. Simple list
- 超基本 : BALVSD02, BALVSD02_GRID, BALVSD02_SAVE 利用現有的資料結構作為欄位定義
- 基本 : BALVSD03, BALVSD04 程式中自訂欄位定義
- 完整 : BALVSD01, BALVSD11(*程式可以知道使用者選擇哪筆record), BALVST01, BALVST02, BALVEX01,BALVEX02

要注意, 這些程式最好用SE80去看, 因為有些GUI Status 為自訂的, 可從SAPLKKBL複製它的"STANDARD" GUI status到自己的程式, 再增刪需要與不需要的按鈕 
範例程式 Package: SLIS
2. Mini ALV List (function module使用方式與simple list看起來差不多)
- BCALV_MINIALV_SIMPLE (最簡單), BCALV_MINIALV

3. Block List
- BALVBT01, BALVBT02
BALVBT01 => 使用 REUSE_ALV_BLOCK_LIST_DISPLAY
搭配下列 Functions:
  REUSE_ALV_BLOCK_LIST_INIT
  REUSE_ALV_BLOCK_LIST_APPEND
  REUSE_ALV_BLOCK_LIST_HS_APPEND
  REUSE_ALV_BLOCK_LIST_DATA_GET
  REUSE_ALV_BLOCK_LIST_DATA_SET
  REUSE_ALV_BLOCK_LIST_REFRESH
  REUSE_ALV_BLOCK_STATUS_GET
BALVBT02 => 使用 REUSE_ALV_LIST_DISPLAY
利用 Event 的 END_OF_LIST 呼叫下一個 ALV List,這個方式可以正常的使用 Double click 功能。
4. Hierarchical-sequential list
- BALVHD01, BALVHD01_GROUP, BALVHT01

5. ALV Grid Control (用Grid control object, 採物件導向式的設計, 注意, 按鈕並不是在GUI status上)
- BCALV_GRID_DEMO, BCALV_GRID_*

6. ALV Tree Control
- BCALV_TREE_SIMPLE_DEMO, BCALV_TREE_DEMO, BCALV_TREE_*

7. Other ALV
- BCALV_TEST_*