vue.js bidirectional calculation on input value
Is it possible to do bidirectional calculations on an input in vue.js? My data is in inches and has to stay in inches. I want to convert my data to other units in a input box and after the user put in some value it has to be converted back to inches.
<input :value="someCrazyBidirectionalCalculation(dataInInches)">
Follow up question:
I followed the guide to make a two-way filter in vue.js v2
<v-unit-input label="Height" v-model="parameters.height"></v-unit-input>
Vue.component('v-unit-input', {
template:
'<v-text-field ref="input" :label="label" type="number" suffix="mm" :value="value" @input="toInch($event.target.value)" @blur="toMm"></v-text-field>',
props: {
value: {
type: Number,
default: 0
},
label: {
type: String,
default: ''
}
},
mounted: function () {
this.toMm()
},
methods: {
toInch: function (value) {
this.$emit('input', this.value / 25.4)
},
toMm: function () {
this.$refs.input.value = this.value * 25.4
},
}
})
It does it job when sending data from the backend to the input but changing the input gives this error:
[Vue warn]: Error in event handler for "input": "TypeError: Cannot read property 'value' of undefined"
Edit 2:
It seems $event.target.value was the problem. My value is stored in $event.
Data from the backend is converted from inches to mm and when entering data in mm it is converted back to inches but it is also displayed in inches as soon as I press a number. Is it possible to only show it in mm?
vue.js
add a comment |
Is it possible to do bidirectional calculations on an input in vue.js? My data is in inches and has to stay in inches. I want to convert my data to other units in a input box and after the user put in some value it has to be converted back to inches.
<input :value="someCrazyBidirectionalCalculation(dataInInches)">
Follow up question:
I followed the guide to make a two-way filter in vue.js v2
<v-unit-input label="Height" v-model="parameters.height"></v-unit-input>
Vue.component('v-unit-input', {
template:
'<v-text-field ref="input" :label="label" type="number" suffix="mm" :value="value" @input="toInch($event.target.value)" @blur="toMm"></v-text-field>',
props: {
value: {
type: Number,
default: 0
},
label: {
type: String,
default: ''
}
},
mounted: function () {
this.toMm()
},
methods: {
toInch: function (value) {
this.$emit('input', this.value / 25.4)
},
toMm: function () {
this.$refs.input.value = this.value * 25.4
},
}
})
It does it job when sending data from the backend to the input but changing the input gives this error:
[Vue warn]: Error in event handler for "input": "TypeError: Cannot read property 'value' of undefined"
Edit 2:
It seems $event.target.value was the problem. My value is stored in $event.
Data from the backend is converted from inches to mm and when entering data in mm it is converted back to inches but it is also displayed in inches as soon as I press a number. Is it possible to only show it in mm?
vue.js
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
1
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15
add a comment |
Is it possible to do bidirectional calculations on an input in vue.js? My data is in inches and has to stay in inches. I want to convert my data to other units in a input box and after the user put in some value it has to be converted back to inches.
<input :value="someCrazyBidirectionalCalculation(dataInInches)">
Follow up question:
I followed the guide to make a two-way filter in vue.js v2
<v-unit-input label="Height" v-model="parameters.height"></v-unit-input>
Vue.component('v-unit-input', {
template:
'<v-text-field ref="input" :label="label" type="number" suffix="mm" :value="value" @input="toInch($event.target.value)" @blur="toMm"></v-text-field>',
props: {
value: {
type: Number,
default: 0
},
label: {
type: String,
default: ''
}
},
mounted: function () {
this.toMm()
},
methods: {
toInch: function (value) {
this.$emit('input', this.value / 25.4)
},
toMm: function () {
this.$refs.input.value = this.value * 25.4
},
}
})
It does it job when sending data from the backend to the input but changing the input gives this error:
[Vue warn]: Error in event handler for "input": "TypeError: Cannot read property 'value' of undefined"
Edit 2:
It seems $event.target.value was the problem. My value is stored in $event.
Data from the backend is converted from inches to mm and when entering data in mm it is converted back to inches but it is also displayed in inches as soon as I press a number. Is it possible to only show it in mm?
vue.js
Is it possible to do bidirectional calculations on an input in vue.js? My data is in inches and has to stay in inches. I want to convert my data to other units in a input box and after the user put in some value it has to be converted back to inches.
<input :value="someCrazyBidirectionalCalculation(dataInInches)">
Follow up question:
I followed the guide to make a two-way filter in vue.js v2
<v-unit-input label="Height" v-model="parameters.height"></v-unit-input>
Vue.component('v-unit-input', {
template:
'<v-text-field ref="input" :label="label" type="number" suffix="mm" :value="value" @input="toInch($event.target.value)" @blur="toMm"></v-text-field>',
props: {
value: {
type: Number,
default: 0
},
label: {
type: String,
default: ''
}
},
mounted: function () {
this.toMm()
},
methods: {
toInch: function (value) {
this.$emit('input', this.value / 25.4)
},
toMm: function () {
this.$refs.input.value = this.value * 25.4
},
}
})
It does it job when sending data from the backend to the input but changing the input gives this error:
[Vue warn]: Error in event handler for "input": "TypeError: Cannot read property 'value' of undefined"
Edit 2:
It seems $event.target.value was the problem. My value is stored in $event.
Data from the backend is converted from inches to mm and when entering data in mm it is converted back to inches but it is also displayed in inches as soon as I press a number. Is it possible to only show it in mm?
vue.js
vue.js
edited Nov 23 '18 at 16:56
biervat
asked Nov 22 '18 at 19:03
biervatbiervat
33
33
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
1
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15
add a comment |
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
1
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
1
1
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15
add a comment |
2 Answers
2
active
oldest
votes
I recomend you use a filter but not in de same input maybe you can put the result on inches after than input.
<input v-model="number">
<label>{{ number | inches }}</label>
Regards!
add a comment |
It's finally working.
Vue.component('v-unit-input', {
template:
'<v-text-field ref="textfield" :label="label" suffix="mm" :value="to_mm" @input="toInch($event)" v-bind="$attrs" "></v-text-field>',
props: {
value: Number,
label: String,
},
computed: {
to_mm(){
const mm = Math.round(this.value * 25.4);
return mm
},
},
methods: {
toInch: function (val) {
if (val !== '-') {
const result = val / 25.4;
this.$emit('input', result)
}
}
}
})
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436830%2fvue-js-bidirectional-calculation-on-input-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I recomend you use a filter but not in de same input maybe you can put the result on inches after than input.
<input v-model="number">
<label>{{ number | inches }}</label>
Regards!
add a comment |
I recomend you use a filter but not in de same input maybe you can put the result on inches after than input.
<input v-model="number">
<label>{{ number | inches }}</label>
Regards!
add a comment |
I recomend you use a filter but not in de same input maybe you can put the result on inches after than input.
<input v-model="number">
<label>{{ number | inches }}</label>
Regards!
I recomend you use a filter but not in de same input maybe you can put the result on inches after than input.
<input v-model="number">
<label>{{ number | inches }}</label>
Regards!
answered Nov 22 '18 at 19:14
iToxiTox
11
11
add a comment |
add a comment |
It's finally working.
Vue.component('v-unit-input', {
template:
'<v-text-field ref="textfield" :label="label" suffix="mm" :value="to_mm" @input="toInch($event)" v-bind="$attrs" "></v-text-field>',
props: {
value: Number,
label: String,
},
computed: {
to_mm(){
const mm = Math.round(this.value * 25.4);
return mm
},
},
methods: {
toInch: function (val) {
if (val !== '-') {
const result = val / 25.4;
this.$emit('input', result)
}
}
}
})
add a comment |
It's finally working.
Vue.component('v-unit-input', {
template:
'<v-text-field ref="textfield" :label="label" suffix="mm" :value="to_mm" @input="toInch($event)" v-bind="$attrs" "></v-text-field>',
props: {
value: Number,
label: String,
},
computed: {
to_mm(){
const mm = Math.round(this.value * 25.4);
return mm
},
},
methods: {
toInch: function (val) {
if (val !== '-') {
const result = val / 25.4;
this.$emit('input', result)
}
}
}
})
add a comment |
It's finally working.
Vue.component('v-unit-input', {
template:
'<v-text-field ref="textfield" :label="label" suffix="mm" :value="to_mm" @input="toInch($event)" v-bind="$attrs" "></v-text-field>',
props: {
value: Number,
label: String,
},
computed: {
to_mm(){
const mm = Math.round(this.value * 25.4);
return mm
},
},
methods: {
toInch: function (val) {
if (val !== '-') {
const result = val / 25.4;
this.$emit('input', result)
}
}
}
})
It's finally working.
Vue.component('v-unit-input', {
template:
'<v-text-field ref="textfield" :label="label" suffix="mm" :value="to_mm" @input="toInch($event)" v-bind="$attrs" "></v-text-field>',
props: {
value: Number,
label: String,
},
computed: {
to_mm(){
const mm = Math.round(this.value * 25.4);
return mm
},
},
methods: {
toInch: function (val) {
if (val !== '-') {
const result = val / 25.4;
this.$emit('input', result)
}
}
}
})
answered Nov 26 '18 at 11:46
biervatbiervat
33
33
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436830%2fvue-js-bidirectional-calculation-on-input-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can use a @keyup on each field to trigger a method to convert and update the other field when something is typed in in either
– Nick M
Nov 22 '18 at 19:31
1
I'm not clear on what you mean here. How does the user know what unit they are using?
– Daniel
Nov 22 '18 at 20:05
There is only one input field. The users unit depends on his settings. But the backend is always in inches.
– biervat
Nov 23 '18 at 8:38
I've found the two way filter from vuejs v1 does exactly what I want but it's depricated. I have to follow this guide vuejs.org/v2/guide/migration.html#Two-Way-Filters-replaced
– biervat
Nov 23 '18 at 10:15