- 程序名:ZABAP1903_07screen_selection
- 主界面
*&---------------------------------------------------------------------*
*& Report ZABAP1903_07SCREEN_SELECTION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZABAP1903_07SCREEN_SELECTION NO STANDARD PAGE HEADING
LINE-COUNT 8
LINE-SIZE 90.
INCLUDE ZHQ_01_02TOP. "top include
INCLUDE ZHQ_01_02FORM. "form include
*&---------------------------------------------------------------------*
*& EVENT 初始化界面Initialization
*&---------------------------------------------------------------------*
INITIALIZATION.
GV_NAME = '待检查…'. "zcode后面提示的内容添加初始值
*&---------------------------------------------------------------------*
*& EVENT SELECTION SCREEN EVENT
*&---------------------------------------------------------------------*
* PBO,PAI 是屏幕中的两个模块
*PBO是在屏幕事件发生前处理的逻辑 PAI就是在屏幕事件发生后处理的逻辑
AT SELECTION-SCREEN OUTPUT. "PBO = Process before output 选择界面的pbo事件
PERFORM MODIFIED_SCREEN.
AT SELECTION-SCREEN . "PAI = Process after input 选择界面的pai事件
PERFORM SELECTION_SCREEN_PAI.
*&---------------------------------------------------------------------*
*& event start of selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
"屏幕代码一般不能使用代码块
PERFORM GET_DATA.
*&---------- -----------------------------------------------------------*
*& event end of selection
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*定义完之后在程序中使用 菜单,执行见效果
SET PF-STATUS 'STATUS' EXCLUDING GT_STATUS. "按钮的创建
SET TITLEBAR 'TITLE' WITH '学生' '如下:'. "修改程序描述,将参数‘学生’传给title里面的变量&1 ,各个参数用空格隔开
* PERFORM DISPLAY_DATA.
IF GT_TOTLE IS NOT INITIAL.
PERFORM DISPLAY_DATA.
ELSE.
MESSAGE S001(PD) DISPLAY LIKE 'E'.
ENDIF.
*&---------- -----------------------------------------------------------*
*& event top of page
*&---------------------------------------------------------------------*
TOP-OF-PAGE. "给report添加后缀LINE-COUNT 10之后,给每个模块都填表头的方法。
PERFORM TOP-OF-PAGE.
*&---------- -----------------------------------------------------------*
*& event line selection
*&---------------------------------------------------------------------*
AT LINE-SELECTION. "双击事件
PERFORM LINE_SELECT.
*&---------- -----------------------------------------------------------*
*& event top of page of line selection
*&---------------------------------------------------------------------*
*初级表单:TOP-OF-PAGE
*次级表单:TOP-OF-PAGE DURING LINE-SELECTION
TOP-OF-PAGE DURING LINE-SELECTION.
CASE SY-UCOMM.
WHEN 'SORTUP' OR 'SORTDOWN'.
PERFORM TOP-OF-PAGE.
WHEN OTHERS.
FORMAT COLOR 3 ON. "背景颜色: 给表头添加颜色 取值范围 1~7
WRITE:/ '学校信息'.
FORMAT COLOR 2 OFF.
ENDCASE.
*&---------- -----------------------------------------------------------*
*& event user ccommand 按钮功能实现
*&---------------------------------------------------------------------*
AT USER-COMMAND.
PERFORM USER_COMMAND.
TABLES: ZSTUDENT_LPJ_01,ZSCHOOL_LPJ_01.
TYPES: BEGIN OF GTY_TOTLE,
ZCODE TYPE ZSTUDENT_LPJ_01-ZCODE,
ZNAME TYPE ZSTUDENT_LPJ_01-ZNAME,
SEX TYPE ZSTUDENT_LPJ_01-SEX,
ZSCHOOL TYPE ZSTUDENT_LPJ_01-ZSCHOOL,
ZSNAME TYPE ZSCHOOL_LPJ_01-ZSNAME,
END OF GTY_TOTLE.
"合计数据
DATA: GT_TOTLE TYPE TABLE OF GTY_TOTLE,
GS_TOTLE TYPE GTY_TOTLE,
"学校表
GT_SCHOOL TYPE TABLE OF ZSCHOOL_LPJ_01,
GS_SCHOOL TYPE ZSCHOOL_LPJ_01,
"学生表
GT_STUDENT TYPE TABLE OF ZSTUDENT_LPJ_01,
GS_STUDENT TYPE ZSTUDENT_LPJ_01.
*隐藏按钮
DATA: GT_STATUS LIKE TABLE OF SY-UCOMM WITH HEADER LINE. "声明内表 SY-UCOMM是一个对象,用like
*&---------------------------------------------------------------------*
*& 选择界面
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK BK1 WITH FRAME TITLE TEXT-T01. "必须是text 后面必须是三位
*单选框 p_zcode:变量; OBLIGATORY: 将单选框设置为必输项
*PARAMETERS: p_zcode LIKE gs_student-zcode OBLIGATORY DEFAULT '123'. "参照结构体里面的字段
"将单选框设置成双选框(用双选框的SELECT-OPTIONS…for…),NO-EXTENSION:再隐藏双选框后面的按钮;no INTERVALS:隐藏按钮前面的框
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (10) TEXT-T06 FOR FIELD P_ZCODE. "加了FOR FIELD commment 和 p_zcode 就会有下划线连接起来。
SELECTION-SCREEN: POSITION 30.
SELECT-OPTIONS: P_ZCODE FOR GS_STUDENT-ZCODE NO-EXTENSION NO INTERVALS.
SELECTION-SCREEN: POSITION 51.
SELECTION-SCREEN: COMMENT (10) GV_NAME. "输入p_zcode后,在后面显示内容
SELECTION-SCREEN: END OF LINE.
*复选框 s_zname:内表 select-options的四个字段:sign,option,low,high
SELECT-OPTIONS: S_ZNAME FOR GS_STUDENT-ZNAME.
*select-options: s_zname for gs_student-zname default 'abc' to 'def' sign e option bt. "DEFAULT:默认值 ,从'abc'到'def',SIGN E:不包含,OPTION BT:前后栏的关系是范围
SELECT-OPTIONS: S_ZSCHL FOR GS_SCHOOL-ZSCHOOL.
SELECTION-SCREEN END OF BLOCK BK1.
SELECTION-SCREEN SKIP 2. "上下块之间的距离
SELECTION-SCREEN BEGIN OF BLOCK BK2 WITH FRAME TITLE TEXT-T02.
*SELECTION-SCREEN BEGIN OF LINE. "行 加行之后,只有按钮,没有文本,所以需要重新加文本
*RADIOBUTTON GROUP:默认char类型,长度为1,选中为X,不选为空
PARAMETERS: R_MALE RADIOBUTTON GROUP RG01. "单选框 男
*SELECTION-SCREEN COMMENT (10) text-t04 FOR FIELD r_male. "重新加文本 ,comment 与长度之间必须有空格
*SELECTION-SCREEN POSITION 20. "两个按钮之间的距离,是从最左边开始的20个长度是第二个按钮
PARAMETERS: R_FEMALE RADIOBUTTON GROUP RG01. "单选框 女
*SELECTION-SCREEN COMMENT (10) text-t05 FOR FIELD r_female. "重新加文本
*SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK BK2.
SELECTION-SCREEN SKIP 2. "上下块之间的距离
"char 1,选中为X,不选为空 。 command:命令,指挥,指令
PARAMETERS P_CHECK AS CHECKBOX USER-COMMAND HIDE.
" hide:function code(功能代码) 的名称(自己取名),实现什么样的功能,用名称进一步去实现。此处是实现隐藏,选中复选框不需要回车就可以隐藏/显示。
*将复选框变成复选功能,带有功能的按钮
SELECTION-SCREEN ULINE. "横线
SELECTION-SCREEN BEGIN OF BLOCK BK3 WITH FRAME TITLE TEXT-T03.
PARAMETERS: SD RADIOBUTTON GROUP GR02 MODIF ID DEP,
MM RADIOBUTTON GROUP GR02 MODIF ID DEP,
PP RADIOBUTTON GROUP GR02 MODIF ID DEP,
FI RADIOBUTTON GROUP GR02 MODIF ID DEP,
CO RADIOBUTTON GROUP GR02 MODIF ID DEP.
SELECTION-SCREEN END OF BLOCK BK3.
*&---------------------------------------------------------------------*
*& Include ZHQ_01_02FORM
*&---------------------------------------------------------------------*
"一小时零九分钟
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& 取数及输出
*&---------------------------------------------------------------------*
FORM GET_DATA.
RANGES R_SEX FOR GS_STUDENT-SEX. "ranges跟select-option的结构一样,但是在界面不产生控件,只是一个单独的内表
*根据选择界面给range赋值 select-options的四个字段:sign,option,low,high
CLEAR: R_SEX,R_SEX[].
IF R_MALE = 'X'. "选择性别为男
R_SEX-SIGN = 'I'. " I 代表include ; E代表的是exclude。
R_SEX-OPTION = 'EQ'.
R_SEX-LOW = 'M'.
APPEND R_SEX.
ELSE.
R_SEX-SIGN = 'I'. "选择性别为女 elseif R_FEMALE = 'X'.
R_SEX-OPTION = 'EQ'.
R_SEX-LOW = 'F'.
APPEND R_SEX.
ENDIF.
* select多条取数——>into table 多表取数据
* 多表取数:1. inner join ;2. left outer join;3. 多表sselect
* 1. inner join
* SELECT
* A~ZCODE A~ZNAME A~SEX B~ZSCHOOL B~ZSNAME
* FROM
* ZSTUDENT_LPJ_01 AS A INNER JOIN ZSCHOOL_LPJ_01 AS B
* ON A~ZSCHOOL = B~ZSCHOOL "join 的条件用ON,连接两张表的条件(外键)
* INTO CORRESPONDING FIELDS OF TABLE GT_TOTLE
* WHERE
* A~ZSCHOOL IN S_ZSCHL AND A~ZCODE IN P_ZCODE AND A~ZNAME IN S_ZNAME.
*2. LEFT OUTER JOIN :左外联合,左外连接;
*SELECT
* A~ZCODE A~ZNAME A~SEX B~ZSCHOOL B~ZSNAME
* FROM
* ZSTUDENT_LPJ_01 AS A LEFT OUTER JOIN ZSCHOOL_LPJ_01 AS B
* ON A~ZSCHOOL = B~ZSCHOOL "ON 条件下必须包含右表 where 条件下必须不包含右表
* INTO CORRESPONDING FIELDS OF TABLE GT_TOTLE
* WHERE "where 条件下必须不包含右表
* A~ZSCHOOL IN S_ZSCHL AND A~ZCODE IN P_ZCODE AND A~ZNAME IN S_ZNAME.
*3. 多表select
SELECT ZCODE ZNAME SEX ZSCHOOL
FROM ZSTUDENT_LPJ_01
INTO CORRESPONDING FIELDS OF TABLE GT_STUDENT
WHERE ZCODE IN P_ZCODE
AND ZNAME IN S_ZNAME
AND ZSCHOOL IN S_ZSCHL
AND SEX IN R_SEX.
* FOR ALL ENTRIES IN 只要有这个语句,必须检查(下面if语句) : 条件一
IF GT_STUDENT IS NOT INITIAL. "如果gt_student中有数据,才会执行下面语句 ,下面一条注释
* range表限制学校代码的取数()
* LOOP AT gt_student INTO gs_student.
* CLEAR r_school.
* r_school-sign = 'I'.
* R_SCHOOL-OPTION = 'EQ'.
* R_SCHOOL-LOW = GS_STUDENT-ZSCHOOL. "RANGE 只能用一个字段来赋值,联合字段不行(一个外键)
* APPEND R_SCHOOL.
* ENDLOOP.
SELECT ZSCHOOL ZSNAME
FROM ZSCHOOL_LPJ_01
INTO CORRESPONDING FIELDS OF TABLE GT_SCHOOL
FOR ALL ENTRIES IN GT_STUDENT "FOR ALL ENTRIES IN: 所有项目 如果gt_school内表中没有数据时,将会取数据库中的所有数据
WHERE ZSCHOOL = GT_STUDENT-ZSCHOOL . " 条件二: 两个表的关联字段(类型,长度必须相同) 基本限制
SORT GT_SCHOOL BY ZSCHOOL. "*使用二分法搜索,先对zschool进行排序 从那个表读的哪个字段,就对哪个表的哪个字段进行排序
ENDIF.
LOOP AT GT_STUDENT INTO GS_STUDENT.
CLEAR GS_TOTLE. "先清空
MOVE-CORRESPONDING GS_STUDENT TO GS_TOTLE.
READ TABLE GT_SCHOOL INTO GS_SCHOOL WITH KEY ZSCHOOL = GS_TOTLE-ZSCHOOL BINARY SEARCH. "BINARY SEARCH: 二分查找 read中使用二分法查找很快
IF SY-SUBRC = 0. "读取到
GS_TOTLE-ZSNAME = GS_SCHOOL-ZSNAME.
ELSE. "读取失败
CONTINUE. "终止当前循环并执行下一次循环
ENDIF.
APPEND GS_TOTLE TO GT_TOTLE. "把totle里面的数据放回内表
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DISPLAY_DATA .
* WRITE:/1(56) SY-ULINE. "添加表头
* WRITE:/1 SY-VLINE NO-GAP, (10) '学号' NO-GAP CENTERED, "centered居中
* SY-VLINE NO-GAP, (10) '姓名' NO-GAP CENTERED,
* SY-VLINE NO-GAP, (10) '性别' NO-GAP CENTERED,
* SY-VLINE NO-GAP, (10) '学校' NO-GAP CENTERED,
* SY-VLINE NO-GAP, (10) '校名' NO-GAP CENTERED,
* SY-VLINE.
* WRITE:/1(56) SY-ULINE. "循环外画一条横线,从第一列开始画,并制定其长度
LOOP AT GT_TOTLE INTO GS_TOTLE.
* 横线:sy-uline 竖线:sy-vline,.。.
WRITE:/1 SY-VLINE NO-GAP, (10) GS_TOTLE-ZCODE NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) GS_TOTLE-ZNAME NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) GS_TOTLE-SEX NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) GS_TOTLE-ZSCHOOL NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) GS_TOTLE-ZSNAME NO-GAP CENTERED,
SY-VLINE.
WRITE:/1(56) SY-ULINE. "每一条数据循环完画一条横线
ENDLOOP.
ENDFORM. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form MODIFIED_SCREEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM MODIFIED_SCREEN.
LOOP AT SCREEN.
IF P_CHECK = 'X'.
"显示
ELSE.
"隐藏
* IF screen-name = 'SD' or "通过变量名来隐藏多个 "注意大小写,小写在屏幕上不识别,会自动转换成大写
* screen-name = 'PP' or
* screen-name = 'MM'.
IF SCREEN-GROUP1 = 'DEP'. "通过用 (MODIF ID 组名)定义组来实现的
SCREEN-INVISIBLE = 1. "1:true 0:false
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM. " MODIFIED_SCREEN
*&---------------------------------------------------------------------*
*& Form SELECTION_SCREEN_PAI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM SELECTION_SCREEN_PAI.
SELECT SINGLE *
FROM ZSTUDENT_LPJ_01
INTO CORRESPONDING FIELDS OF GS_STUDENT "当是结构体的时候不需要table关键字,是表的时候需要。对比99行和21行
WHERE ZCODE IN P_ZCODE.
IF SY-SUBRC = 0.
GV_NAME = '存在'.
MESSAGE S000(HQ) WITH '该学号存在'.
ELSE.
GV_NAME = '不存在'.
MESSAGE S000(HQ) WITH '该学号不存在' DISPLAY LIKE 'E'.
ENDIF.
ENDFORM. " SELECTION_SCREEN_PAI
*&---------------------------------------------------------------------*
*& Form TOP-OF-PAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM TOP-OF-PAGE .
FORMAT COLOR 2 ON. "背景颜色: 给表头添加颜色 取值范围 1~7
WRITE:/1(56) '学生信息表' CENTERED.
FORMAT COLOR 2 OFF.
WRITE:/1(56) SY-ULINE. "添加表头
FORMAT COLOR 6 INVERSE ON. "INVERSE:字体颜色
WRITE:/1 SY-VLINE NO-GAP, (10) '学号' NO-GAP CENTERED, "centered居中
SY-VLINE NO-GAP, (10) '姓名' NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) '性别' NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) '学校' NO-GAP CENTERED,
SY-VLINE NO-GAP, (10) '校名' NO-GAP CENTERED,
SY-VLINE.
WRITE:/1(56) SY-ULINE.
FORMAT COLOR 6 INVERSE OFF.
ENDFORM. " TOP-OF-PAGE
*&---------------------------------------------------------------------*
*& Form LINE_SELECT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM LINE_SELECT .
DATA: LV_FIELD(30),
LV_VALUE(30).
*获取光标所在的位置
GET CURSOR FIELD LV_FIELD "字段名 gs_totle-zschool
VALUE LV_VALUE. "字段值
"判断是否是学校ID字段
CHECK LV_FIELD = 'GS_TOTLE-ZSCHOOL'. "check 注意字符串里面的大小写
*从学校表取数
SELECT SINGLE *
FROM ZSCHOOL_LPJ_01
* into…… "省略此语句,默认取到tables语句的结构体中
WHERE ZSCHOOL = LV_VALUE.
"取数成功与否
IF SY-SUBRC = 0. "成功
*显示学校的详细信息
WRITE:/ '学校编号:',ZSCHOOL_LPJ_01-ZSCHOOL,
/ '学校名称:',ZSCHOOL_LPJ_01-ZSNAME,
/ '学校地址:',ZSCHOOL_LPJ_01-ZADD.
ELSE. "不成功
* 消息提示
MESSAGE I000(HQ) WITH '不存在该学校的信息'.
ENDIF.
ENDFORM. " LINE_SELECT
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM USER_COMMAND .
*判断function code 确定点击的按钮
CASE SY-UCOMM. "SY-UCOMM:记录操作
WHEN 'BACK'.
LEAVE TO SCREEN 0. "0 代表上一个
WHEN 'EXIT'OR 'CANCEL'.
LEAVE PROGRAM. "退出程序
WHEN 'SORTUP'.
SY-LSIND = SY-LSIND - 1. "每进行一次排序之后的操作不进行累加(默认累加),后退一步,就可以直接退出执行界面,一般只用于排序。
* PERFORM SORT_UP.
* 下面三行代码是拖进来的拖的子程序是SORT_LIST
PERFORM SORT_LIST
USING
'UP'. "将P_FLAG参数的值赋为up.up是实参,传递给形参P_flag
WHEN 'SORTDOWN'.
SY-LSIND = SY-LSIND - 1.
PERFORM SORT_LIST
USING
'DOWN'. "将P_FLAG的值赋为DOWN.
* PERFORM SORT_DOWN.
WHEN 'DOWNLOAD'.
MESSAGE S000(HQ) WITH '下载成功! '.
WHEN OTHERS.
* ……
ENDCASE.
ENDFORM. " USER_COMMAND
*&---------------------------------------------------------------------*
*& Form SORT_LIST
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p_FLAG text
*----------------------------------------------------------------------*
*调用SORT_LIST子程序,用P_FLAG参数进行传递
FORM SORT_LIST USING P_FLAG. "P_FLAG:形参
DATA: LV_FIELD(30),
LV_FIELDNAME(30).
*获取光标
GET CURSOR FIELD LV_FIELD. "GS_TOTLE-zcode
*排序
CHECK LV_FIELD+0(8) = 'GS_TOTLE'.
* SPLIT LV_FIELD AT '-' INTO LV_FIELD LV_FIELDNAME.
LV_FIELDNAME = LV_FIELD+9(*). "获取点击的字段名
IF P_FLAG = 'UP'.
SORT GT_TOTLE BY (LV_FIELDNAME) ASCENDING.
ELSEIF P_FLAG = 'DOWN'.
SORT GT_TOTLE BY (LV_FIELDNAME) DESCENDING. "加括号表示取此变量的值 当通过zcode字段进行排序的时候,就将zcode赋给变量即可
ENDIF.
*输出
PERFORM DISPLAY_DATA.
ENDFORM. "SORT_LIST
网友评论