1

My object returned is a list like this:

data = [{'phone': '321-321-321',
          'email': 'test@test.com'
         },
        {'phone' : '123-123-123', 
         'email': 'bc@bcc.com'
        }]

I need a custom variable to be used in Javascript that looks like this (in fact a localstorage variable): //Javascript variable needs to iterate over the data and create a comma seperated list:

//var emails = 'test@test.com, bc@bcc.com' 

Is there any way of doing this efficiently?

Loser Coder
  • 2,338
  • 8
  • 42
  • 66
  • You need to do it on server side or on client side? It's not clear from your question. If `data` is a `javascript` variable you can do it like [in this issue](http://stackoverflow.com/questions/12043865/join-json-object-member-string-values-together). Otherwise you can do the same operation on server side, then send it to `Thymeleaf`. – sanluck Apr 13 '16 at 03:42
  • Client side in this case. Though the ideal solution would be server side, but that is not feasible at the moment – Loser Coder Apr 13 '16 at 04:43

2 Answers2

0

I'm not sure if I understood correctly, but you can assign Spring object to javascript variable like this:

<script th:inline="javascript">
/*<![CDATA[*/
...

var username = /*[[${session.user.name}]]*/ 'Sebastian';

...
/*]]>*/
</script>

documentation

Iwo Kucharski
  • 3,735
  • 3
  • 50
  • 66
0

You can store data in dom using

<div style="display: none" id="data" th:data-form-data="${formData}"></div>
//Access using document.querySelector("#id").dataset
Imamudin Naseem
  • 1,604
  • 18
  • 21