Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <common-input :inputValue="product.price" v-model="product.price" v-validate="'required'" name="price"></common-input>
  2.  
  3. <template>
  4. <input type="text" :id="id" class="form-control" v-model="value" @keyup="checkData">
  5. </template>
  6. <script>
  7. const {unformat} = require('number-currency-format');
  8. import axiosGetPost from '../../helper/axiosGetPostCommon';
  9.  
  10. export default {
  11. extends: axiosGetPost,
  12. props: ['id','inputValue'],
  13. data() {
  14. return {
  15. value: ''
  16. }
  17. },
  18. mounted() {
  19. },
  20. watch: {
  21. value: function (newVal) {
  22. this.processData(newVal);
  23. },
  24. },
  25. created() {
  26. this.editData();
  27. },
  28. methods: {
  29. editData(){
  30. let instance = this;
  31. if (this.inputValue) {
  32. this.value = instance.numberFormat(this.inputValue);
  33. let check = this.value.search(this.currencySymbol);
  34. this.value = this.value.slice(check);
  35. }
  36. },
  37. checkData(value) {
  38. let formattedNumber = '';
  39. if (this.value && this.value != this.currencySymbol) {
  40. formattedNumber = unformat(this.value, {
  41. thousandSeparator: this.thousandSeparator,
  42. decimalSeparator: this.decimalSeparator,
  43. });
  44. }
  45.  
  46. this.$emit('input', formattedNumber);
  47. },
  48. processData(newVal) {
  49. let allowChar, check, checkDecimalSep;
  50.  
  51. if (this.thousandSeparator === "space") this.thousandSeparator = ' ';
  52.  
  53. allowChar = new RegExp("[^0-9b" + this.thousandSeparator + this.decimalSeparator + "b]", "is");
  54. this.$set(this, 'value', newVal.replace(allowChar, ''));
  55. check = this.value.substring(0, 1);
  56.  
  57. if (check == this.decimalSeparator || check == this.thousandSeparator) this.value = this.value.substring(1);
  58.  
  59. let currentChar = newVal.slice(-1);
  60. if (currentChar == this.thousandSeparator || currentChar == this.currencySymbol || currentChar == this.decimalSeparator) {
  61.  
  62. checkDecimalSep = newVal.split(this.decimalSeparator).length - 1;
  63.  
  64. if (checkDecimalSep > 1) {
  65. this.value = this.value.substring(0, this.value.length - 1);
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement