SQL LIFE

Sap Abap Alv Grid Kullanımı

Abap Alv grid kullanımı,sap abap salv grid kullanımı, abap alv grid raporlama,
abap cl_salv_table kullanımı, abap alv grid click events

 
Merhaba, bu yazımda abap programlarında sıklıkla kullandığımız alv grid kullanımı ile ilgili kodlar paylaşacağım.
Alv grid pf-status tanımlama, buton click event’leri, liste ve grid görünümü özelliklerinin hepsi yer almaktadır.


sap abap alv grid abap reporting selection screen
 

Alv Grid Görüntüsü



 

Liste Görünümü seçilirse çıktı:





abap gui status alv grid button click

Program kodları:
report ZALVTESTOTTEST line-size 255.

include <color>.

include <icon>.
include <symbol>.

types: begin of g_type_s_outtab.
include type alv_t_t2.
types:   hotspot     type icon_d,
         t_celltype  type salv_t_int4_column,
       end   of g_type_s_outtab,

       begin of g_type_s_test,
         amount  type i,
         repid   type syrepid,
         display type i,
         dynamic type sap_bool,
       end of g_type_s_test.

constants: gc_true  type sap_bool value 'X',

           begin of gc_s_display,
             list       type i value 1,
             fullscreen type i value 2,
             grid       type i value 3,
           end   of gc_s_display.

class lcl_handle_events definition deferred.

data: gs_test type g_type_s_test.

data: gt_outtab type standard table of g_type_s_outtab.

data: gr_table   type ref to cl_salv_table.

data: gr_container type ref to cl_gui_custom_container.

* object for handling the events of cl_salv_table
data: gr_events type ref to lcl_handle_events.

data: g_okcode type syucomm.



class lcl_handle_events definition.
  public section.
    methods:
      on_user_command for event added_function of cl_salv_events
        importing e_salv_function,

      on_double_click for event double_click of cl_salv_events_table
        importing row column,

      on_link_click for event link_click of cl_salv_events_table
        importing row column.
endclass.                    


class lcl_handle_events implementation.
  method on_user_command.
    perform handle_user_command using e_salv_function.
  endmethod.                    "on_user_command

  method on_double_click.
    perform show_cell_info using 0 row column text-i07.
  endmethod.                    "on_double_click

  method on_link_click.
    perform show_cell_info using 0 row column text-i06.
  endmethod.                    "on_single_click
endclass.                    "lcl_handle_events IMPLEMENTATION

selection-screen begin of block dsp with frame.
parameters:
p_full   radiobutton group dsp,
p_list   radiobutton group dsp.
selection-screen end of block dsp.


start-of-selection.


  case gc_true.
    when p_list.
      gs_test-display = gc_s_display-list.
    when p_full.
      gs_test-display = gc_s_display-fullscreen.

  endcase.

*... §1 select data into global output table
  perform select_data.

end-of-selection.

  case gs_test-display.
    when gc_s_display-fullscreen.
      perform display_fullscreen.
    when gc_s_display-list.
      perform display_list.
  endcase.


form select_data.

  field-symbols: <ls_outtab> type g_type_s_outtab.

  data: l_filename type string.

      select * from alv_t_t2 into corresponding fields of table gt_outtab
        up to gs_test-amount rows.                          

      loop at gt_outtab assigning <ls_outtab>.
        <ls_outtab>-hotspot    = icon_overview.
      endloop.


endform.

form display_fullscreen .

  try.
      cl_salv_table=>factory(
        importing
          r_salv_table = gr_table
        changing
          t_table      = gt_outtab ).
    catch cx_salv_msg.                                  
  endtry.

*... Functions
*... activate ALV generic Functions
*... include own functions by setting own status
*  gr_table->set_screen_status(
*    pfstatus      =  'SALV_STANDARD'
*    report        =  gs_test-repid
*    set_functions = gr_table->c_functions_all ).

*... set the columns technical
  data: lr_columns type ref to cl_salv_columns_table,
        lr_column  type ref to cl_salv_column_table.

  lr_columns = gr_table->get_columns( ).
  lr_columns->set_optimize( gc_true ).

  perform set_columns_technical using lr_columns.

*... §4 set hotspot column
  try.
      lr_column ?= lr_columns->get_column( 'HOTSPOT' ).
      lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
      lr_column->set_icon( if_salv_c_bool_sap=>true ).
      lr_column->set_long_text( 'HOTSPOT' ).
    catch cx_salv_not_found.                           
  endtry.

*... §6 register to the events of cl_salv_table
  data: lr_events type ref to cl_salv_events_table.

  lr_events = gr_table->get_event( ).

  create object gr_events.


  set handler gr_events->on_user_command for lr_events.

  set handler gr_events->on_double_click for lr_events.

  set handler gr_events->on_link_click for lr_events.

*... §7 selections
  data: lr_selections type ref to cl_salv_selections,
        lt_rows       type salv_t_row,
        lt_column     type salv_t_column,
        ls_cell       type salv_s_cell.

  lr_selections = gr_table->get_selections( ).

*... §7.1 set selection mode
  lr_selections->set_selection_mode( if_salv_c_selection_mode=>cell ).

*... §7.2 set selected columns.
  append 'CARRID' to lt_column.
  append 'CONNID' to lt_column.
  append 'FLDATE' to lt_column.
  lr_selections->set_selected_columns( lt_column ).




*... §8 display the table
  gr_table->display( ).

endform.                    



form display_list .

*... §2 create an ALV table
*    §2.1 set LIST_DISPLAY to 'X' for displaying an ALV List
  try.
      cl_salv_table=>factory(
        exporting
          list_display = gc_true
        importing
          r_salv_table = gr_table
        changing
          t_table      = gt_outtab ).
    catch cx_salv_msg.                                  
  endtry.

*... set the columns technical
  data: lr_columns type ref to cl_salv_columns_table,
        lr_column  type ref to cl_salv_column_table.

  lr_columns = gr_table->get_columns( ).
  lr_columns->set_optimize( gc_true ).

  perform set_columns_technical using lr_columns.

*... §4 set hotspot column
  try.
      lr_column ?= lr_columns->get_column( 'HOTSPOT' ).
      lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
      lr_column->set_icon( if_salv_c_bool_sap=>true ).
      lr_column->set_long_text( 'HOTSPOT' ).
    catch cx_salv_not_found.                            
  endtry.

*... §6 register to the events of cl_salv_table
  data: lr_events type ref to cl_salv_events_table.

  lr_events = gr_table->get_event( ).

  create object gr_events.

*... §6.1 register to the event USER_COMMAND
  set handler gr_events->on_user_command for lr_events.
*... §6.2 register to the event DOUBLE_CLICK
  set handler gr_events->on_double_click for lr_events.
*... §6.3 register to the event LINK_CLICK
  set handler gr_events->on_link_click for lr_events.

*... §7 selections
  data: lr_selections type ref to cl_salv_selections,
        lt_rows       type salv_t_row,
        lt_column     type salv_t_column,
        ls_cell       type salv_s_cell.

  lr_selections = gr_table->get_selections( ).

*... §7.1 set selection mode
  lr_selections->set_selection_mode( if_salv_c_selection_mode=>multiple ).

*... §7.2 set selected columns.
  append 'CARRID' to lt_column.
  append 'CONNID' to lt_column.
  append 'FLDATE' to lt_column.
  lr_selections->set_selected_columns( lt_column ).

*... §7.3 set selected rows.
  append 1 to lt_rows.
  append 2 to lt_rows.
  append 3 to lt_rows.
  lr_selections->set_selected_rows( lt_rows ).

*... §7.4 set current cell
  ls_cell-row        = 4.
  ls_cell-columnname = 'PRICE'.
  lr_selections->set_current_cell( ls_cell ).

gr_table->set_screen_status(
    pfstatus      =  'SALV_STANDARD'
    report        =  'ZALVTESTOTTEST'
    set_functions = gr_table->c_functions_all ).

*... §8 display the table
  gr_table->display( ).

endform.                    

form set_columns_technical 
      using ir_columns type ref to cl_salv_columns_table.

  data: lr_column type ref to cl_salv_column.

  try.
      lr_column = ir_columns->get_column( 'MANDT' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'FLOAT_FI' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'STRING_F' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'XSTRING' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                             
 endtry.

  try.
      lr_column = ir_columns->get_column( 'INT_FIEL' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'HEX_FIEL' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'DROPDOWN' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

  try.
      lr_column = ir_columns->get_column( 'TAB_INDEX' ).
      lr_column->set_technical( if_salv_c_bool_sap=>true ).
    catch cx_salv_not_found.                            
  endtry.

endform.                    


form get_selections .

  data: lr_selections type ref to cl_salv_selections.

  data: lt_rows   type salv_t_row,
        lt_cols   type salv_t_column,
        ls_cell   type salv_s_cell.

  data: l_row        type i,
        l_col        type lvc_fname,
        l_row_string type char128,
        l_col_string type char128,
        l_row_info   type char128,
        l_col_info   type char128.

  lr_selections = gr_table->get_selections( ).
  lt_rows = lr_selections->get_selected_rows( ).
  lt_cols = lr_selections->get_selected_columns( ).
  ls_cell = lr_selections->get_current_cell( ).

  clear l_row_info.
  loop at lt_rows into l_row.
  write l_row to l_row_string left-justified.
  concatenate l_row_info l_row_string into l_row_info separated by space.
  endloop.
  if sy-subrc eq 0.
    message i000(0k) with text-i02 l_row_info.
  endif.

  clear l_col_info.
  loop at lt_cols into l_col.
    write l_col to l_col_string left-justified.
    concatenate l_col_info l_col_string into l_col_info separated by space.
  endloop.
  if sy-subrc eq 0.
    message i000(0k) with text-i03 l_col_info.
  endif.

*... Zelle
  if ls_cell is not initial.
    message i000(0k) with text-i02 
    ls_cell-row text-i03 ls_cell-columnname.
  endif.

endform.                    

form show_cell_info using i_level  type i
                          i_row    type i
                          i_column type lvc_fname
                          i_text   type string.

  data: l_row_string type string,
        l_col_string type string,
        l_row        type char128.

  write i_row to l_row left-justified.

  concatenate text-i02 l_row into l_row_string separated by space.
  concatenate text-i03 i_column into l_col_string separated by space.

  if i_level is initial.
    message i000(0k) with i_text l_row_string l_col_string.
  else.
    case i_level.
      when 1.
        message i000(0k) with text-i04 
 i_text l_row_string l_col_string.
      when 2.
        message i000(0k) with text-i05 i_text l_row_string l_col_string.
    endcase.
  endif.

endform.                    


form handle_user_command using i_ucomm type salv_de_function.

  case i_ucomm.
    when 'GET_SEL'.
      perform get_selections.
  endcase.

endform.                    


Yorumlarınız ve düşünceleriniz varsa makale altındaki yorum bölümüne yazabilirsiniz. reklamlara tıklayarak destek olabilirsiniz,
bir başka yazıda görüşmek üzere.

#
Ezgim Çelik
They didn't know it was impossible, so they did it - Mark Twain