ALV checkbox实例

 PROGRAM BCALV_EDIT_05.
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.  "for event handling
DATA: OK_CODE LIKE SY-UCOMM,
      SAVE_OK LIKE SY-UCOMM,
      G_CONTAINER TYPE SCRFNAME VALUE 'BCALV_GRID_DEMO_0100_CONT1',
      G_GRID  TYPE REF TO CL_GUI_ALV_GRID,
      G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      G_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER,
      GT_FIELDCAT TYPE LVC_T_FCAT,
      GS_LAYOUT TYPE LVC_S_LAYO,
      G_MAX TYPE I VALUE 100.

TYPES: BEGIN OF GS_OUTTAB.
TYPES: CHECKBOX TYPE C.                "field for checkbox
TYPES: CELLTAB TYPE LVC_T_STYL.        "field to switch editability
        INCLUDE STRUCTURE SFLIGHT.
TYPES: END OF GS_OUTTAB.

DATA: GT_OUTTAB TYPE GS_OUTTAB OCCURS 0 WITH HEADER LINE.

***********************************************************************
* LOCAL CLASSES
***********************************************************************
*
* This local class only handles event DOUBLE_CLICK.
* WHen the user double clicks on a checkbox cell the status of
* this cell is switched from editable to not editable and vice versa.
*
CLASS LCL_EVENT_RECEIVER DEFINITION.

PUBLIC SECTION.
METHODS: CATCH_DOUBLECLICK
         FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
         IMPORTING
            E_COLUMN
            ES_ROW_NO
            SENDER.
ENDCLASS.

CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

METHOD CATCH_DOUBLECLICK.
  DATA: LS_OUTTAB TYPE GS_OUTTAB,
        LS_CELLTAB TYPE LVC_S_STYL.
*--
* Function:
*  Switch between 'editable' and 'not editable' checkbox.
*--

* If the user clicked on another column there is
* nothing to do.
  IF E_COLUMN-FIELDNAME NE 'CHECKBOX'.
    EXIT.
  ENDIF.

  READ TABLE GT_OUTTAB INTO LS_OUTTAB INDEX ES_ROW_NO-ROW_ID.

* The loop is only needed if there are other columns that
* use checkboxes. At this point the loop could be
* replaced by a READ of the first line of CELLTAB.
      LOOP AT LS_OUTTAB-CELLTAB INTO LS_CELLTAB.
        IF LS_CELLTAB-FIELDNAME EQ 'CHECKBOX'.
* §B4.Switch the style to dis- or enable a cell for input
         IF LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
          LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
         ELSE.
          LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
         ENDIF.
         MODIFY LS_OUTTAB-CELLTAB FROM LS_CELLTAB.
        ENDIF.
      ENDLOOP.
      MODIFY GT_OUTTAB FROM LS_OUTTAB INDEX ES_ROW_NO-ROW_ID.

    CALL METHOD SENDER->REFRESH_TABLE_DISPLAY.
ENDMETHOD.
ENDCLASS.
*
********************************************************************

*---------------------------------------------------------------------*
*       MAIN                                                          *
*---------------------------------------------------------------------*
END-OF-SELECTION.
  CALL SCREEN 100.

*---------------------------------------------------------------------*
*       MODULE PBO OUTPUT                                             *
*---------------------------------------------------------------------*
MODULE PBO OUTPUT.
  SET PF-STATUS 'MAIN100'.
  SET TITLEBAR 'MAIN100'.
  IF G_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_AND_INIT_ALV.
  ENDIF.

ENDMODULE.
*---------------------------------------------------------------------*
*       MODULE PAI INPUT                                              *
*---------------------------------------------------------------------*
MODULE PAI INPUT.
  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.
  CASE SAVE_OK.
    WHEN 'EXIT'.
      PERFORM EXIT_PROGRAM.
    WHEN 'SELECT'.
      PERFORM SELECT_ALL_ENTRIES CHANGING GT_OUTTAB[].
    WHEN 'DESELECT'.
      PERFORM DESELECT_ALL_ENTRIES CHANGING GT_OUTTAB[].
    WHEN 'RESET'.
      PERFORM RESET_SELECTED_ENTRIES CHANGING GT_OUTTAB[].
    WHEN 'SWITCH'.
      PERFORM SWITCH_ACTIVATION CHANGING GT_OUTTAB[].
  ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_OUTTAB  text
*      <--P_GT_FIELDCAT  text
*      <--P_GS_LAYOUT  text
*----------------------------------------------------------------------*
FORM CREATE_AND_INIT_ALV.

  DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.

  CREATE OBJECT G_CUSTOM_CONTAINER
         EXPORTING CONTAINER_NAME = G_CONTAINER.
  CREATE OBJECT G_GRID
         EXPORTING I_PARENT = G_CUSTOM_CONTAINER.

  PERFORM BUILD_FIELDCAT CHANGING GT_FIELDCAT.

* Exclude all edit functions in this example since we do not need them:
  PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.

  PERFORM BUILD_DATA.

*§ B3.Use the layout structure to aquaint additional field to ALV.

  GS_LAYOUT-STYLEFNAME = 'CELLTAB'.

  CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY
       EXPORTING IS_LAYOUT             = GS_LAYOUT
                 IT_TOOLBAR_EXCLUDING  = LT_EXCLUDE
       CHANGING  IT_FIELDCATALOG       = GT_FIELDCAT
                 IT_OUTTAB             = GT_OUTTAB[].

  CREATE OBJECT G_EVENT_RECEIVER.
  SET HANDLER G_EVENT_RECEIVER->CATCH_DOUBLECLICK FOR G_GRID.

* Set editable cells to ready for input initially
  CALL METHOD G_GRID->SET_READY_FOR_INPUT
   EXPORTING
    I_READY_FOR_INPUT = 1.


ENDFORM.                               "CREATE_AND_INIT_ALV

*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_FIELDCAT  text
*----------------------------------------------------------------------*
FORM BUILD_FIELDCAT CHANGING PT_FIELDCAT TYPE LVC_T_FCAT.

  DATA LS_FCAT TYPE LVC_S_FCAT.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
       EXPORTING
            I_STRUCTURE_NAME = 'SFLIGHT'
       CHANGING
            CT_FIELDCAT      = PT_FIELDCAT.

*§A2.Add an entry for the checkbox in the fieldcatalog
  CLEAR LS_FCAT.
  LS_FCAT-FIELDNAME = 'CHECKBOX'.
* Essential: declare field as checkbox and
*            mark it as editable field:
  LS_FCAT-CHECKBOX = 'X'.
  LS_FCAT-EDIT = 'X'.

* do not forget to provide texts for this extra field
  LS_FCAT-COLTEXT = TEXT-F01.
  LS_FCAT-TOOLTIP = TEXT-F02.
  LS_FCAT-SELTEXT = TEXT-F03.

* optional: set column width
  LS_FCAT-OUTPUTLEN = 10.
*
  APPEND LS_FCAT TO PT_FIELDCAT.

ENDFORM.
                            

*&---------------------------------------------------------------------*
*&      Form  EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LT_EXCLUDE  text
*----------------------------------------------------------------------*
FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.

  DATA LS_EXCLUDE TYPE UI_FUNC.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.
  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_CUT.
  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.


ENDFORM.                               " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*&      Form  build_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BUILD_DATA.

  DATA: LT_SFLIGHT TYPE TABLE OF SFLIGHT,
        LS_SFLIGHT TYPE SFLIGHT,
        LS_CELLTAB TYPE LVC_S_STYL,
        LT_CELLTAB TYPE LVC_T_STYL,
        L_INDEX TYPE I.

  SELECT * FROM SFLIGHT INTO TABLE LT_SFLIGHT UP TO G_MAX ROWS.
  IF SY-SUBRC NE 0.
* generate own entries if db-table is empty so that this example
* still works
    PERFORM GENERATE_ENTRIES CHANGING LT_SFLIGHT.
  ENDIF.

*§A3.Optionally, check checkboxes initially after selecting data.
* (Omitted in this example)
  LOOP AT LT_SFLIGHT INTO LS_SFLIGHT.
    MOVE-CORRESPONDING LS_SFLIGHT TO GT_OUTTAB.
*   if gt_outtab-connid eq '400'.
*     gt_outtab-checkbox = 'X'.
*   endif.
    APPEND GT_OUTTAB.
  ENDLOOP.

* §B2.After selecting data,
*    assign a style for each row of your checkbox column.
*
* Initially, set all checkbox cells editable.
  LS_CELLTAB-FIELDNAME = 'CHECKBOX'.
  LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.

  LOOP AT GT_OUTTAB.
    L_INDEX = SY-TABIX.
    REFRESH LT_CELLTAB.

    LS_CELLTAB-FIELDNAME = 'CHECKBOX'.
    LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
    INSERT LS_CELLTAB INTO TABLE LT_CELLTAB.

    INSERT LINES OF LT_CELLTAB INTO TABLE GT_OUTTAB-CELLTAB.
    MODIFY GT_OUTTAB INDEX L_INDEX.
  ENDLOOP.

ENDFORM.                               " build_data
*---------------------------------------------------------------------*
*       FORM EXIT_PROGRAM                                             *
*---------------------------------------------------------------------*
FORM EXIT_PROGRAM.
  LEAVE PROGRAM.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  generate_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LT_SLFIGHT  text
*----------------------------------------------------------------------*
FORM GENERATE_ENTRIES CHANGING PT_SLFIGHT TYPE STANDARD TABLE.
*
* This form is only needed for the case that there is no
* data in database table SFLIGHT.
*
  DATA: LS_SFLIGHT TYPE SFLIGHT,
        L_MONTH(2) TYPE C,
        L_DAY(2) TYPE C,
        L_DATE(8) TYPE C.


  LS_SFLIGHT-CARRID = 'LH'.
  LS_SFLIGHT-CONNID = '0400'.
  LS_SFLIGHT-CURRENCY = 'DEM'.
  LS_SFLIGHT-PLANETYPE = '747-400'.
  LS_SFLIGHT-SEATSMAX = 660.

  DO 110 TIMES.
    LS_SFLIGHT-PRICE = SY-INDEX * 100.
    LS_SFLIGHT-SEATSOCC = 660 - SY-INDEX * 6.
    LS_SFLIGHT-PAYMENTSUM = LS_SFLIGHT-SEATSOCC * LS_SFLIGHT-PRICE.

    L_MONTH = SY-INDEX / 10 + 1.
    DO 2 TIMES.
      L_DAY = L_MONTH + SY-INDEX * 2.
      L_DATE+0(4) = '2000'.
      L_DATE+4(2) = L_MONTH+0(2).
      L_DATE+6(2) = L_DAY+0(2).
      LS_SFLIGHT-FLDATE = L_DATE.
      APPEND LS_SFLIGHT TO PT_SLFIGHT.
    ENDDO.
  ENDDO.
ENDFORM.                               " generate_entries
*&---------------------------------------------------------------------*
*&      Form  select_all_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_OUTTAB  text
*----------------------------------------------------------------------*
FORM SELECT_ALL_ENTRIES CHANGING PT_OUTTAB TYPE STANDARD TABLE.
  DATA: LS_OUTTAB TYPE GS_OUTTAB.
  DATA: L_VALID TYPE C,
        L_LOCKED TYPE C.

*§A4ad. Before you (a)set, reset or (d)evaluate checkboxes,
*       you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD G_GRID->CHECK_CHANGED_DATA
              IMPORTING
                 E_VALID = L_VALID.

  IF L_VALID EQ 'X'.

    LOOP AT PT_OUTTAB INTO LS_OUTTAB.
      PERFORM CHECK_LOCK USING    LS_OUTTAB
                         CHANGING L_LOCKED.
      IF L_LOCKED IS INITIAL
         AND NOT LS_OUTTAB-CHECKBOX EQ '-'.
        LS_OUTTAB-CHECKBOX = 'X'.
      ENDIF.
      MODIFY PT_OUTTAB FROM LS_OUTTAB.
    ENDLOOP.

    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY.

  ENDIF.

ENDFORM.                               " select_all_entries
*&---------------------------------------------------------------------*
*&      Form  deselect_all_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_OUTTAB[]  text
*----------------------------------------------------------------------*
FORM DESELECT_ALL_ENTRIES CHANGING PT_OUTTAB TYPE STANDARD TABLE.
  DATA: LS_OUTTAB TYPE GS_OUTTAB.
  DATA: L_VALID TYPE C,
        L_LOCKED TYPE C.


*§A4ad. Before you (a)set, reset or (d)evaluate checkboxes,
*       you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD G_GRID->CHECK_CHANGED_DATA
              IMPORTING
                 E_VALID = L_VALID.

  IF L_VALID EQ 'X'.

    LOOP AT PT_OUTTAB INTO LS_OUTTAB.
      PERFORM CHECK_LOCK USING    LS_OUTTAB
                       CHANGING L_LOCKED.
      IF L_LOCKED IS INITIAL
         AND NOT LS_OUTTAB-CHECKBOX EQ '-'.
        LS_OUTTAB-CHECKBOX = ' '.
      ENDIF.

      MODIFY PT_OUTTAB FROM LS_OUTTAB.
    ENDLOOP.

    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY.

  ENDIF.



ENDFORM.                               " deselect_all_entries
*&---------------------------------------------------------------------*
*&      Form  reset_selected_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_GT_OUTTAB[]  text
*----------------------------------------------------------------------*
FORM RESET_SELECTED_ENTRIES CHANGING PT_OUTTAB TYPE STANDARD TABLE.
  DATA: LS_OUTTAB TYPE GS_OUTTAB.
  DATA: L_VALID TYPE C.

*§A4b. Before you set, (b)reset or evaluate checkboxes,
*      you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD G_GRID->CHECK_CHANGED_DATA
              IMPORTING
                 E_VALID = L_VALID.

  IF L_VALID EQ 'X'.

    LOOP AT PT_OUTTAB INTO LS_OUTTAB.
      IF     NOT LS_OUTTAB-CHECKBOX IS INITIAL
         AND NOT LS_OUTTAB-CHECKBOX EQ '-'.
        CLEAR LS_OUTTAB.
        MODIFY PT_OUTTAB FROM LS_OUTTAB.
      ENDIF.
    ENDLOOP.

    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY.

  ENDIF.

ENDFORM.                               " reset_selected_entries

*-----------------------------

FORM SWITCH_ACTIVATION CHANGING PT_OUTTAB TYPE STANDARD TABLE.
  DATA: LS_OUTTAB TYPE GS_OUTTAB.
  DATA: L_VALID TYPE C,
        LT_ROW_NO TYPE LVC_T_ROID WITH HEADER LINE.

*§A4c. Before you set, reset, (c)(de)activate

*      or evaluate checkboxes, you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.

  CALL METHOD G_GRID->CHECK_CHANGED_DATA
              IMPORTING
                 E_VALID = L_VALID.

  IF L_VALID EQ 'X'.
    CALL METHOD G_GRID->GET_SELECTED_ROWS
      IMPORTING
         ET_ROW_NO     = LT_ROW_NO[].

    LOOP AT LT_ROW_NO.
       READ TABLE PT_OUTTAB INTO LS_OUTTAB INDEX LT_ROW_NO-ROW_ID.
       IF LS_OUTTAB-CHECKBOX NE '-'.
         LS_OUTTAB-CHECKBOX = '-'.
       ELSE.
         LS_OUTTAB-CHECKBOX = ' '.
       ENDIF.
       MODIFY PT_OUTTAB FROM LS_OUTTAB INDEX LT_ROW_NO-ROW_ID.

    ENDLOOP.

    CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY.

  ENDIF.

ENDFORM.                               " switch_activation

*&---------------------------------------------------------------------*
*&      Form  check_lock
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LS_OUTTAB  text
*      <--P_L_LOCKED  text
*----------------------------------------------------------------------*
FORM CHECK_LOCK USING    PS_OUTTAB TYPE GS_OUTTAB
                CHANGING P_LOCKED.
  DATA LS_CELLTAB TYPE LVC_S_STYL.

  LOOP AT PS_OUTTAB-CELLTAB INTO LS_CELLTAB.
    IF LS_CELLTAB-FIELDNAME = 'CHECKBOX'.
      IF LS_CELLTAB-STYLE EQ CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
        P_LOCKED = 'X'.
      ELSE.
        P_LOCKED = SPACE.
      ENDIF.
    ENDIF.
  ENDLOOP.

ENDFORM.                               " check_lock

你可能感兴趣的:(ALV checkbox实例)