5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【ABAP】統合データアクセス(IDA)を備えたALVでテーブルデータを表示する(CL_SALV_GUI_TABLE_IDA)

Last updated at Posted at 2021-04-24

#やること
統合データアクセス(IDA)を備えたALVで、SAPのテーブルデータを表示する。
CL_SALV_GUI_TABLE_IDAを使用。

参考:SAP List Viewer with Integrated Data Access (ALV with IDA)

テーブルデータではなく、CDS Viewのデータを出力する方法についてはこちら
統合データアクセス(IDA)を備えたALVでCDS Viewのデータを表示する(CL_SALV_GUI_TABLE_IDA)

#実行結果
※EKKOのデータを表示。ダブルクリックするとEKPOの画面に遷移する

###選択画面
image.png

###ALV画面(EKKO)
image.png

###ALV画面で明細行をダブルクリック後(EKPO出力)
image.png

#ソースコード

Y_TEST_CL_SALV_GUI_TABLE_IDA
*&---------------------------------------------------------------------*
*& Report Y_TEST_CL_SALV_GUI_TABLE_IDA
*&---------------------------------------------------------------------*
*& 購買発注ヘッダ(EKKO)のデータをALV画面で出力
*&---------------------------------------------------------------------*
REPORT y_test_cl_salv_gui_table_ida.

* 選択画面定義
DATA w_ebeln TYPE ekko-ebeln.

PARAMETERS p_lifnr TYPE ekko-lifnr.
SELECT-OPTIONS s_ebeln FOR w_ebeln.

*----------------------------------------------------------------------*
*       CLASS DEFINITION
*----------------------------------------------------------------------*
*----- メイン
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS run.
  PRIVATE SECTION.

ENDCLASS.

*----- ハンドルアクション
CLASS lcl_handle_action DEFINITION.
  PUBLIC SECTION.
    METHODS :
      constructor IMPORTING io_ida TYPE REF TO if_salv_gui_table_ida,
      handle_dbclick FOR EVENT double_click OF if_salv_gui_table_display_opt.
    DATA o_ida TYPE REF TO if_salv_gui_table_ida.

ENDCLASS.
*----------------------------------------------------------------------*
*       CLASS IMPLEMENTATION
*----------------------------------------------------------------------*
*----- メイン
CLASS lcl_main IMPLEMENTATION.
  METHOD run.

* DBチェック
    CHECK cl_salv_gui_table_ida=>db_capabilities( )->is_table_supported( iv_ddic_table_name = 'EKKO').

* IDA作成
    DATA(o_ida) = cl_salv_gui_table_ida=>create( iv_table_name = 'EKKO' ). "テーブルID指定

* 最大行の設定
    IF cl_salv_gui_table_ida=>db_capabilities( )->is_max_rows_recommended( ).
      o_ida->set_maximum_number_of_rows(  iv_number_of_rows = 20 ).  "20行だけ出力
    ENDIF.

* 抽出条件の設定
*   PARAMETERSの場合(値が設定されてる場合、RANGE型に格納し設定)
    DATA lr_lifnr TYPE RANGE OF ekko-lifnr.
    DATA(o_sel) = NEW cl_salv_range_tab_collector( ).
    IF p_lifnr IS NOT INITIAL.
      lr_lifnr = VALUE #(
        ( sign = 'I' option = 'EQ' low = p_lifnr )
      ).
      o_sel->add_ranges_for_name( iv_name = 'LIFNR' it_ranges = lr_lifnr[] ).
    ENDIF.
*   SELECT-OPTIONSの場合
    o_sel->add_ranges_for_name( iv_name = 'EBELN' it_ranges = s_ebeln[] ).

    o_sel->get_collected_ranges( IMPORTING et_named_ranges = DATA(lt_named_ranges) ).
    o_ida->set_select_options( it_ranges = lt_named_ranges ).

* 出力項目の設定
    DATA lt_field_names TYPE if_salv_gui_types_ida=>yts_field_name.
    lt_field_names = VALUE #(
        ( CONV string('LIFNR') )
        ( CONV string('EBELN') )
        ( CONV string('BSART') )
        ( CONV string('EKORG') )
        ( CONV string('EKGRP') )
      ).

    o_ida->field_catalog( )->set_available_fields( EXPORTING its_field_names = lt_field_names ).

* ソート
    DATA lt_sort_order TYPE if_salv_gui_types_ida=>yt_sort_rule.
    lt_sort_order = VALUE #(
      ( field_name = 'LIFNR' descending = abap_false is_grouped = abap_true )
      ( field_name = 'EBELN' descending = abap_true is_grouped = abap_false )
    ).

    o_ida->default_layout( )->set_sort_order( EXPORTING it_sort_order = lt_sort_order ).

* ダブルクリック有効
    o_ida->display_options( )->enable_double_click( ).
* selection Mode
    o_ida->selection( )->set_selection_mode( EXPORTING iv_mode = 'SINGLE' ).

* イベントハンドラー
    DATA(o_handler) = NEW lcl_handle_action( io_ida = o_ida ) .
    SET HANDLER o_handler->handle_dbclick FOR o_ida->display_options( ) .

* ALV出力
    o_ida->fullscreen( )->display( ).

  ENDMETHOD.
ENDCLASS.                    "lcl_main IMPLEMENTATION

*----- ハンドルアクション
* EKKOでダブルクリックされた行の購買伝票番号(EBELN)を条件に、EKPOを出力
CLASS lcl_handle_action IMPLEMENTATION.
  METHOD constructor .
    o_ida = io_ida.
  ENDMETHOD.
  METHOD handle_dbclick.

    DATA ls_ekko TYPE ekko.
    DATA it_named_ranges TYPE if_salv_service_types=>yt_named_ranges.
    IF o_ida IS BOUND.
      IF o_ida->selection( )->is_row_selected( ).
        " Get selected row

        o_ida->selection( )->get_selected_row( IMPORTING es_row = ls_ekko ).
        DATA(o_ida_item) = cl_salv_gui_table_ida=>create( iv_table_name = 'EKPO' ).
        IF o_ida_item IS BOUND.
          it_named_ranges = VALUE #(
                              ( name = 'EBELN'
                                sign = 'I'
                                option = 'EQ'
                                low = ls_ekko-ebeln )
                               ).
          o_ida_item->set_select_options( it_ranges = it_named_ranges ).
          o_ida_item->fullscreen( )->display( ) .
        ENDIF.

      ELSE.
        MESSAGE 'No row selected' TYPE ' I' .
      ENDIF.
    ENDIF.

  ENDMETHOD .

ENDCLASS.
*----------------------------------------------------------------------*
*       START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.

  lcl_main=>run( ).
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?