grid_alv

作者: 聂落尘 | 来源:发表于2023-09-10 09:48 被阅读0次
    REPORT
    REPORT zlc_alv_learn_1.
    
    INCLUDE zlc_alv_learn_1_top.
    INCLUDE zlc_alv_learn_1_form.
    
    START-OF-SELECTION.
    PERFORM form_get_data.
    
    END-OF-SELECTION.
    PERFORM form_build_alv.
    
    zlc_alv_learn_1_top
    TYPE-POOLS slis.
    TYPE-POOLS icon.
    
    TYPES: BEGIN OF gty_student.
             INCLUDE STRUCTURE zstudent_hq_03.
    TYPES:   icon      TYPE c LENGTH 4, "增加图标列,用来显示图标
             check     TYPE c LENGTH 1, " 复选框
             box       TYPE c LENGTH 1, " alv最左侧的选择框
             linecolor TYPE c LENGTH 4, "行颜色
             tabcolor  TYPE lvc_t_scol," 单元格颜色
           END OF gty_student.
    
    DATA: gt_student TYPE TABLE OF gty_student,
          gs_student TYPE gty_student.
    
    DATA: gt_fieldcat TYPE slis_t_fieldcat_alv, " 列属性
          gs_fieldcat TYPE slis_fieldcat_alv.
    
    DATA: gs_layout TYPE slis_layout_alv.
    
    " 排序
    DATA: gt_sort TYPE slis_t_sortinfo_alv,
          gs_sort TYPE slis_sortinfo_alv.
    
    " 筛选
    DATA: gt_filter TYPE slis_t_filter_alv,
          gs_filter TYPE slis_filter_alv.
    
    " 事件
    DATA: gt_event TYPE slis_t_event,
          gs_event TYPE slis_alv_event.
    
    DATA: gs_varient TYPE disvariant.
    
    ZLC_ALV_LEARN_1_FORM
    " 宏定义一般前面用加上%%,也可以不加, 一个宏最多9个参数
    *DEFINE %%append_fieldcat.
    *  CLEAR gs_fieldcat.
    *  gs_fieldcat-col_pos = &1.
    *  gs_fieldcat-fieldname = &2.
    *  gs_fieldcat-outputlen = &3.
    *  gs_fieldcat-seltext_m = &4.
    *  CASE &2.
    *    WHEN 'ZCODE'.
    *      gs_fieldcat-key = 'X'." 设置为关键列
    *      WHEN 'ICON'.
    *         gs_fieldcat-icon = 'X'." 只要设置了图标, 不添加这一行也会显示图标
    *    WHEN OTHERS.
    *  ENDCASE.
    *  APPEND gs_fieldcat TO gt_fieldcat.
    *END-OF-DEFINITION.
    FORM form_show_alv .
      gs_varient-report = sy-repid.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
    *     I_INTERFACE_CHECK           = ' '
    *     I_BYPASSING_BUFFER          = ' '
    *     I_BUFFER_ACTIVE             = ' '
          i_callback_program          = sy-cprog " 调用程序的名称
    *     I_CALLBACK_PF_STATUS_SET    = ' '
    *     I_CALLBACK_USER_COMMAND     = ' '
    *     I_CALLBACK_TOP_OF_PAGE      = ' '
          i_callback_html_top_of_page = 'FORM_HTML_TOP_OF_PAGE'
    *     I_CALLBACK_HTML_END_OF_LIST = ' '
    *     I_STRUCTURE_NAME            = " 可以传一个数据库表的名字,就不需要传it_fieldcat字段
    *     I_BACKGROUND_ID             = ' '
          i_grid_title                = 'GRID ALV 标题'
    *     I_GRID_SETTINGS             =
          is_layout                   = gs_layout
          it_fieldcat                 = gt_fieldcat
    *     IT_EXCLUDING                =
    *     IT_SPECIAL_GROUPS           =
          it_sort                     = gt_sort
          it_filter                   = gt_filter
    *     IS_SEL_HIDE                 =
    *     I_DEFAULT                   = 'X'
          i_save                      = 'A'
          is_variant                  = gs_varient
          it_events                   = gt_event
    *     IT_EVENT_EXIT               =
    *     IS_PRINT                    =
    *     IS_REPREP_ID                =
    *     I_SCREEN_START_COLUMN       = 0
    *     I_SCREEN_START_LINE         = 0
    *     I_SCREEN_END_COLUMN         = 0
    *     I_SCREEN_END_LINE           = 0
    *     I_HTML_HEIGHT_TOP           = 0
    *     I_HTML_HEIGHT_END           = 0
    *     IT_ALV_GRAPHICS             =
    *     IT_HYPERLINK                =
    *     IT_ADD_FIELDCAT             =
    *     IT_EXCEPT_QINFO             =
    *     IR_SALV_FULLSCREEN_ADAPTER  =
    *     O_PREVIOUS_SRAL_HANDLER     =
    * IMPORTING
    *     E_EXIT_CAUSED_BY_CALLER     =
    *     ES_EXIT_CAUSED_BY_USER      =
        TABLES
          t_outtab                    = gt_student
        EXCEPTIONS
          program_error               = 1
          OTHERS                      = 2.
      IF sy-subrc <> 0.
    * Implement suitable error handling here
      ENDIF.
    ENDFORM.
    *&---------------------------------------------------------------------*
    *& Form form_get_data
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM form_get_data .
      DATA ls_col TYPE lvc_s_scol.
    
      SELECT * FROM zstudent_hq_03 INTO CORRESPONDING FIELDS OF TABLE gt_student.
      LOOP AT gt_student INTO gs_student.
        IF gs_student-sex = 'F'.
          gs_student-icon = icon_green_light.
        ELSE.
          gs_student-icon = icon_red_light.
        ENDIF.
    
        "行颜色
    *    IF gs_student-sex is INITIAL.
    *      gs_student-linecolor = 'C600'.
    *    ENDIF.
    
        " 单元格颜色
        " 性别字段添加颜色
        IF gs_student-sex = 'M'.
          CLEAR LS_COL.
          ls_col-fname = 'SEX'." 字段名
          ls_col-color-col = 6." 颜色
          ls_col-color-int = 0." 加重
          ls_col-color-inv = 0." 反色
          APPEND ls_col TO gs_student-tabcolor.
        ENDIF.
    
        MODIFY gt_student FROM gs_student.
      ENDLOOP.
    ENDFORM.
    
    
    FORM form_build_fieldcat.
      PERFORM form_fieldcat USING: '1' 'ZCODE' '10' '学生编号',
                                    '2' 'ZNAME'  '20' '学生姓名',
                                    '3' 'SEX' '3' '性别',
                                     '4' 'ZSCHOOL' '10' '学校',
                                     '5' 'WEIGH' '15' '体重',
                                     '6' 'ZUNIT' '5' '单位',
                                     '7' 'ICON' '4' '图标',
                                     '8' 'CHECK' '3' '选择'.
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *& Form form_build_alv
    *&---------------------------------------------------------------------*
    *& text
    *&---------------------------------------------------------------------*
    *& -->  p1        text
    *& <--  p2        text
    *&---------------------------------------------------------------------*
    FORM form_build_alv .
      PERFORM form_build_layout.
      PERFORM form_build_fieldcat.
    *  PERFORM form_sort.
    *  PERFORM form_filter.
      PERFORM form_event.
      PERFORM form_show_alv.
    ENDFORM.
    
    " 排序
    FORM form_sort.
      " 显按照SCHOOL升序,在按照SEX降序
      CLEAR gs_sort.
      gs_sort-spos = '1'.
      gs_sort-fieldname = 'ZSCHOOL'.
      gs_sort-up = 'X'.
      gs_sort-subtot = 'X'. "根据ZSCHOOL字段进行小计, 前提是已有字段进行合计
      APPEND gs_sort TO gt_sort.
      CLEAR gs_sort.
      gs_sort-spos = '2'.
      gs_sort-fieldname = 'SEX'.
      gs_sort-down = 'X'.
      APPEND gs_sort TO gt_sort.
    ENDFORM.
    
    " 筛选
    FORM form_filter.
      CLEAR gs_filter.
      gs_filter-fieldname = 'SEX'.
      gs_filter-sign0 = 'E'.
      gs_filter-optio = 'EQ'.
      gs_filter-valuf = ''.
      APPEND gs_filter TO gt_filter.
    ENDFORM.
    
    " 事件
    FORM form_event.
      CLEAR gs_event.
      gs_event-name = 'PF_STATUS_SET'.
      gs_event-form = 'FORM_SET_STATUS'.
      APPEND gs_event TO gt_event.
    
      CLEAR gs_event.
      gs_event-name = 'USER_COMMAND'.
      gs_event-form = 'FORM_USER_COMMAND'.
      APPEND gs_event TO gt_event.
    
      CLEAR gs_event.
      gs_event-name = 'TOP_OF_PAGE'.
      gs_event-form = 'FORM_TOP_OF_PAGE'.
      APPEND gs_event TO gt_event.
    
    ENDFORM.
    
    
    
    FORM form_set_status  USING rt_extab TYPE slis_t_extab.
      SET PF-STATUS 'STAUTS1' EXCLUDING rt_extab.
    ENDFORM.
    
    
    FORM form_user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield.
      CASE r_ucomm.
        WHEN 'SMARFORM'.
          MESSAGE i002(zlc_me/ossage) WITH '点击了下载'.
        WHEN '&IC1'. "单击功能码(可以通过打断点获取), 设置过热点才可单击
          " 单击和双击公用一个功能码
          CASE rs_selfield-fieldname.
            WHEN 'ZCODE'. " 单击或双击
              SET PARAMETER ID 'LIF' FIELD rs_selfield-value.
              CALL TRANSACTION 'FK03'.
            WHEN 'WEIGH'." 单击或双击
              MESSAGE s003(zlc_message) WITH '点击的内容' rs_selfield-value.
            WHEN OTHERS.
              " 双击跳转到学校alv
              READ TABLE gt_student INTO gs_student INDEX rs_selfield-tabindex. "rs_selfield-tabindex 点击的第几行
              PERFORM form_student_alv USING gs_student.
          ENDCASE.
        WHEN OTHERS.
      ENDCASE.
    ENDFORM.
    
    
    FORM form_fieldcat USING col fieldname optlen text_m.
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos = col.
      gs_fieldcat-fieldname = fieldname.
      gs_fieldcat-outputlen = optlen.
      gs_fieldcat-seltext_m = text_m.
      CASE fieldname.
        WHEN 'ZCODE'.
          gs_fieldcat-key = 'X'." 设置为关键列
        WHEN 'ICON'.
          gs_fieldcat-icon = 'X'." 以图标显示(只要设置了图标, 不添加这一行也会显示图标)
        WHEN 'CHECK'.
          gs_fieldcat-checkbox = 'X'." 以复选框显示
          "gs_fieldcat-edit = 'X'.
        WHEN 'WEIGH'.
          gs_fieldcat-do_sum = 'X'." 对字段进行汇总
          gs_fieldcat-hotspot = 'X'." 设置热点
          "gs_fieldcat-quantity = 'KG'." 直接指明的固定单位
          " 如果数据中,单位一致,比如有的是kg有的是g,则需要添加该字段
          "货币也有同样的字段 cfieldname
          gs_fieldcat-qfieldname = 'ZUNIT'."内表中标明当前数值的单位字段
          " 以列为单位设置颜色
          "gs_fieldcat-emphasize = 'C511'.
        WHEN OTHERS.
      ENDCASE.
      APPEND gs_fieldcat TO gt_fieldcat.
    ENDFORM.
    
    FORM form_build_layout.
      gs_layout-colwidth_optimize = 'X'." 优化列宽
      gs_layout-zebra = 'X'." 斑马纹输出
      " 在 fieldcat中设置过key的字段都会设置热点
      gs_layout-key_hotspot = 'X'." 设置热点
      gs_layout-box_fieldname = 'BOX'."内表中的字段名称, 定义为当前行的选择按钮
      gs_layout-detail_initial_lines = 'X'." 是否显示值为空的字段
      gs_layout-window_titlebar = '窗体描述'.
      gs_layout-detail_titlebar = '对话框描述'.
      " 退出时是否弹出确认对话框
      gs_layout-confirmation_prompt = 'X'.
    
      " 以行为单位设置颜色 字段名
      "gs_layout-info_fieldname = 'LINECOLOR'.
    
      " 以单元格为单色设置颜色
        gs_layout-coltab_fieldname = 'TABCOLOR'.
    ENDFORM.
    
    FORM form_top_of_page.
      " 不论添加内标的顺序,展示的时候从上到下的顺序时 H S A
      " 内容可以重复添加
      DATA: lt_header TYPE slis_t_listheader,
            ls_header TYPE slis_listheader.
      ls_header-typ = 'H'.
      ls_header-info = '表头描述文本'.
      APPEND ls_header TO lt_header.
    
      CLEAR ls_header.
      ls_header-typ = 'S'.
      ls_header-key = '字段名称或描述'.
      ls_header-info = '字段值'.
      APPEND ls_header TO lt_header.
    
      CLEAR ls_header.
      ls_header-typ = 'A'.
      ls_header-info = '结尾斜体'.
      APPEND ls_header TO lt_header.
    
      CLEAR ls_header.
      ls_header-typ = 'S'.
      ls_header-key = '字段名称或描述'.
      ls_header-info = '字段值'.
      APPEND ls_header TO lt_header.
    
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          it_list_commentary = lt_header
          i_logo             = 'ZLC_LOGO'
    *     I_END_OF_LIST_GRID =
    *     I_ALV_FORM         =
        .
    ENDFORM.
    
    FORM form_html_top_of_page USING pr_document TYPE REF TO cl_dd_document.
      DATA lv_datum TYPE sdydo_text_element.
      CALL METHOD pr_document->add_text
        EXPORTING
          text      = '学生信息表'
          sap_style = cl_dd_document=>table_heading.
      CALL METHOD pr_document->new_line. "新行
      CALL METHOD pr_document->add_text
        EXPORTING
          text         = '日期'
          sap_fontsize = cl_dd_document=>medium.
      CALL METHOD pr_document->add_gap " 添加间隔
        EXPORTING
          width = 3.
      WRITE sy-datum TO lv_datum USING EDIT MASK '__年__月__日'.
      CALL METHOD pr_document->add_text
        EXPORTING
          text         = lv_datum
          sap_fontsize = cl_dd_document=>medium.
      CALL METHOD pr_document->new_line
        EXPORTING
          repeat = 1.
      CALL METHOD pr_document->add_text
        EXPORTING
          text         = '链接'
          sap_fontsize = cl_dd_document=>medium.
      CALL METHOD pr_document->add_gap
        EXPORTING
          width = 5.
      CALL METHOD pr_document->add_link
        EXPORTING
          text = '百度'
          url  = 'http://www.baidu.com'.
    
    ENDFORM.
    
    
    FORM form_student_alv USING gs_stu TYPE gty_student.
    
      DATA gt_school TYPE TABLE OF zschool_hq_03.
      SELECT * FROM zschool_hq_03 INTO CORRESPONDING FIELDS OF TABLE gt_school WHERE zschool = gs_stu-zschool.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
    *     I_INTERFACE_CHECK                 = ' '
    *     I_BYPASSING_BUFFER                = ' '
    *     I_BUFFER_ACTIVE  = ' '
    *     I_CALLBACK_PROGRAM                = ' '
    *     I_CALLBACK_PF_STATUS_SET          = ' '
    *     I_CALLBACK_USER_COMMAND           = ' '
    *     I_CALLBACK_TOP_OF_PAGE            = ' '
    *     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
    *     I_CALLBACK_HTML_END_OF_LIST       = ' '
          i_structure_name = 'ZSCHOOL_HQ_03'
    *     I_BACKGROUND_ID  = ' '
    *     I_GRID_TITLE     =
    *     I_GRID_SETTINGS  =
    *     IS_LAYOUT        =
    *     IT_FIELDCAT      =
    *     IT_EXCLUDING     =
    *     IT_SPECIAL_GROUPS                 =
    *     IT_SORT          =
    *     IT_FILTER        =
    *     IS_SEL_HIDE      =
    *     I_DEFAULT        = 'X'
    *     I_SAVE           = ' '
    *     IS_VARIANT       =
    *     IT_EVENTS        =
    *     IT_EVENT_EXIT    =
    *     IS_PRINT         =
    *     IS_REPREP_ID     =
    *     I_SCREEN_START_COLUMN             = 0
    *     I_SCREEN_START_LINE               = 0
    *     I_SCREEN_END_COLUMN               = 0
    *     I_SCREEN_END_LINE                 = 0
    *     I_HTML_HEIGHT_TOP                 = 0
    *     I_HTML_HEIGHT_END                 = 0
    *     IT_ALV_GRAPHICS  =
    *     IT_HYPERLINK     =
    *     IT_ADD_FIELDCAT  =
    *     IT_EXCEPT_QINFO  =
    *     IR_SALV_FULLSCREEN_ADAPTER        =
    *     O_PREVIOUS_SRAL_HANDLER           =
    *   IMPORTING
    *     E_EXIT_CAUSED_BY_CALLER           =
    *     ES_EXIT_CAUSED_BY_USER            =
        TABLES
          t_outtab         = gt_school
        EXCEPTIONS
          program_error    = 1
          OTHERS           = 2.
      IF sy-subrc <> 0.
    * Implement suitable error handling here
      ENDIF.
    
    ENDFORM.
    
    ALV9.PNG alv1.PNG alv2.PNG alv3.PNG ALV4.PNG ALV5.PNG alv6.PNG alv7.PNG alv8.PNG

    相关文章

      网友评论

          本文标题:grid_alv

          本文链接:https://www.haomeiwen.com/subject/yngjvdtx.html