[英]Multiple V-model Javascript Vue Js
I want to bind two functions here into one HTML element.我想将这里的两个函数绑定到一个 HTML 元素中。 Bellow the code of Vue function
下面是 Vue function 的代码
var table_content = new Vue({
el: '#table_content',
data: {
arr: [],
_arr_original: [],
price: 0,
},
created(){
// some stuff here
},
methods:{
change(index,index1){
arr = this.arr[index]
arr.total = arr.data[index1].Koef * arr.data[index1].harga_satuan
Vue.set(this.arr,index,arr)
}
},
computed:{
modify_price:{
get(){
return this.price.toLocaleString()
},
set(value){
var v = parseInt(value.replace(/,/g,''))
isNaN(v) ? "" : this.price = v;
}
},
}
})
HTML element HTML 元件
<table class="tableizer-table table-hover" id="table_content">
<thead>
<td>
<div class="handsontableInputHolder">
<textarea tabindex="-1" name="uom" class="handsontableInput area-custom val2 text-center" style="" v-model="data.harga_satuan modify_price" v-on:keyup="change(0,index)"></textarea>
</div>
{{-- <input name="txtEmmail" class="val2"/> --}}
</td>
<td>
<div class="handsontableInputHolder">
<textarea tabindex="-1" name="total" class="handsontableInput area-custom multTotal text-center" disabled style="">@{{ data.Koef * data.harga_satuan }}</textarea>
</div>
</td>
//..
The idea is I want to bind function change
and modify_price
.这个想法是我想绑定 function
change
和modify_price
。 So here are the detail所以这里是细节
Function `change` will handle any input from user and count a total on HTML DOM `total`
Function `modify_price` will `get` input from user (number) and auto number formating with comma that input. In other hands function set will convert text with comma and turns into number.
So how I can running that code simmulteanously and binding both of function?那么我如何同时运行该代码并绑定两个 function? I have check from thisgithub issue that
我从这个github 问题中检查了
Component v-model is designed for single value input components that attend to similar use cases for native input elements.
组件 v-model 专为处理原生输入元素的类似用例的单值输入组件而设计。
For a complex component that manages the synchronization of more than one values, explicit prop/event pairs is the proper solution.
对于管理多个值同步的复杂组件,显式 prop/event 对是正确的解决方案。 In this particular case I don't think the saved keystrokes are worth the added complexity of additional syntax.
在这种特殊情况下,我认为保存的击键不值得增加额外语法的复杂性。
Any idea?任何想法? Thank you.
谢谢你。
After a day doing some research, I found a solution to have 2 v-model in one HTML DOM.经过一天的研究,我找到了在一个 HTML DOM 中有 2 个 v-model 的解决方案。 Here are my solution
这是我的解决方案
First, we need vue.component with v-model binded to computed parameter首先,我们需要 vue.component 和 v-model 绑定到计算参数
Vue.component('number-input', { props: ["value"], template: ` <div> <input type="textarea" class="val2 bhnPrice alatPrice handsontableInput area-custom text-center text-bold" v-model="displayValue" @blur="isInputActive = false" @focus="isInputActive = true"/> </div>`, data: function() { return { isInputActive: false } }, computed: { displayValue: { get: function() { if (this.isInputActive) { return this.value.toString() } else { return this.value.toFixed(0).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,") } }, set: function(modifiedValue) { let newValue = parseFloat(modifiedValue.replace(/[^\d\.]/g, "")) if (isNaN(newValue)) { newValue = 0 } this.$emit('input', newValue) } } } });
Second, change HTML DOM as bellow二、将 HTML DOM 更改为如下
<div class="handsontableInputHolder"> <number-input style="" v-model="data.harga_satuan" v-on:keyup="change(2,index)"></number-input> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.