5

I have a component which render elements. The width of each element depends on minimized value (true or false). I need conditional style binding without class here.

I tried:

{ conversation.minimized == false ? right: $index * 285 + 'px' : right: $index * 150 + 'px' }

but it didn't work and i got errors.

2
  • 1
    Should be v-bind:style="{ right: conversation.minimized ? $index * 150 + 'px' : $index * 285 + 'px' }" Commented Feb 8, 2016 at 14:28
  • I did it different way. Commented Feb 8, 2016 at 16:06

4 Answers 4

5

I don't really get your code but if you want conditional style binding you can do like that:

<template>
    <div
        :style="condition ? { 'color': 'red' } : { 'color': 'blue' }"
    >
        Some data
    </div>
</template>

<script>
export default {
    data() {
        return {
            condition: false
        }
    }
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

3

I'm not sure what you're trying to do. but you want to make a div smaller or bigger? If so, you can use a computed var to evaluate that value. And because it's reactive, if you change any of those values it will evaluate the value on the fly.

a small example:

<style>
  .classA {float:right,width:200px}
  .classB {float:right,width:400px}
</style>

on your HTML

<div id="#app">
<div class="{{ classChanger }}">your stuff</div>
</div>

and on your Vue

computed:{
    classChanger: function(){
        var theClass = 'classB';
        if(this.conversation.minimized == false){
            theClass = 'classA';
        }
        return theClass:
    }
}

It harder without knowing your code exactly.

5 Comments

Exactly, I want to make div smaller or bigger depends of its minimized or not. Could you show me an example how that should look?
updated my answer with an example without knowing the code is more dificult
That wasn't what I actually needed. I posted my working code so you can check it out. Thanks for advice anyway!
Use :class="classChanger" and not class="{{ classChanger }}" to work with current Vue
You can simplify that computed into a one liner... return !this.conversation.minimized ? 'classA' : 'classB';
2
currentItemPosition: function(index, conversation) {
            var indexOfCurrentElement = this.conversations.indexOf(conversation);  
            var right = 0;

            if (indexOfCurrentElement > 0) {
                for (var i=0; i<indexOfCurrentElement; i++) {
                    if (!this.conversations[i].minimized) {
                        right += 285;
                    } else {
                        right += 170;
                    }
                }
                return "right: " + right + "px";

            } else {
                return "right: 0px";
            } 
        }
    }

That's how i solved my problem.

<div class="card" v-bind:style="currentItemPosition($index, conversation)">

Works perfectly for me right now. There was to much logic anyway to do it in line with <div> tag.

Comments

0

To make things more modular, we could create a component that would wrap the logic for the div and the styles. The usage would be:

<card :index="$index" :minimized="conversation.minimized"></card>

And the definition of the component is:

Vue.component( 'card', {
    props: ['index', 'minimized'],
    template: '\
        <div class="card" :style=[cardStyles]>\
    ',
    computed: function () {
        cardStyles: function () {
            var ratio = this.minimized ? 150 : 285;
            return {
                right: ( this.index * ratio ) + 'px'
            }
        }
    }
} );

It receives the index and the minified value, and then uses a computed function to obtain the div styles.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.