太久不写这类的ALV了,关于金额指定币种,仅仅有个印象,实在是忘了。
所以做了个小测试:
先建了个表
然后用代码往里面插入了10条记录,5 usd 5 jpy
USD是两位小数点,JPY最小单位是元,没有小数点。存到库表的时候,存100,实际是10000。
*&---------------------------------------------------------------------*
*& Report ZPR_TEST_0001
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZPR_TEST_0001.
DATA:
LT_DATA TYPE TABLE OF ZTEST_0001,
LS_DATA TYPE ZTEST_0001.
DO 10 TIMES.
LS_DATA-ID = SY-INDEX.
LS_DATA-PRICE = 1000.
LS_DATA-CURRENCY = 'USD'.
IF LS_DATA-ID > 5.
LS_DATA-CURRENCY = 'JPY'. (100000日元,不是1000日元)
ENDIF.
APPEND LS_DATA TO LT_DATA.
ENDDO.
INSERT ZTEST_0001 FROM TABLE LT_DATA ACCEPTING DUPLICATE KEYS.
COMMIT WORK.
测试代码
REPORT ZPR_TEST_0001.
DATA:
LT_DATA TYPE TABLE OF ZTEST_0001,
LS_DATA TYPE ZTEST_0001.
DATA: LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
LS_FIELDCAT LIKE LINE OF LT_FIELDCAT,
LS_LAYOUT TYPE SLIS_LAYOUT_ALV.
START-OF-SELECTION.
SELECT * FROM ZTEST_0001 INTO TABLE LT_DATA .
PERFORM SUB_FIELDCAT_INIT CHANGING LT_FIELDCAT[].
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = LS_LAYOUT
IT_FIELDCAT = LT_FIELDCAT[]
TABLES
T_OUTTAB = LT_DATA
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
主要就是看这个form:
第一种方式:
FORM SUB_FIELDCAT_INIT USING RT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-REPID
I_STRUCTURE_NAME = 'ZTEST_0001'
I_CLIENT_NEVER_DISPLAY = 'X'
CHANGING
CT_FIELDCAT = RT_FIELDCAT.
ENDFORM.
没问题,debug看看
第二种情况
FORM SUB_FIELDCAT_INIT USING RT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
LS_FIELDCAT-COL_POS = 1.
LS_FIELDCAT-FIELDNAME = 'ID'. "field name
LS_FIELDCAT-SELTEXT_M = 'ID'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 2.
LS_FIELDCAT-FIELDNAME = 'PRICE'. "field name
LS_FIELDCAT-SELTEXT_M = 'PRICE'. "text
LS_FIELDCAT-REF_TABNAME = 'ZTEST_0001'. "
LS_FIELDCAT-REF_FIELDNAME = 'CURRENCY'. "
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 3.
LS_FIELDCAT-FIELDNAME = 'CURRENCY'. "field name
LS_FIELDCAT-SELTEXT_M = 'CURRENCY'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
ENDFORM. "fieldcat_init
不对
第三种情况
FORM SUB_FIELDCAT_INIT USING RT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
LS_FIELDCAT-COL_POS = 1.
LS_FIELDCAT-FIELDNAME = 'ID'. "field name
LS_FIELDCAT-SELTEXT_M = 'ID'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 2.
LS_FIELDCAT-FIELDNAME = 'PRICE'. "field name
LS_FIELDCAT-SELTEXT_M = 'PRICE'. "text
LS_FIELDCAT-REF_TABNAME = 'ZTEST_0001'. "
LS_FIELDCAT-CFIELDNAME = 'CURRENCY'. "
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 3.
LS_FIELDCAT-FIELDNAME = 'CURRENCY'. "field name
LS_FIELDCAT-SELTEXT_M = 'CURRENCY'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
ENDFORM. "fieldcat_init
对
第四种
FORM SUB_FIELDCAT_INIT USING RT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
LS_FIELDCAT-COL_POS = 1.
LS_FIELDCAT-FIELDNAME = 'ID'. "field name
LS_FIELDCAT-SELTEXT_M = 'ID'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 2.
LS_FIELDCAT-FIELDNAME = 'PRICE'. "field name
LS_FIELDCAT-SELTEXT_M = 'PRICE'. "text
LS_FIELDCAT-CFIELDNAME = 'CURRENCY'. "
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
LS_FIELDCAT-COL_POS = 3.
LS_FIELDCAT-FIELDNAME = 'CURRENCY'. "field name
LS_FIELDCAT-SELTEXT_M = 'CURRENCY'. "text
APPEND LS_FIELDCAT TO RT_FIELDCAT.
CLEAR LS_FIELDCAT.
ENDFORM. "fieldcat_init
好像主要就这一个字段起作用
然后我对这个字段不显示
也没问题。
如果不取值呢 ?
START-OF-SELECTION.
SELECT
ID
PRICE
*CURRENCY
FROM ZTEST_0001 INTO CORRESPONDING FIELDS OF TABLE LT_DATA .
PERFORM SUB_FIELDCAT_INIT CHANGING LT_FIELDCAT[].
显然是不行的
现在应该彻底清楚了。