English 中文(简体)
Vue3 Cleave js not working if not using v-model.lazy
原标题:

Currently i try to use cleavejs to formatting thousand separator my input number, it s show strange behavior when i input 1000.123 it show 1,000.12 which is correct format, but my v-model value is 1000.123 not 1000.12. other people suggesting using v-model.lazy but it only updated when i leave my focus from input text.

Is there any other way to solve this issue without using v-model.lazy ?

This is my current component

<script setup lang="ts">
import { ref, watch } from  vue 
const props = withDefaults(defineProps<Props>(), {
  type:  text ,
  required: false,
  readonly: false,
  disabled: false
})
const inputValue = ref(props.modelValue)
const emit = defineEmits<{
  (e:  update:modelValue , value: string | number): void
}>()

watch(
  () => props.modelValue,
  () => {
    inputValue.value = numeric.format(props.modelValue)
  },
  {
    immediate: true
  }
)

watch(
  inputValue,
  () => {
    emit( update:modelValue , parseFloat(inputValue.value.toString().replace(/(d+),(?=d+(D|$))/g,  $1 )))
  },
  {
    immediate: true
  }
)
</script>

<template>
 <input
        class="form-input"
        v-model.lazy="inputValue"
        v-cleave="{ numeral: true, numeralThousandsGroupStyle:  thousand  }"
        :placeholder="props.placeholder"
        :required="props.required"
        :readonly="props.readonly"
        :disabled="props.disabled"
      />
</template>

I expect v-model return the same value as cleavejs did without using .lazy

问题回答

First, you don t need use watch at all, and we will emit on Cleavejs event

const inputValue = ref(props.modelValue)

Second, emit raw value data from Cleavejs onValueChanged event

const emit = defineEmits<{
  (e:  update:modelValue , value: number): void
}>()

const onValueChanged = (e: any) => {
  emit( update:modelValue , Number(e.target.rawValue))
}

Last, Update your input directive

v-cleave="{ numeral: true, numeralThousandsGroupStyle:  thousand , onValueChanged: onValueChanged }"

Full Component example

<script setup lang="ts">
import { ref } from  vue 

export interface Props {
  modelValue: number
}

const props = withDefaults(defineProps<Props>(), {})

const inputValue = ref(props.modelValue)

const emit = defineEmits<{
  (e:  update:modelValue , value: number): void
}>()

const onValueChanged = (e: any) => {
  emit( update:modelValue , Number(e.target.rawValue))
}
</script>

<template>
  <input
    v-cleave="{ numeral: true, numeralThousandsGroupStyle:  thousand , onValueChanged: onValueChanged }"
    v-model="inputValue"
  />
</template>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签