cancel
Showing results for 
Search instead for 
Did you mean: 

Mixing ABAP and Javascript... help!!!

Former Member
0 Kudos

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?

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member181879
Active Contributor
0 Kudos

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

Former Member
0 Kudos

I think one problem was that I was not reloading my browser each time. I hit "Refresh" but perhaps this does not really work, and what I saw on the screen was not my code. In the end I used a different approach... The following code works OK:

<% data: script type string,               jscripts type table of string,               table_cnt type i,               jscript type string.         jscript = 'var Jscripts = new Array(" ",'.         describe table scripts lines table_cnt.         loop at scripts into script.           concatenate jscript '"' script '"' into jscript.           if sy-tabix < table_cnt.             concatenate jscript ',' into jscript.           else.             concatenate jscript ');' into jscript.           endif.            append jscript to jscripts.            clear jscript.          endloop. %> and later on... <script> <%  loop at jscripts into jscript. %>               <%=jscript%>             <% endloop. %> </script>

former_member181879
Active Contributor
0 Kudos

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