ABAP tablecontrol Table Control 增加 过滤 筛选 功能 按钮

戚逸清
2023-12-01
FORM fcode_tc_filter  USING p_tc_name p_table_name.
  DATA: ls_col     LIKE LINE OF tc_01-cols,
        lt_sel_col TYPE lvc_t_col WITH HEADER LINE,
        lt_fcat    TYPE lvc_t_fcat,
        ls_layo    TYPE lvc_s_layo,
        lt_filt    TYPE lvc_t_filt WITH HEADER LINE,
        lt_fidx    TYPE lvc_t_fidx WITH HEADER LINE,
        lt_output  TYPE TABLE OF ty_price WITH HEADER LINE,
        lt_indx    TYPE RANGE OF i,
        ls_indx    LIKE LINE OF lt_indx,
        lt_cpdr    TYPE TABLE OF ty_price WITH HEADER LINE,
        ls_price   TYPE ty_price.

  gt_price_buffer = gt_price. "为取消过滤做准备 往往把过滤和取消过滤只能出现其一
  LOOP AT tc_01-cols INTO ls_col WHERE selected = 'X'.
    lt_sel_col-fieldname = ls_col-screen-name+10(5).
    APPEND lt_sel_col.
  ENDLOOP.

* 半自动创建表头目录方法:LVC_FIELDCATALOG_MERGE
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'ZSDTPRICE'
    CHANGING
      ct_fieldcat            = lt_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 99.

  IF sy-subrc = 0.
    CLEAR lt_fidx[].
    CALL FUNCTION 'LVC_FILTER'
      EXPORTING
        it_fieldcat      = lt_fcat
        it_selected_cols = lt_sel_col[]
        is_layout        = ls_layo
      IMPORTING
        et_filter_index  = lt_fidx[] "该参数传出被筛除的数据行号
      TABLES
        it_data          = gt_price
      CHANGING
        ct_filter        = lt_filt[]
      EXCEPTIONS
        OTHERS           = 0.

    LOOP AT lt_fidx.
      READ TABLE gt_price INTO ls_price INDEX lt_fidx.
      lt_output = ls_price .
      APPEND lt_output.
    ENDLOOP.


    LOOP AT lt_output  .
      READ TABLE gt_price INTO ls_price WITH KEY bjfs = lt_output-bjfs
                                                 werks = lt_output-werks
                                                 bjlb = lt_output-bjlb
                                                 wlflbm = lt_output-wlflbm.
      IF sy-subrc = 0.
        DELETE gt_price INDEX sy-tabix.
      ENDIF.
    ENDLOOP.
  ENDIF.
ENDFORM.                    " FCODE_TC_FILTER

FORM fcode_tc_filter_undo  USING p_tc_name p_table_name.
  IF gt_price_buffer IS NOT INITIAL.
    gt_price = gt_price_buffer.
  ENDIF.
ENDFORM.                    " FCODE_TC_FILTER_UNDO
 类似资料: