Get property by variable name

I want to display a table using emberjs templates. The table should allow dynamic columns:

<table>
    {{#each item in this.items}}
        <tr>
            {{#each colName in this.columnNames}}
                <td>{{item.[colName]}}</td>
            {{/each}}
        </tr>
    {{/each}}
</table>

However, I believe that handlebars is trying to access the property of the colNameelement. How can I access a property dynamically?

+4
source share
1 answer

UPDATE: As Visualize noted in the comments, a native gethelper now exists . You should prefer this in accordance with the implementation below (which may not update correctly with new versions of Ember.)


, Handlebars ​​ ( , ). , , .

Ember.Handlebars.helper('getProperty', function(object, property, options) {
    return Ember.get(object, property);
});

:

{{#each item in items}}
    <tr>
    {{#each colName in columnNames}}
        <td>{{getProperty item colName}}</td>
    {{/each}}
    </tr>
{{/each}}

Ember , item colName.

+2

Source: https://habr.com/ru/post/1528990/


All Articles