ALV tutorial 10 – Pushbuttons and Hyperlinks in cells

Pushbuttons and hyperlinks in ALV fields

REPORT  z_alv_demo_10.
TYPES: BEGIN OF ty_data.
        INCLUDE STRUCTURE sflight.
TYPES:  cell_table TYPE lvc_t_styl,             " field to hold the info about pushbutton
        hl_fname   TYPE int4,                   " field to hold the hyperlink handle
       END OF ty_data.

DATA: gt_data TYPE TABLE OF ty_data,
      g_grid TYPE REF TO cl_gui_alv_grid,
      gs_layout TYPE lvc_s_layo,
      gt_fieldcat TYPE lvc_t_fcat,
      gt_hypetab TYPE lvc_t_hype.

CONSTANTS:
  c_max_rows TYPE i VALUE 100.

CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  read_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM read_data.
  DATA ls_styl TYPE lvc_s_styl.

  FIELD-SYMBOLS:
     TYPE ty_data.

  PERFORM get_hyperlinks.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
    FROM sflight
    UP TO c_max_rows ROWS.

  ls_styl-style = cl_gui_alv_grid=>mc_style_button.

  LOOP AT gt_data ASSIGNING .
* Display pushbutton in case a travel class is not full so new booking can be made
    IF -seatsocc < -seatsmax.
      ls_styl-fieldname = 'SEATSOCC'.
      APPEND ls_styl TO -cell_table.

      -hl_fname = '1'.                  " Assign the hyperlink handle
    ENDIF.

    IF -seatsocc_b < -seatsmax_b.
      ls_styl-fieldname = 'SEATSOCC_B'.
      APPEND ls_styl TO -cell_table.

      -hl_fname = 2.                    " Assign the hyperlink handle
    ENDIF.

    IF -seatsocc_f < -seatsmax_f.
      ls_styl-fieldname = 'SEATSOCC_F'.
      APPEND ls_styl TO -cell_table.

      -hl_fname = 1.                    " Assign the hyperlink handle
    ENDIF.
  ENDLOOP.
ENDFORM.                    "read_data

*&---------------------------------------------------------------------*
*&      Form  display_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_grid.
  PERFORM get_layout.
  PERFORM get_fieldcat.

  CREATE OBJECT g_grid
    EXPORTING
      i_parent = cl_gui_container=>default_screen.

  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_structure_name              = 'SFLIGHT'
      is_layout                     = gs_layout
      it_hyperlink                  = gt_hypetab           " Attach table with hyperlink handles
    CHANGING
      it_outtab                     = gt_data
      it_fieldcatalog               = gt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  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.                    "display_grid
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MAIN'.
  SET TITLEBAR 'ALV_EXAMPLES'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE display_grid OUTPUT.
  PERFORM read_data.
  PERFORM display_grid.
ENDMODULE.                 " DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
*   to react on oi_custom_events:
  CALL METHOD cl_gui_cfw=>dispatch.
  CASE sy-ucomm.
    WHEN 'BACK' OR
         'EXIT' OR
         'CANCEL'.
      LEAVE PROGRAM.
    WHEN OTHERS.
*     do nothing
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  GET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_layout .
  gs_layout-zebra = 'X'.
  gs_layout-stylefname = 'CELL_TABLE'.
ENDFORM.                    " GET_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  GET_HYPERLINKS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_hyperlinks .
  DATA: ls_hype TYPE lvc_s_hype.

  ls_hype-handle = '1'.
  ls_hype-href = 'http://google.com'.
  APPEND ls_hype TO gt_hypetab.

  ls_hype-handle = '2'.
  ls_hype-href = 'http://yahoo.com'.
  APPEND ls_hype TO gt_hypetab.
ENDFORM.                    " GET_HYPERLINKS
*&---------------------------------------------------------------------*
*&      Form  GET_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_fieldcat .
  FIELD-SYMBOLS:  TYPE lvc_s_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  CHECK sy-subrc = 0.

  " Attach the hyperlink to PLANETYPE field
  READ TABLE gt_fieldcat ASSIGNING  WITH KEY fieldname = 'PLANETYPE'.
  CHECK sy-subrc = 0.
  -web_field = 'HL_FNAME'.

ENDFORM.                    " GET_FIELDCAT

Leave a Reply