ABAP FUNCTION ALV简单屏幕复选框代码分享

如有不好之处请留言,希望追求完美。

接触ABAP时间 第56天

" 定义数据类型
TYPES : BEGIN OF WA_ITAB,
          VBELN TYPE VBRK-VBELN,  "发票编号
          GEWEI TYPE C,
          LIGHT TYPE C LENGTH 10, "红路灯接收
          SEL   TYPE C,           "复选框
        END OF WA_ITAB.

DATA : WA_VBRK   TYPE WA_ITAB,
       WA_OUTPUT LIKE TABLE OF WA_VBRK WITH HEADER LINE.

" 引入宏方式添加屏幕显示字段
DEFINE ADD_FIELD.
  WA_FIELD-FIELDNAME      = &1.
  WA_FIELD-REPTEXT_DDIC   = &2.
  WA_FIELD-EDIT           = &3. "设置可编辑
  WA_FIELD-CHECKBOX       = &4. "设置是否选择框
  APPEND WA_FIELD TO IT_FIELD.
END-OF-DEFINITION.

" 声明屏幕显示字段需要的变量
DATA: IT_FIELD TYPE SLIS_T_FIELDCAT_ALV.
DATA: WA_FIELD TYPE SLIS_FIELDCAT_ALV.

"两种写法均可
DATA: IT_FIELD1 TYPE SLIS_T_FIELDCAT_ALV,
      WA_FIELD1 TYPE SLIS_FIELDCAT_ALV.

START-OF-SELECTION.
*  PERFORM ADD_FIELD."调用屏幕显示字段方法1
  PERFORM FIELD_OUTPUT."调用屏幕显示字段方法2  宏方式
  PERFORM DATA_FILL."调用填充虚拟数据方法
  PERFORM DATA_OUTPUT."调用屏幕输出设置方法

" 处理屏幕显示字段方式2  宏方式
FORM FIELD_OUTPUT.
  CLEAR WA_FIELD.
  CLEAR IT_FIELD.
  ADD_FIELD 'SEL'         '选择框' 'X' 'X'.
  ADD_FIELD 'VBELN'       '物料编号' '' ''.
  ADD_FIELD 'GEWEI'       '单位'     '' ''.
  ADD_FIELD 'LIGHT'       '红绿灯'     '' ''.
ENDFORM.

" 屏幕输出展示字段设置
FORM DATA_OUTPUT.
  "定义ALV输入时宽度自动适应
  DATA: WA_LAYOUT TYPE SLIS_LAYOUT_ALV. "设置输出格式,自适应等
  WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.    "设置宽度自适应
  WA_LAYOUT-BOX_FIELDNAME = 'SEL'.    "设置复选框字段

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' " 调用函数
    EXPORTING
      IT_FIELDCAT = IT_FIELD
      IS_LAYOUT   = WA_LAYOUT  " 设置表单格式
    TABLES
      T_OUTTAB    = WA_OUTPUT.
ENDFORM.

"数据填充
FORM DATA_FILL.
  DO 3 TIMES.
    WA_VBRK-VBELN = '10001'.
    WA_VBRK-GEWEI = '10'.
    " 添加红绿灯简单方式
*    WA_VBRK-LIGHT = ICON_GREEN_LIGHT."绿
    WA_VBRK-LIGHT = ICON_YELLOW_LIGHT."黄
*    WA_VBRK-LIGHT = ICON_RED_LIGHT.  "红
    APPEND WA_VBRK TO WA_OUTPUT.
    CLEAR WA_VBRK.
  ENDDO.
ENDFORM.

你可能感兴趣的:(ABAP)