0

I would like to format entered numbers with comma - thousand delimiter like that (1000000 -> 1000,000). For this purpose I want to use KeyUpEventHandler: @keyup.stop=“KeyUpEventHandler”. The issue is that - I cannot move the position of Caret at the end of a text. I found that it’s possible to do with Javascript Range but I never used it before. Could you help me to fix my code please. Thanks!

KeyUpEventHandler: function(event) {
  var self = this;
  var evt = event || window.event;

  var charCode = event.which || event.keyCode;  
  // skip for arrow keys:
  if(charCode >= 37 && charCode <= 40) return;

  var value = event.target.innerHTML;
  var len = value.length;
  console.log("value: " + value +" len: " + len);
  var commaFormatted = value.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");

  if (typeof window.getSelection != "undefined") {
    var el = window.getSelection();
    console.log(el);
    if (el.getRangeAt && el.rangeCount) {
      var range = el.getRangeAt(0);
      var lastChild = el.lastChild; //null
      //var endOffset = el.childNodes.length;
      //el.innerHTML = commaFormatted; //not visible for Vue
      var spanEl = self.$refs[self.refName]; 
      spanEl.innerHTML = commaFormatted;
      //var range = spanEl.getRangeAt(0);
      //range.setEnd(spanEl, len); 
      range.setEndAfter(lastChild); //.setEndAfter**strong text**(spanEl, endOffset);
    }

  }
},

Sorry please for misunderstanding. There is no issue with formatting numbers:

formatNumber: function(num) {
  var parts = num.split('.');
  var part1 = parts[0];
  var part2 = parts.length > 1 ? '.' + parts[1].substring(0, 2) : ''; // always two decimal digits
  //BAD: https://blog.abelotech.com/posts/number-currency-formatting-javascript/ <-- BAD
  //BAD: return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); //BAD
  return part1.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",") + part2;    
},

The issue is only with Cursor (Caret) Position at elements that are editable by the user:
css -webkit-user-modify: read-write;

Thank you all! I found a working solution:

formatNumber: function(num) {
  var parts = num.split('.');
  var part1 = parts[0];
  var part2 = parts.length > 1 ? '.' + parts[1].substring(0, 2) : ''; // always two decimal digits
  return part1.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",") + part2;    
},
KeyUpEventHandler: function(event) {
  var self = this;
  var evt = event || window.event;

  var charCode = evt.which || evt.keyCode;  
  // skip for arrow keys - we want to allow navigation around textbox using arrow keys
  if(charCode >= 37 && charCode <= 40) return;

  if (typeof window.getSelection != "undefined") {
    var selection = window.getSelection();
    var value = event.target.innerHTML;
    var len = value.length;

    var commaFormatted = self.formatNumber(value);
    var el = self.$refs[self.refName]; 
    el.innerHTML = commaFormatted;

    if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
    {
      range = document.createRange();
      range.selectNodeContents(el);
      range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start
      selection.removeAllRanges();//remove any selections already made
      selection.addRange(range); 
    } else if(document.selection)//IE 8 and lower
    {
      range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
      range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
      range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
      range.select();//Select the range (make it the visible selection
    }

  };

}
Alex Pilugin
  • 683
  • 2
  • 10
  • 38
  • Possible duplicate of [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Salim Djerbouh Sep 16 '19 at 12:21
  • Thank you very much for your comment. Awesome link! But this question is not only about comma as thousands separators. My question about Caret Position after text – Alex Pilugin Sep 16 '19 at 12:25
  • Not sure I understand, why dont you use vues `data` object and use interpolation to display your mutated numbers? – Michael Sep 16 '19 at 15:20
  • Since this is contenteditable HTML Element I don't use data – Alex Pilugin Sep 16 '19 at 15:35

0 Answers0