SAP ABAP - ALV With Button on Grid Cell

 



*&---------------------------------------------------------------------*
*& Report ZALV004
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zalv004.
TABLES: mara.
TYPE-POOLS: slis.                      " ALV Global Types

TYPES:
  BEGIN OF ty_data.
    INCLUDE TYPE zsrmat001.
TYPES:
    bexec   TYPE char10,
    status  TYPE char10,
    snote   TYPE string,
    celltab TYPE lvc_t_styl,
  END OF ty_data.

CONSTANTS :
  c_x        VALUE 'X',
  gc_refresh TYPE syucomm VALUE '&REFRESH',
  gc_print   TYPE syucomm VALUE '&PRN',
  gc_all     TYPE syucomm VALUE '&ALL',
  gc_sal     TYPE syucomm VALUE '&SAL'.

DATA:
  lt_material TYPE TABLE OF ty_data,
  ls_material TYPE ty_data.

DATA:
  lt_fieldcat   TYPE slis_t_fieldcat_alv,
  ls_fieldcat   LIKE LINE OF lt_fieldcat,
  lt_event_exit TYPE slis_t_event_exit,
  ls_event_exit TYPE slis_event_exit,
  is_layout     TYPE slis_layout_alv,
  is_variant    TYPE disvariant.

DATA:
  ls_ssfctrlop TYPE ssfctrlop,
  ls_ssfcompop TYPE ssfcompop,
  ls_out_info  TYPE ssfcrescl,
  formname     TYPE tdsfname,
  lv_fmname    TYPE rs38l_fnam.

SELECT-OPTIONS:
  s_matnr FOR mara-matnr,
  s_ersda FOR mara-ersda.

START-OF-SELECTION.
  PERFORM f_get_data.

END-OF-SELECTION.
  PERFORM f_display_data.


FORM f_get_data.
  REFRESH lt_material.

  SELECT
  mara~matnr, makt~maktx, mara~mtart,
  mara~matkl, mara~meins, mara~ersda
  FROM mara INNER JOIN makt
  ON mara~matnr = makt~matnr AND makt~spras = @sy-langu
  INTO CORRESPONDING FIELDS OF TABLE @lt_material
  WHERE mara~matnr IN @s_matnr AND ersda IN @s_ersda.

  LOOP AT lt_material ASSIGNING FIELD-SYMBOL(<fs_fmat>).
    <fs_fmat>-bexec  = '@15@'.
    <fs_fmat>-status = '@09@'.
  ENDLOOP.

ENDFORM.

FORM generate_fieldcat.
  REFRESH lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'ZSEL'.
  ls_fieldcat-seltext_l   = 'Select'.
  ls_fieldcat-outputlen   = 15.
  ls_fieldcat-checkbox    = 'X'.
  ls_fieldcat-edit        = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'BEXEC'.
  ls_fieldcat-seltext_l   = 'Execute'.
  ls_fieldcat-hotspot     = 'X'.
  ls_fieldcat-icon        = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'STATUS'.
  ls_fieldcat-seltext_l   = 'Status'.
  ls_fieldcat-icon        = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'MATNR'.
  ls_fieldcat-ref_tabname = 'MARA'.
  ls_fieldcat-hotspot     = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'MAKTX'.
  ls_fieldcat-seltext_l   = 'Description'.
  ls_fieldcat-datatype    = 'CHAR'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'MEINS'.
  ls_fieldcat-seltext_l   = 'Uom'.
  ls_fieldcat-datatype    = 'UNIT'.
  APPEND ls_fieldcat TO lt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname   = 'SNOTE'.
  ls_fieldcat-seltext_l   = 'Message'.
  ls_fieldcat-datatype    = 'CHAR'.
  ls_fieldcat-outputlen   = '30'.
  APPEND ls_fieldcat TO lt_fieldcat.

ENDFORM.

FORM f_display_data.

  PERFORM generate_fieldcat.

  CLEAR ls_event_exit.
  ls_event_exit-ucomm = gc_refresh.    " Refresh
  ls_event_exit-after = c_x.
  APPEND ls_event_exit TO lt_event_exit.

  CLEAR ls_event_exit.
  ls_event_exit-ucomm = gc_print.    " print
  ls_event_exit-after = c_x.
  APPEND ls_event_exit TO lt_event_exit.

  CLEAR ls_event_exit.
  ls_event_exit-ucomm = gc_all.    " Select All
  ls_event_exit-after = c_x.
  APPEND ls_event_exit TO lt_event_exit.

  CLEAR ls_event_exit.
  ls_event_exit-ucomm = gc_sal.    " Deselect All
  ls_event_exit-after = c_x.
  APPEND ls_event_exit TO lt_event_exit.

  is_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-cprog
*     i_callback_top_of_page   = 'top_of_page'
      i_callback_top_of_page   = 'TOP_OF_PAGE'
      i_callback_pf_status_set = 'PF_STATUS_SET'
      i_callback_user_command  = 'USER_COMMAND'
      is_layout                = is_layout
      it_fieldcat              = lt_fieldcat
      i_save                   = 'A'
      is_variant               = is_variant
      it_event_exit            = lt_event_exit
    TABLES
      t_outtab                 = lt_material.
ENDFORM.

FORM user_command USING u_ucomm     TYPE sy-ucomm
      us_selfield TYPE slis_selfield.                       "#EC CALLED

  DATA ref1 TYPE REF TO cl_gui_alv_grid.

  CASE u_ucomm.
    WHEN '&PRINT'.
*      BREAK-POINT.

      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = ref1.
      CALL METHOD ref1->check_changed_data.

      READ TABLE lt_material WITH KEY zsel = 'X' TRANSPORTING NO FIELDS.
      IF sy-subrc = 0.
        CLEAR : ls_ssfctrlop, ls_ssfcompop.
        ls_ssfctrlop-no_open  = 'X'.
        ls_ssfctrlop-no_close = 'X'.

        ls_ssfctrlop-no_dialog = abap_true.
        ls_ssfctrlop-preview   = abap_true.
        ls_ssfcompop-tddest    = 'PDF'.

        CALL FUNCTION 'SSF_OPEN'
          EXPORTING
            user_settings      = ' '
            output_options     = ls_ssfcompop
            control_parameters = ls_ssfctrlop.

        formname = 'ZFMATERIAL'.

        CLEAR lv_fmname.
        CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
          EXPORTING
            formname           = formname    " Form name
          IMPORTING
            fm_name            = lv_fmname
          EXCEPTIONS
            no_form            = 1
            no_function_module = 2
            OTHERS             = 3.

        LOOP AT lt_material INTO ls_material WHERE zsel = 'X'.


          CALL FUNCTION lv_fmname
            EXPORTING
              control_parameters = ls_ssfctrlop
              output_options     = ls_ssfcompop
              user_settings      = ' '
              p_matnr            = ls_material-matnr
            IMPORTING
              job_output_info    = ls_out_info
            EXCEPTIONS
              formatting_error   = 1
              internal_error     = 2
              send_error         = 3
              user_canceled      = 4
              OTHERS             = 5.
        ENDLOOP.

        CALL FUNCTION 'SSF_CLOSE'
          IMPORTING
            job_output_info = ls_out_info.
      ELSE.
        MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'W'.
      ENDIF.



    WHEN gc_refresh.
      PERFORM f_get_data.             " Refresh data
      us_selfield-refresh    = c_x.
      us_selfield-col_stable = c_x.
      us_selfield-row_stable = c_x.

    WHEN gc_all.
      DO 1 TIMES.
        CLEAR ls_material.
        ls_material-zsel = 'X'.
        MODIFY lt_material FROM ls_material
        TRANSPORTING zsel WHERE zsel = ''.
      ENDDO.
      us_selfield-refresh = 'X'.
    WHEN gc_sal.
      DO 1 TIMES.
        CLEAR ls_material.
        ls_material-zsel = ''.
        MODIFY lt_material FROM ls_material
        TRANSPORTING zsel WHERE zsel = 'X'.
      ENDDO.
      us_selfield-refresh = 'X'.
    WHEN gc_print.
*      BREAK-POINT.
*      DATA ref1 TYPE REF TO cl_gui_alv_grid.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = ref1.
      CALL METHOD ref1->check_changed_data.

      READ TABLE lt_material WITH KEY zsel = 'X' TRANSPORTING NO FIELDS.
      IF sy-subrc = 0.
        DATA icount TYPE i.
        LOOP AT lt_material INTO ls_material WHERE zsel = 'X'.
          icount = icount + 1.
        ENDLOOP.

        DATA str_msg TYPE string.
        CLEAR str_msg.
        str_msg = |{ icount } row data selected|.
        MESSAGE str_msg TYPE 'S' DISPLAY LIKE 'S'.
      ELSE.
        MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
      ENDIF.

    WHEN '&IC1'.
      CASE us_selfield-fieldname.
        WHEN 'MATNR'.
          SET PARAMETER ID 'MAT' FIELD us_selfield-value.
          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
        WHEN 'BEXEC'.


          CLEAR ls_material.
          READ TABLE lt_material INTO ls_material INDEX us_selfield-tabindex.
          DO 1 TIMES.
            CLEAR ls_material-status.
            IF us_selfield-value = '@15@'.
              ls_material-status = '@08@'.
              ls_material-bexec  = '@0W@'.
              ls_material-snote  = 'Success'.
              MESSAGE 'Execute Process....' TYPE 'S' DISPLAY LIKE 'S'.
            ELSE.
              ls_material-status = '@09@'.
              ls_material-bexec  = '@15@'.
              ls_material-snote  = 'Cancelled'.
              MESSAGE 'Cancel Process....' TYPE 'S' DISPLAY LIKE 'S'.
            ENDIF.

            MODIFY lt_material FROM ls_material
              TRANSPORTING status bexec snote
                WHERE matnr = ls_material-matnr.
          ENDDO.
          us_selfield-refresh = 'X'.
      ENDCASE.
  ENDCASE.

ENDFORM.                    "user_command
*---------------------------------------------------------------------*
*       FORM PF_STATUS_SET                                            *
*---------------------------------------------------------------------*
FORM pf_status_set USING ut_extab TYPE slis_t_extab.        "#EC CALLED

  DELETE ut_extab WHERE fcode = gc_refresh.
  DELETE ut_extab WHERE fcode = gc_print.
  DELETE ut_extab WHERE fcode = gc_sal.
  DELETE ut_extab WHERE fcode = gc_all.

  SET PF-STATUS 'ZALV_STAT' "OF PROGRAM 'SAPLKKBL'
  EXCLUDING ut_extab.

ENDFORM.                    "pf_status_set
********* END OF PROGRAM Z_DEMO_ALV_REFRESH_BUTTON ********************

FORM top_of_page.
  DATA:
    wa_top TYPE          slis_listheader,
    it_top TYPE          slis_t_listheader.

  CLEAR: wa_top.
  wa_top-typ  = 'H'.
  wa_top-info = 'Testing ALV Report'.
  APPEND wa_top TO it_top.


  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = it_top.
ENDFORM.

Comments

Popular posts from this blog

PHP MySql CRUD Dengan Konsep MVC

Laravel - Sistem Informasi Perpustakaan Sederhana Laravel

IT Asset Management Dengan PHP MySQL