1

In the Globals I have a field which is of type table. I was trying to access this table in the javascript in order to get the values of particular rows.

<script>
        var myTable = "{{ craft.globals.getSetByHandle('myGlobal').myTable }}"; 
</script>

It returns a string "Array". If I put [] around the expression, it will return ["Array"].

Do you know a way to get access to this table?

1

If you want to get the values of your global table field, you need to loop through it.

somewhere in a template:

{% js %}
    var table = '
    {% for row in global.tableFieldName %}
      ({{ row.column1 }} - {{ row.column2 }}){% if not loop.last %},{% endif %} 
    {% endfor %}'
{% endjs %}

Output:

<script>var table = '(Value 1-1 - Value 1-2),(Value 2-1 - Value 2-2)';</script>

You should check out the documentation about Craft 3 Table Fields, {% js %} Tag and for removing the trailing comma Remove trailing comma from an output loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.