ALV: Checkboxes

SAP Tech Pro

SAP Technical Blog
Magzter [CPS] IN

ALV: Checkboxes

ALV: Checkboxes

Text Elements:
F01     Direct Flight
F02     Flight Without Change
F03     Direct Flight Indicator


program bcalv_edit_05.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This example shows how to use checkboxes within an ALV Grid Control.
* You learn:
*  o how to define a column for editable checkboxes for an attribute
*    of your list (see also remark below)
*  o how to evaluate the checked checkboxes
*  o how to switch between editable and non-editable checkboxes
*
* Important Remark
* ~~~~~~~~~~~~~~~~
* The checkbox functionality has been replaced by selection buttons
* in front of each row (field SEL_MODE of the layout structure
* set to 'A' or 'D'; when using the editable ALV Grid Control,
* these selection buttons are always visible).
* Class methods like GET_SELECTED_ROWS work only for this new
* functionality and not for checkboxes.
* Thus checkboxes should not be used for line selection but for
* a column as an additional or for an already existing attribute
* (like field SMOKER in SBOOK).
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Try out the functions displayed in the application toolbar:
* o The first sets all checked lines to initial values.
*   (see form reset_selected_entries)
* o The seconds marks all checkboxes that are input enabled
* o The third unmarks all checkboxes that are input enabled
* o To try the forth, you have to select a line first using
*   the selection buttons on the left.
*   The function deactivates/activates a checkbox.
*
* Checkboxes may be locked/unlocked using a double click on the
* checkbox cell.
*
*-----------------------------------------------------------------
* Essential steps (search for 'ǧ)
* ~~~~~~~~~~~~~~~
* This example focusses on two aspects of checkboxes in an
* editable ALV Grid Control:
* A How to integrate, set, reset and evaluate checkboxes
* B What you must do to lock particular checkboxes against input
*
* A) Integrate, set, reset and evaluate checkboxes
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* A1.Extend your output table by a checkbox field.
* A2.Add an entry for the checkbox in the fieldcatalog
* A3.Optionally, check checkboxes initially after selecting data.
* A4.Before you (a)set, (b)reset, (c)(de)activate or
*    (d)evaluate checkboxes, you must check the input cells.
*
* B) Lock particular checkboxes against input
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* B1.Extend your output table by a field to dis- or enable
*     cells for input.
* B2.After selecting data,
*     assign a style for each row of your checkbox column.
* B3.Use the layout structure to aquaint additional field to ALV.
* B4.Switch the style to dis- or enable a cell for input
*-----------------------------------------------------------------
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
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.

*
*ȁ1.Extend your output table by a checkbox field.
*     If you do not want to lock sole checkboxes against input
*     you do not need field 'celltab'.

types: begin of gs_outtab.
types: checkbox type c.                "field for checkbox
* Ȃ1.Extend your output table by a field to dis- or enable
*     cells for input.
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.
* Wenn 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'.
* Ȃ4.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 EXIT_PROGRAM                                             *
*---------------------------------------------------------------------*
form exit_program.
  leave program.
endform.
*&---------------------------------------------------------------------*
*&      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.

*ȁ2.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  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  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.

*ȁ3.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.

* Ȃ2.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  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.

*ȁ4ad. 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.


*ȁ4ad. 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.

*ȁ4b. 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.

*ȁ4c. 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

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in