0

I need to change the display of a input depending on the value returned from the db for this variable: paragraph.RCBulletinNumber

I have tried the computed method, but I think this is not what I need

     computed: {

    formatRCBulletinNumber: {

        set (val){
            return this.paragraph.RCBulletinNumber;
        }

    }

},

This is my input declaration using v-model='paragraph.RCBulletinNumber':

    <div class="form-group">
        <div v-if='typeof paragraph != undefined /*/<!--&& parseInt(paragraph.RCBulletinNumber)>0 -->/*/'>
            <input type="text" style="width: 40%;" class='form-control' id="RCNumber" placeholder="RC Number" v-model='paragraph.RCBulletinNumber'>
                </div>
                    </div>

What I expect is that if the value of paragraph.RCBulletinNumber is less than or equal to 0 the input remains empty. Right now if the variable equals 0 the input is 0

But if the paragraph.RCBulletinNumber is equal to 0, the value must go to the database again, my goal is just to change the value of the input to be more userfriendly.

2
  • What are you trying to do with the set() function?? Commented Jun 18, 2019 at 1:50
  • for example, If this were pure js, I would just do [ if(myvalue<=0) {$("id").val("");} else{$("id").val(myvalue);} What I need is to have the variable paragraph.RCBulletinNumber without any modification, but if its input is 0 or less leave it empty (Just the frontend, the input, without touching the variable). Commented Jun 18, 2019 at 1:53

1 Answer 1

2

Simply define the getter and setter of the computed property:

computed: {
  formatRCBulletinNumber: {
    // getter
    get: function () {
      return this.paragraph.RCBulletinNumber || '';
    },
    // setter
    set: function (newValue) {
      this.paragraph.RCBulletinNumber = newValue;
    }
  }
}

And then reference the computed property on the input:

<input type="text" style="width: 40%;" class='form-control' id="RCNumber" placeholder="RC Number" v-model='formatRCBulletinNumber'>

reference: https://v2.vuejs.org/v2/guide/computed.html#Computed-Setter

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.