二维码

[salv] ABAP报表开发HANA平台大数据展示工具SALV学习之四

Twilight发表于 2019-10-09 10:00Twilight 最后回复于 2019-10-09 10:00 [复制链接] 4314 0

1、sALV添加计算列,计算列的概念来自HANA数据库计算试图,计算列是通过基础列经过一些计算得出的一列数据。
本示例,根据销售订单美元价格简单计算人民币价格。

2、se24 创建计算列类
  1. CLASS zcl_vbak_calc_field DEFINITION
  2.   PUBLIC
  3.   FINAL
  4.   CREATE PUBLIC .

  5.   PUBLIC SECTION.

  6.     INTERFACES if_salv_ida_calc_field_handler .

  7.     TYPES:
  8.       BEGIN OF ty_vbak,
  9.         netwr_cny TYPE vbak-netwr,
  10.         waerk_cny TYPE vbak-waerk,
  11.       END OF ty_vbak .

  12.     DATA gs_calc_vbak TYPE ty_vbak .
  13.   PROTECTED SECTION.
  14.   PRIVATE SECTION.
  15. ENDCLASS.



  16. CLASS zcl_vbak_calc_field IMPLEMENTATION.


  17.   METHOD if_salv_ida_calc_field_handler~calculate_line.
  18.     DATA:ls_vbak TYPE vbak.
  19.     DATA:ls_calc_field TYPE ty_vbak.

  20.     ls_vbak = is_data_base_line.
  21.     ls_calc_field-netwr_cny = ls_vbak-netwr * 7.
  22.     ls_calc_field-waerk_cny = 'CNY'.
  23.     es_calculated_fields = ls_calc_field.

  24.   ENDMETHOD.


  25.   METHOD if_salv_ida_calc_field_handler~get_calc_field_structure.
  26.     ro_calc_field_structure ?= cl_abap_structdescr=>describe_by_data( p_data = gs_calc_vbak ).
  27.   ENDMETHOD.


  28.   METHOD if_salv_ida_calc_field_handler~get_requested_fields.
  29.     DATA:ls_fname TYPE LINE OF if_salv_ida_types=>yts_field_name.
  30.     "列出所有参与计算的字段
  31.     ls_fname = 'NETWR'.
  32.     INSERT ls_fname INTO TABLE rts_db_field_name.

  33.   ENDMETHOD.
  34.   METHOD if_salv_ida_calc_field_handler~end_page.

  35.   ENDMETHOD.

  36.   METHOD if_salv_ida_calc_field_handler~start_page.

  37.   ENDMETHOD.

  38. ENDCLASS.
复制代码
3、salv调用计算列
  1. CLASS lcl_handle DEFINITION.
  2.   PUBLIC SECTION.
  3.     METHODS double_click FOR EVENT double_click OF if_salv_gui_table_display_opt
  4.       IMPORTING ev_field_name eo_row_data.
  5.     METHODS cell_action FOR EVENT cell_action OF if_salv_gui_field_display_opt
  6.       IMPORTING ev_field_name eo_row_data.
  7. ENDCLASS.

  8. CLASS lcl_handle IMPLEMENTATION.
  9.   METHOD double_click.
  10.     DATA: ls_vbak TYPE vbak.
  11.     CHECK ev_field_name = 'VBELN'.
  12. * read the row data
  13.     eo_row_data->get_row_data(
  14.       EXPORTING
  15.         iv_request_type = if_salv_gui_selection_ida=>cs_request_type-all_fields
  16.       IMPORTING
  17.         es_row = ls_vbak ).
  18. * Display the row data
  19.     cl_salv_ida_show_data_row=>display( iv_text = 'VBAK Row Info' is_data = ls_vbak ).
  20.   ENDMETHOD.
  21.   METHOD cell_action.
  22.     DATA: ls_vbak TYPE vbak.
  23.     CHECK ev_field_name = 'VBELN'.
  24. * read the row data
  25.     eo_row_data->get_row_data(
  26.       EXPORTING
  27.         iv_request_type = if_salv_gui_selection_ida=>cs_request_type-all_fields
  28.       IMPORTING
  29.         es_row = ls_vbak ).
  30. * Display the row data
  31.     cl_salv_ida_show_data_row=>display( iv_text = 'VBAK Row Info' is_data = ls_vbak ).
  32.   ENDMETHOD.
  33. ENDCLASS.

  34. TABLES: vbak.

  35. SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.

  36. START-OF-SELECTION.
  37. **********************************************************************
  38. *alv data
  39. **********************************************************************
  40.   DATA:lr_calc_field TYPE REF TO zcl_vbak_calc_field.

  41.   CREATE OBJECT lr_calc_field.
  42.   TRY.
  43.       CALL METHOD cl_salv_gui_table_ida=>create
  44.         EXPORTING
  45.           iv_table_name         = 'VBAK'
  46.           io_calc_field_handler = lr_calc_field
  47.         RECEIVING
  48.           ro_alv_gui_table_ida  = DATA(lr_alv).
  49.     CATCH cx_salv_db_connection.
  50.     CATCH cx_salv_db_table_not_supported.
  51.     CATCH cx_salv_ida_contract_violation.
  52.   ENDTRY.

  53. **********************************************************************
  54. *alv select option
  55. **********************************************************************
  56.   DATA:lt_ranges TYPE if_salv_service_types=>yt_named_ranges.
  57.   DATA:ls_ranges TYPE if_salv_service_types=>ys_named_range.
  58.   DATA: lr_range TYPE REF TO cl_salv_range_tab_collector.
  59.   DATA: lr_handle TYPE REF TO lcl_handle.

  60.   CREATE OBJECT lr_range.
  61.   lr_range->add_ranges_for_name(
  62.     EXPORTING
  63.       iv_name   = 'VBELN'
  64.       it_ranges = s_vbeln[]
  65.   ).

  66.   lr_range->get_collected_ranges(
  67.     IMPORTING
  68.       et_named_ranges = lt_ranges[]
  69.   ).

  70.   ls_ranges-name = 'WAERK'.
  71.   ls_ranges-sign = 'I'.
  72.   ls_ranges-option = 'EQ'.
  73.   ls_ranges-low = 'USD'.
  74.   APPEND ls_ranges TO lt_ranges.

  75.   TRY.
  76.       lr_alv->set_select_options(
  77.     EXPORTING
  78.       it_ranges    = lt_ranges[]
  79.   ).
  80.     CATCH cx_salv_ida_associate_invalid.
  81.     CATCH cx_salv_db_connection.
  82.     CATCH cx_salv_ida_condition_invalid.
  83.     CATCH cx_salv_ida_unknown_name.
  84.   ENDTRY.

  85. **********************************************************************
  86. *alv select option
  87. **********************************************************************
  88.   DATA(lr_option) = lr_alv->display_options( ).
  89.   lr_option->set_title( iv_title = 'VBAK information alv with ida' ).
  90.   lr_option->enable_alternating_row_pattern( ).
  91.   lr_option->enable_double_click( ).
  92.   CREATE OBJECT lr_handle.
  93.   SET HANDLER lr_handle->double_click FOR ALL INSTANCES.
  94. **********************************************************************
  95. *alv field catalog
  96. **********************************************************************
  97.   DATA(lr_fcatlog) = lr_alv->field_catalog( ).
  98.   DATA(lr_display) = lr_fcatlog->display_options( ).
  99.   TRY.
  100.       lr_display->display_as_link_to_action( iv_field_name = 'VBELN' ).
  101.     CATCH cx_salv_ida_unknown_name.
  102.     CATCH cx_salv_call_after_1st_display.
  103.   ENDTRY.
  104.   SET HANDLER lr_handle->cell_action FOR ALL INSTANCES.
  105. * Prepare the field catalog for two calculated fields
  106.   TRY.
  107.       lr_fcatlog->set_field_header_texts(
  108.     EXPORTING
  109.       iv_field_name        = 'NETWR_CNY'
  110.       iv_header_text       = 'CNY Value'
  111.       iv_tooltip_text      = 'Value of CNY'
  112.       iv_tooltip_text_long = 'currency Value of CNY'
  113.   ).
  114.     CATCH cx_salv_ida_unknown_name INTO DATA(lc_error).
  115.       DATA(lv_error) = lc_error->get_text( ).
  116.     CATCH cx_salv_call_after_1st_display.
  117.       lv_error = lc_error->get_text( ).
  118.   ENDTRY.

  119.   TRY.
  120.       lr_fcatlog->set_field_header_texts(
  121.     EXPORTING
  122.       iv_field_name        = 'WAERK_CNY'
  123.       iv_header_text       = 'CNY'
  124.       iv_tooltip_text      = 'unit of CNY'
  125.       iv_tooltip_text_long = 'currency unit of CNY'
  126.   ).
  127.     CATCH cx_salv_ida_unknown_name.
  128.     CATCH cx_salv_call_after_1st_display.
  129.   ENDTRY.
  130. **********************************************************************
  131. *alv select option
  132. **********************************************************************
  133. *  DATA: lt_sort TYPE if_salv_gui_types_ida=>yt_sort_rule.
  134. *  DATA: ls_sort TYPE if_salv_gui_types_ida=>ys_sort_rule.
  135. *  DATA: lt_aggregations TYPE if_salv_gui_types_ida=>yt_aggregation_rule.
  136. *  DATA: ls_aggregations TYPE if_salv_gui_types_ida=>ys_aggregation_rule.
  137. *
  138. *  CLEAR: ls_sort.
  139. *  ls_sort-field_name = 'ERNAM'.
  140. *  ls_sort-descending = ''.
  141. *  ls_sort-is_grouped = 'X'.   "按照字段ERNAM分类
  142. *  APPEND ls_sort TO lt_sort.
  143. *
  144. *  CLEAR: ls_aggregations.
  145. *  ls_aggregations-field_name = 'NETWR'.
  146. *  ls_aggregations-function   = 'SUM'. "SUM,AVG,MIN,MAX,按照分类聚合字段NETWR数值
  147. *  APPEND ls_aggregations TO lt_aggregations.
  148. *
  149. *  DATA(lr_layout) = lr_alv->default_layout( ).
  150. *  TRY.
  151. *      lr_layout->set_sort_order( it_sort_order = lt_sort ).
  152. *    CATCH cx_salv_ida_unknown_name.
  153. *    CATCH cx_salv_ida_duplicate_name.
  154. *  ENDTRY.
  155. *
  156. *  TRY.
  157. *      lr_layout->set_aggregations( it_aggregations = lt_aggregations ).
  158. *    CATCH cx_salv_key_figure_definition INTO DATA(lc_text).
  159. *    CATCH cx_salv_ida_unknown_name.
  160. *    CATCH cx_salv_ida_contract_violation.
  161. *  ENDTRY.
  162. **********************************************************************
  163. *alv rows
  164. **********************************************************************
  165.   TRY.
  166.       lr_alv->set_maximum_number_of_rows(
  167.         EXPORTING
  168.           iv_number_of_rows = 20
  169.       ).
  170.     CATCH cx_salv_param_out_of_bounds.
  171.   ENDTRY.
  172. **********************************************************************
  173. *alv dispaly
  174. **********************************************************************
  175.   TRY.
  176.       lr_alv->fullscreen( )->display( ).
  177.     CATCH cx_salv_ida_contract_violation.
  178.   ENDTRY.
复制代码
4、执行效果展示
salv-calc.jpg

回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表