0

I want to access an array of data from my twig in my javascript but it never gets the full array.

I have a controller that returns an array of objects that I'm using in my twig, here is an example of the objects stored :
enter image description here

Now, I want to access it in my javascript, I tried a lot of things but ended up with using data in my div : (following that https://symfony.com/doc/current/frontend/encore/server-data.html?fbclid=IwAR2BHWMl-OJSytkfbNgaNUFRefC85bTPw63ymvuC9Q4zPlKxZZoKZ7dvH9E)

<div class="table_congesService" data-conges-service="{{congesService|json_encode|e('html_attr') }}">

And

<script>
  document.addEventListener('DOMContentLoaded', function() {
    var tableCongesService = document.querySelector('.table_congesService');
    var listCongesService = tableCongesService.dataset.congesService;
    console.log(listCongesService);
  });
</script>

However, instead of having the full structure, I only have the id when I'm displaying the result with the console.log: enter image description here What am I missing? I'm new to web and can't find what is wrong. Does it have something with the structure of my data before the encode? Or an option that I should use?

tereško
  • 58,060
  • 25
  • 98
  • 150
FloLp
  • 43
  • 5

1 Answers1

2

It looks like the properties of the objects are private, except id_conge. That is why json_encode encodes only the public property id_conge.

You can serialize an object with private properties by implementing the \JsonSerializable as described here or export the object as array with all the properties you need in JS.

Paweł Napierała
  • 1,631
  • 10
  • 15