SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索

文章目录

    • 1 背景
    • 2.实现
      • 2.1 建立自定义搜索帮助
      • 2.2 在ALV中
      • 2.3 在table control中

1 背景

在alv或者table control里需要定义字段的搜索帮助,本文通过自定义的搜索帮助出口来实现,相对于定义alv的f4事件或者屏幕的process on value-request的方式代码量少,更加方便。同时自定义搜索帮助里需要取自ALV或者table control的其他字段来实现关联搜索帮助功能。
本文根据物料、工厂、库存地点取批次及库存信息

2.实现

2.1 建立自定义搜索帮助

事务代码SE11建立搜索帮助
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第1张图片
从标准搜索帮助出口函数F4IF_SHLP_EXIT_EXAMPLE复制到自定义函数如上图所示。
在select部分编写代码如下:
获取输入的条件值,然后根据条件取数,最后调用显示函数

DATA:lr_matnr TYPE RANGE OF mchb-matnr,
lr_werks TYPE RANGE OF mchb-werks,
lr_charg TYPE RANGE OF mchb-charg,
lr_lgort TYPE RANGE OF mchb-lgort.

IF callcontrol-step = ‘SELECT’.

LOOP AT shlp-selopt INTO DATA(ls_selopt).
  CASE ls_selopt-shlpfield.
    WHEN 'MATNR'.
      lr_matnr[] = VALUE #( BASE lr_matnr[] (
      sign   = ls_selopt-sign
      option = ls_selopt-option
      low    = ls_selopt-low
      high   = ls_selopt-high ) ).
    WHEN 'WERKS'.
      lr_werks[] = VALUE #( BASE lr_werks[] (
      sign   = ls_selopt-sign
      option = ls_selopt-option
      low    = ls_selopt-low
      high   = ls_selopt-high ) ).
    WHEN 'CHARG'.
      lr_charg[] = VALUE #( BASE lr_charg[] (
      sign   = ls_selopt-sign
      option = ls_selopt-option
      low    = ls_selopt-low
      high   = ls_selopt-high ) ).
    WHEN 'LGORT'.
      lr_lgort[] = VALUE #( BASE lr_lgort[] (
      sign   = ls_selopt-sign
      option = ls_selopt-option
      low    = ls_selopt-low
      high   = ls_selopt-high ) ).
    WHEN OTHERS.
  ENDCASE.
ENDLOOP.

SELECT matnr, werks, charg FROM v_cf_mcha
                     WHERE matnr IN @lr_matnr
                     AND   werks IN @lr_werks
                     AND   charg IN @lr_charg
                     AND   lvorm_0 = @space
                     INTO TABLE @DATA(lt_mcha_tab).
IF sy-subrc = 0.
  SELECT n~matnr, n~werks, n~lgort, n~charg, n~clabs,
   m~sgt_scat
   FROM ( nsdm_e_mchb_diff AS n
          INNER JOIN v_mchb_md AS m
          ON n~matnr = m~matnr AND
             n~werks = m~werks AND
             n~lgort = m~lgort AND
             n~charg = m~charg )
   FOR ALL ENTRIES IN @lt_mcha_tab
   WHERE n~matnr = @lt_mcha_tab-matnr
     AND n~werks = @lt_mcha_tab-werks
     AND n~charg = @lt_mcha_tab-charg
     AND n~lgort IN @lr_lgort
     AND n~clabs > 0
   INTO  TABLE @DATA(lt_charg_tab).
ENDIF.

SORT lt_charg_tab.

CALL FUNCTION 'F4UT_RESULTS_MAP'
  •    EXPORTING
    
  •      SOURCE_STRUCTURE         =
    
  •      APPLY_RESTRICTIONS       = ' '
    TABLES
      shlp_tab    = shlp_tab
      record_tab  = record_tab
      source_tab  = lt_charg_tab
    CHANGING
      shlp        = shlp
      callcontrol = callcontrol
    
  •    EXCEPTIONS
    
  •   ILLEGAL_STRUCTURE        = 1
    
  •   OTHERS      = 2
    .
    

    IF sy-subrc <> 0.

  • Implement suitable error handling here
    ENDIF.

    callcontrol-step = ‘DISP’.

    EXIT. "Don’t process STEP DISP additionally in this call.
    ENDIF.
    编写后激活

2.2 在ALV中

se11中ALV对应的结构中相应字段引入搜索帮助
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第2张图片
注意当前结构中的其他字段名称务必输入正确
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第3张图片
效果展示:
根据ALV中的其他字段显示搜索帮助
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第4张图片

SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第5张图片

2.3 在table control中

在table control中如果按照ALV的方法将不能实现关联搜索,此时需要在自定义搜索帮助的出口中disp处写入代码,实现逻辑为:先取table control的当前行中的关联字段,然后设置默认搜索条件,取数,最后调用显示函数。
代码如下:
DATA:lv_matnr TYPE mara-matnr.
DATA:lt_dynpfields LIKE dynpread OCCURS 0 WITH HEADER LINE.
DATA:lv_get_data_flag TYPE c.
IF callcontrol-step = ‘DISP’.

  • PERFORM AUTHORITY_CHECK TABLES RECORD_TAB SHLP_TAB

  •                       CHANGING SHLP CALLCONTROL.
    

    "仅限生产领退料单-修改领退料单-初始化搜索帮助时运行以下逻辑
    "取屏幕其他字段
    PERFORM frm_get_screen_data TABLES lt_dynpfields USING callcontrol CHANGING lv_get_data_flag.

    IF lv_get_data_flag = ‘X’.

    DELETE lt_dynpfields WHERE fieldvalue IS INITIAL.
    
    LOOP AT lt_dynpfields.
    
      CASE lt_dynpfields-fieldname.
        WHEN 'GT_OUT4-MATNR'.
          lv_matnr = lt_dynpfields-fieldvalue.
          CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
            EXPORTING
              input        = lv_matnr
            IMPORTING
              output       = lv_matnr
            EXCEPTIONS
              length_error = 1
              OTHERS       = 2.
          lt_dynpfields-fieldvalue = lv_matnr.
          lr_matnr[] = VALUE #( BASE lr_matnr[] (
          sign   = 'I'
          option = 'EQ'
          low    = lt_dynpfields-fieldvalue ) ).
        WHEN 'GT_OUT4-WERKS'.
          lr_werks[] = VALUE #( BASE lr_werks[] (
          sign   = 'I'
          option = 'EQ'
          low    = lt_dynpfields-fieldvalue ) ).
        WHEN 'GT_OUT4-CHARG'.
          lr_charg[] = VALUE #( BASE lr_charg[] (
          sign   = 'I'
          option = 'EQ'
          low    = lt_dynpfields-fieldvalue ) ).
        WHEN 'GT_OUT4-LGORT'.
          lr_lgort[] = VALUE #( BASE lr_lgort[] (
          sign   = 'I'
          option = 'EQ'
          low    = lt_dynpfields-fieldvalue ) ).
        WHEN OTHERS.
      ENDCASE.
    
      "设置默认搜索条件
      APPEND INITIAL LINE TO shlp-selopt ASSIGNING FIELD-SYMBOL().
      -shlpname = shlp-shlpname.
      SPLIT lt_dynpfields-fieldname AT '-' INTO DATA(lv_table) DATA(lv_field).
      -shlpfield = lv_field.
      -sign = 'I'.
      -option = 'EQ'.
      -low = lt_dynpfields-fieldvalue.
      CLEAR:lv_table,lv_field.
    ENDLOOP.
    
    SELECT matnr, werks, charg FROM v_cf_mcha
                      WHERE matnr IN @lr_matnr
                      AND   werks IN @lr_werks
                      AND   charg IN @lr_charg
                      AND   lvorm_0 = @space
                      INTO  TABLE @lt_mcha_tab.
    IF sy-subrc = 0.
      SELECT n~matnr, n~werks, n~lgort, n~charg, n~clabs,
       m~sgt_scat
       FROM ( nsdm_e_mchb_diff AS n
              INNER JOIN v_mchb_md AS m
              ON n~matnr = m~matnr AND
                 n~werks = m~werks AND
                 n~lgort = m~lgort AND
                 n~charg = m~charg )
       FOR ALL ENTRIES IN @lt_mcha_tab
       WHERE n~matnr = @lt_mcha_tab-matnr
         AND n~werks = @lt_mcha_tab-werks
         AND n~charg = @lt_mcha_tab-charg
         AND n~lgort IN @lr_lgort
         AND n~clabs > 0
       INTO  TABLE @lt_charg_tab.
    ENDIF.
    
    SORT lt_charg_tab.
    
    CLEAR:record_tab[].
    CALL FUNCTION 'F4UT_RESULTS_MAP'
    
  •    EXPORTING
    
  •      SOURCE_STRUCTURE         =
    
  •      APPLY_RESTRICTIONS       = ' '
      TABLES
        shlp_tab    = shlp_tab
        record_tab  = record_tab
        source_tab  = lt_charg_tab
      CHANGING
        shlp        = shlp
        callcontrol = callcontrol
    
  •    EXCEPTIONS
    
  •     ILLEGAL_STRUCTURE        = 1
    
  •     OTHERS      = 2
      .
    IF sy-subrc <> 0.
    
  • Implement suitable error handling here
    ENDIF.
    ENDIF.

    EXIT.
    ENDIF.

&---------------------------------------------------------------------
*& Form frm_get_screen_data
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_get_screen_data TABLES ft_dynpfields
USING callcontrol TYPE ddshf4ctrl
CHANGING fc_get_data_flag.

DATA:lt_dynpfields LIKE dynpread OCCURS 0 WITH HEADER LINE.
DATA:lv_stepl LIKE sy-stepl.
DATA:lv_repid LIKE sy-repid.
DATA:lv_dynnr LIKE sy-dynnr.

"GT_OUT4为table control的工作区名称
IF sy-msgv1 = ‘>_OUT4&’ AND sy-cprog = ‘ZPPE007’.

fc_get_data_flag = 'X'.

"点击的屏幕数据行号
lv_stepl = callcontrol-curow - 1.

REFRESH lt_dynpfields.

lt_dynpfields[] = VALUE #( BASE lt_dynpfields[] ( fieldname = 'GT_OUT4-MATNR' stepl = lv_stepl ) ).
lt_dynpfields[] = VALUE #( BASE lt_dynpfields[] ( fieldname = 'GT_OUT4-WERKS' stepl = lv_stepl ) ).
lt_dynpfields[] = VALUE #( BASE lt_dynpfields[] ( fieldname = 'GT_OUT4-LGORT' stepl = lv_stepl ) ).
lt_dynpfields[] = VALUE #( BASE lt_dynpfields[] ( fieldname = 'GT_OUT4-CHARG' stepl = lv_stepl ) ).

lv_repid = 'ZPPE007'.
lv_dynnr = '9000'.
CALL FUNCTION 'DYNP_VALUES_READ'
  EXPORTING
    dyname               = lv_repid
    dynumb               = lv_dynnr
  TABLES
    dynpfields           = lt_dynpfields
  EXCEPTIONS
    invalid_abapworkarea = 1
    invalid_dynprofield  = 2
    invalid_dynproname   = 3
    invalid_dynpronummer = 4
    invalid_request      = 5
    no_fielddescription  = 6
    invalid_parameter    = 7
    undefind_error       = 8
    double_conversion    = 9
    OTHERS               = 10.

ft_dynpfields[] = lt_dynpfields[].

ENDIF.

ENDFORM.

效果展示:
搜索条件默认带有table control中的关联字段(搜索字段)
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第6张图片
SAP 自定义搜索帮助在ALV和TABLE CONTROL里实现关联搜索_第7张图片

你可能感兴趣的:(ABAP,SAP,搜索帮助,SAP,搜索帮助出口,SAP,关联搜索帮助)