I am trying to "sort" hierarchically the options of a select using a bootstrap plugin that needs to identify the "order" of each item.
In view of this, I performed a database search to bring only the order of each of the items listed in select, put them inside an array set in twig and am trying to set each value of that array for each of the options.
Note: I am trying to use this plugin https://neofusion.github.io/hierarchy-select/index.html#
{% set ordem = [] %}
{% for item in cPublicaItemEstruturas %}
{% if item not in ordem %}
{% set ordem = ordem|merge([item.ordem]) %}
{% endif %}
{% endfor %}
{% for i in ordem %}
{{ i }}<!--Results in 1 1 1 1 2 3 2 3 1 2 3 1 2 1 2 3 4 1-->
<script>
$('#c_publica_item_estrutura_itemPai option').attr('data-level', '{{ i }}');
</script>
{% endfor %}
I expected something like this:
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 1));
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 2));
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 3));
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 2));
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 3));
console.log($('#c_publica_item_estrutura_itemPai option').attr('data-level', 1));
But they all come out with only the first array index that is the 1.