3

I have a vue object with an array of items as data (in my case flightplans). The list is rendered correctly so far.

Now comes the problem. I want to apply different styles while iterating with v-for to each item (flightplan) in the list dependent of the value of current iterated flightplan memeber fplType. Currently all list items (flightplans) get the class flightplan-list-ifr-dep.

But i need something like (pseudo code):

<li v-for="flightplan in flightplans"
    v-bind:id="flightplan.id"                        
    v-bind:class="{
      flightplan-list-ifr-dep: flightplan.fplType === 'departure',
      flightplan-list-ifr-arr: flightplan.fplType === 'arrival'
    }"
>

So each items gets its own class applied dependent from the fplType of the current iterated flightplan.

<div id="flightplan-list-area" class="flightplan-list-area-style">
  <ul>
    <li v-for="flightplan in flightplans"
      v-bind:id="flightplan.id"
      @click="selected(flightplan, $event)">
      <div class="flightplan-list-ifr-dep">
          <p class="flightplan-list-ifr-dep-callsign">{{ flightplan.callsign }}</p>
          <p class="flightplan-list-ifr-dep-aircraft-type">{{ flightplan.aircraft_type }}</p>
          <p class="flightplan-list-ifr-dep-aircraft-wtc">{{ flightplan.aircraft_wtc }}</p>
      </div>
    </li>
   </ul>
</div>


<script lang="javascript"> 
var vue_FLIGHTPLAN_MODEL = new Vue({
  el: "#flightplan-list-area",

  data: {
    flightplans: [],
    selected_flightplan_element: null,
  },
});
</script>
3
  • The chapter about class and style bindings in the official guide covers this very well :) Commented Apr 25, 2018 at 5:39
  • I can not find the solution when data is an array and with iterating... Commented Apr 25, 2018 at 5:43
  • @NandoLambrusco if your problem is solved, please consider marking an answer as accepted Commented Apr 25, 2018 at 6:29

3 Answers 3

3

Your syntax is almost right, but since your class names contain the "-" character, you just need to quote them inside v-bind:class:

...
v-bind:class="{
    'flightplan-list-ifr-dep': flightplan.fplType === 'departure',
    'flightplan-list-ifr-arr': flightplan.fplType === 'arrival'
}"
...

Live demo

var vue_FLIGHTPLAN_MODEL = new Vue({
  el: "#flightplan-list-area",

  data: {
    flightplans: [
      { id: 1, callsign: 'ABC', aircraft_type: 'Boeing 737', aircraft_wtc: 'xyz', fplType: 'departure' },
      { id: 2, callsign: 'DEF', aircraft_type: 'Boeing 737', aircraft_wtc: 'uvw', fplType: 'arrival' },
      { id: 3, callsign: 'HIJ', aircraft_type: 'Boeing 737', aircraft_wtc: 'abc', fplType: 'departure' },
    ],
    selected_flightplan_element: null,
  },
  
  methods: {
    selected (plan, e) {
      this.selected_flightplan_element = plan;
    }
  },
});
li.flightplan-list-ifr-dep {
  color: blue;
}

li.flightplan-list-ifr-arr {
  color: green;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<div id="flightplan-list-area" class="flightplan-list-area-style">
  <ul>
    <li v-for="flightplan in flightplans"
                v-bind:id="flightplan.id"
                v-bind:class="{
                        'flightplan-list-ifr-dep': flightplan.fplType === 'departure',
                        'flightplan-list-ifr-arr': flightplan.fplType === 'arrival'
                    }"
                @click="selected(flightplan, $event)">
                <div class="flightplan-list-ifr-dep">
                    <p class="flightplan-list-ifr-dep-callsign">{{ flightplan.callsign }}</p>
                    <p class="flightplan-list-ifr-dep-aircraft-type">{{ flightplan.aircraft_type }}</p>
                    <p class="flightplan-list-ifr-dep-aircraft-wtc">{{ flightplan.aircraft_wtc }}</p>
                </div>
            </li>
   </ul>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can either use the object style syntax like so:

<li v-for="flightplan in flightplans">
  <div :class="{
    'fpl-foo': flightplan.fplType === 1,
    'fpl-bar': flightplan.fplType === 2,
    'fpl-baz': flightplan.fplType === 3
  }">
    <!-- ... -->
  </div>
</li>

...or use a method:

<li v-for="flightplan in flightplans">
  <div :class="getClass(flightplan.fplType)">
    <!-- ... -->
  </div>
</li>

methods: {
  getClass (fplType) {
    // Determine the class and return it,
    // this can also be an array in case
    // of more than one class
  }
}

2 Comments

when you call the getClass method,you pass fplType.Am i wrong or you have to pass flightplan.fplType?
Thank you Lars! That solves my problem perfectly! I am very new to vue.js so sorry for this question... Have a nice day
0

Your pseudocode is right.You can bind a class dynamically.See class and style Bindings

So the follow code will work for you:

<li v-for="flightplan in flightplans"
    :key="flightplan.id"
    v-bind:id="flightplan.id"                        
    v-bind:class="{
           'flightplan-list-ifr-dep': flightplan.fplType === 'departure',
           'flightplan-list-ifr-arr': flightplan.fplType === 'arrival'
          }"
 >

And when using v-for,it is recommended always to use a unique key.

I made a simplify example for you.See it in action

1 Comment

Yes this is true

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.