Skip to Content
author's profile photo Former Member
Former Member

Mixing ABAP and Javascript... help!!!

I want to take an ABAP internal table and use it in Javascript commands. But I can't do it, of course. So my next shot is to convert the table into a Javascript Array. The correct syntax is:

<script type="text/javascript"> var Jscripts = new Array("X","Y","Z") </script> Now, how to get my internal table involved? My approach is to simulate the var statement with a mixture of constants and ABAP code and ABAP variables. I want to comma delimit except at the end, when I terminate with a parenthesis. Let's say it's a table of strings... I tried the following (assume data is already in internal table): <% data: scripts type table of string, script type string, myindex1 type i. %> <script type="text/javascript"> var Jscripts = new Array( <% describe table scripts lines myindex1. loop at scripts into script. if sy-tabix < myindex1. %> "<%=script%>", <% else. %> "<%=script%>") <% endif. endloop. %> </script>

This doesn't work. The array apppears to contain 2 entries with weird values. The ABAP processor does not seem to be recognized, no looping is taking place! Can anyone suggest a way to do this?

Add comment
10|10000 characters needed characters exceeded

1 Answer

  • Posted on Dec 14, 2003 at 12:32 PM

    Hallo Paul,

    Looking at the code, I see no obvious immediately problem. You write "appears two weird values" and then "no looping". This problem description is definitely not so good that one can give you the correct answer. So let me just shoot around in the dark. Maybe we find something in a hole.

    What I suspect is that you should check for, and be careful of the use of "-character. Sometimes it happens to be used inside , class CL_HTMLB_DATACONTAINER, example page sbspext_htmlb/dropdownlistbox.bsp. This tag can render either an array of values, or if keyfields are given, an object of name/value pairs. If you don't feel like using the tag directly, copy the source code from DO_AT_BEGINNING. It should do all the tricks you require.

    brian

    Add comment
    10|10000 characters needed characters exceeded

    • Hallo Paul,

      Just a few comments, not really directly relevant, but interesting.

      Refresh: Maybe you set caching for the page, so that the page is either cached in browser and/or in server. This could explain code changes not becoming visible.

      Determining lines in a table: I forgot last time to tell you that there is a very nice operator LINES( itab ), which gives you the number of lines in a table. So you can write "table_cnt = LINES( scripts )". Same coding, much easier to read. This operator can also be directly used in IF statements. For example, "IF sy-tabix < LINES( itab ).". Keep performance in mind when in loop. Similar, there is STRLEN( s ) operator!

      ','-Handling: You keep testing for the last entry, if not, add the ',' into the script. Much easier to test if not first entry. Then you don't have to calculate LINES( itab) either. Something such as IF sy-tabix>1. ..add ',' before next line... ENDIF. See my previous append.

      But otherwise, what works, works!

      brian