1

I'm trying to parameterise variable names so they can be passed as properties into my component. In the example below I want to use this to pass the names of the item variables into the array so I can selectively display them as columns in a table without the binding having to know the item variable names.

<div id="myApp">
   <h2>parameterized variable names</h2>
    <table>
      <tr>
        <th v-for="label in labels">{{label}}</th>
      </tr>  
      <tr v-for="item in items">
         <td v-for="label in labels">{{item.label}}</td>
      </tr>  
    </table>  
 </div>

My Vue instance looks like this -

new Vue({
  el: '#myApp',
  data: {
      labels:[
        'text', 
        'value'
      ],
      items:[ 
        {text: 'One', value: 'A', something:'12'},
        {text: 'Two', value: 'B', something:'67'},
        {text: 'Three', value: 'C', something:'66'}  
      ]  

  }
});

This doesn't work because it is attempting to render a variable called 'label' in the declaration {{item.label}}. How can I tell it that 'label' is not the literal variable name?

1 Answer 1

1

You can use array syntax in Vue.js templates, so the following should work:

<td v-for="label in labels">{{ item[label] }}</td>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.