使用替换和位置设置器不正常工作。

huangapple go评论55阅读模式
英文:

Using replace and position setters not working properly

问题

以下是您的代码的中文翻译:

我有以下的代码,思路是将输入的值更改为不同的语言,并显示在输入框中。它运行良好,但一旦将光标放在文本的中间,例如在那里键入一些内容,光标会再次跳到末尾。

到目前为止,我有以下代码:

<mat-form-field *ngIf="operationType != 'joint'"
[ngClass]="{'inputBIG uniqueStander': item?.person?.factualAddress?.length > 50, 'inputBIG': !item?.person?.factualAddress?.length > 50}"
class="inputBIG" appearance="outline">
<mat-label>მისამართი</mat-label>
<input autocomplete="off" autocomplete="off" matInput placeholder=""
  [(ngModel)]="dataaa" name="address" [maxlength]="maxFactAddress"
  (keyup)="iReplaceToGEO('firstAddr')" id="firstAddr">
</mat-form-field>

还有相应的TypeScript代码:

dataaa: any;

public iReplaceToGEO(position?): void {
  console.log('position', position);
  if (position) {
    this.getCaretPosition(position);
  }
  this.dataaa = replacer(this.dataaa);
  if (position) {
    this.setCaretPosition(position, this.caretPos, this.caretPos);
  }
  function replacer(val): string {
    val = val
      .replace(/a/g, 'ა')
      .replace(/b/g, 'ბ')
      .replace(/c/g, 'ც')
      .replace(/d/g, 'დ')
      .replace(/e/g, 'ე')
      .replace(/f/g, 'ფ')
      .replace(/g/g, 'გ')
      .replace(/h/g, 'ჰ')
      .replace(/i/g, 'ი')
      .replace(/j/g, 'ჯ')
      .replace(/k/g, 'კ')
      .replace(/l/g, 'ლ')
      .replace(/m/g, 'მ')
      .replace(/n/g, 'ნ')
      .replace(/o/g, 'ო')
      .replace(/p/g, 'პ')
      .replace(/q/g, 'ქ')
      .replace(/r/g, 'რ')
      .replace(/s/g, 'ს')
      .replace(/t/g, 'ტ')
      .replace(/u/g, 'უ')
      .replace(/v/g, 'ვ')
      .replace(/w/g, 'წ')
      .replace(/x/g, 'ხ')
      .replace(/y/g, 'ყ')
      .replace(/z/g, 'ზ')
      .replace(/J/g, 'ჟ')
      .replace(/T/g, 'თ')
      .replace(/R/g, 'ღ')
      .replace(/S/g, 'შ')
      .replace(/C/g, 'ჩ')
      .replace(/Z/g, 'ძ')
      .replace(/W/g, 'ჭ');
    return val;
  }
}

caretPos: number = 0;
public getCaretPosition(ctrl) {
  let arr: any = document.getElementById(ctrl);
  console.log('getCaretPosition', arr.selectionStart);
  if (arr.selectionStart || arr.selectionStart == '0') {
    this.caretPos = arr.selectionStart;
    return {
      start: arr.selectionStart,
      end: arr.selectionEnd,
    };
  } else {
    this.caretPos = 0;
    return {
      start: 0,
      end: 0,
    };
  }
}
public setCaretPosition(ctrl, start, end) {
  let arr: any = document.getElementById(ctrl);
  console.log('setCaretPosition', arr.setSelectionRange);
  console.log('start', start);
  console.log('end', end);
  if (arr.setSelectionRange) {
    arr.focus();
    arr.setSelectionRange(start, end);
  } else if (arr.createTextRange) {
    var range = arr.createTextRange();
    range.collapse(true);
    range.moveEnd('character', end);
    range.moveStart('character', start);
    range.select();
  }
  this.caretPos = this.caretPos + 1;
}

您还可以查看StackBlitz示例链接:https://stackblitz.com/edit/angular-material-mat-form-fields-kpryeu?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts

英文:

I have following code, idea is to change input value to different language and show it in the input. It works fine, but as soon an you place cursor in the middle of the text for example and type something there cusror jumps to the end again.

So far I have this:

&lt;mat-form-field *ngIf=&quot;operationType != &#39;joint&#39;&quot;
[ngClass]=&quot;{&#39;inputBIG uniqueStander&#39;:item?.person?.factualAddress?.length &gt; 50, &#39;inputBIG&#39;:!item?.person?.factualAddress?.length &gt; 50}&quot;
class=&quot;inputBIG&quot; appearance=&quot;outline&quot;&gt;
&lt;mat-label&gt;მისამართი&lt;/mat-label&gt;
&lt;input autocomplete=&quot;off&quot; autocomplete=&quot;off&quot; matInput placeholder=&quot;&quot;
[(ngModel)]=&quot;dataaa&quot; name=&quot;address&quot; [maxlength]=&quot;maxFactAddress&quot;
(keyup)=&quot;iReplaceToGEO(&#39;firstAddr&#39;)&quot; id=&quot;firstAddr&quot;&gt;
&lt;/mat-form-field&gt;

and typescript as well:

  dataaa: any;
public iReplaceToGEO(position?): void {
console.log(&#39;position&#39;, position);
if (position) {
this.getCaretPosition(position);
}
this.dataaa = replacer(this.dataaa);
if (position) {
this.setCaretPosition(position, this.caretPos, this.caretPos);
}
function replacer(val): string {
val = val
.replace(/a/g, &#39;ა&#39;)
.replace(/b/g, &#39;ბ&#39;)
.replace(/c/g, &#39;ც&#39;)
.replace(/d/g, &#39;დ&#39;)
.replace(/e/g, &#39;ე&#39;)
.replace(/f/g, &#39;ფ&#39;)
.replace(/g/g, &#39;გ&#39;)
.replace(/h/g, &#39;ჰ&#39;)
.replace(/i/g, &#39;ი&#39;)
.replace(/j/g, &#39;ჯ&#39;)
.replace(/k/g, &#39;კ&#39;)
.replace(/l/g, &#39;ლ&#39;)
.replace(/m/g, &#39;მ&#39;)
.replace(/n/g, &#39;ნ&#39;)
.replace(/o/g, &#39;ო&#39;)
.replace(/p/g, &#39;პ&#39;)
.replace(/q/g, &#39;ქ&#39;)
.replace(/r/g, &#39;რ&#39;)
.replace(/s/g, &#39;ს&#39;)
.replace(/t/g, &#39;ტ&#39;)
.replace(/u/g, &#39;უ&#39;)
.replace(/v/g, &#39;ვ&#39;)
.replace(/w/g, &#39;წ&#39;)
.replace(/x/g, &#39;ხ&#39;)
.replace(/y/g, &#39;ყ&#39;)
.replace(/z/g, &#39;ზ&#39;)
.replace(/J/g, &#39;ჟ&#39;)
.replace(/T/g, &#39;თ&#39;)
.replace(/R/g, &#39;ღ&#39;)
.replace(/S/g, &#39;შ&#39;)
.replace(/C/g, &#39;ჩ&#39;)
.replace(/Z/g, &#39;ძ&#39;)
.replace(/W/g, &#39;ჭ&#39;);
return val;
}
}
caretPos: number = 0;
public getCaretPosition(ctrl) {
let arr: any = document.getElementById(ctrl);
console.log(&#39;getCaretPosition&#39;, arr.selectionStart);
if (arr.selectionStart || arr.selectionStart == &#39;0&#39;) {
this.caretPos = arr.selectionStart;
return {
start: arr.selectionStart,
end: arr.selectionEnd,
};
} else {
this.caretPos = 0;
return {
start: 0,
end: 0,
};
}
}
public setCaretPosition(ctrl, start, end) {
let arr: any = document.getElementById(ctrl);
console.log(&#39;setCaretPosition&#39;, arr.setSelectionRange);
console.log(&#39;start&#39;, start);
console.log(&#39;end&#39;, end);
if (arr.setSelectionRange) {
arr.focus();
arr.setSelectionRange(start, end);
} else if (arr.createTextRange) {
var range = arr.createTextRange();
range.collapse(true);
range.moveEnd(&#39;character&#39;, end);
range.moveStart(&#39;character&#39;, start);
range.select();
}
this.caretPos = this.caretPos + 1;
}

You can also check stackblitz example:
https://stackblitz.com/edit/angular-material-mat-form-fields-kpryeu?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts

答案1

得分: 1

这是您要翻译的代码部分:

It's because yo need "wait" to Angular redraw the value before position the cursor.

I simply a bit your code

public iReplaceToGEO(event:any,name:string): void {
    const item=event.target;
    const pos = item.selectionStart;
    const value = item.value;
    this[name]=this.replacer(value);
    setTimeout(() => {
        item.selectionStart=item.selectionEnd=pos;
    })
}

Where you define your function replacer outside the function:

replacer(val:string): string {
    return val
        .replace(/a/g, '')
        .replace(/b/g, '')
        ...
}

You use not using (keyup) else (input) event

<input matInput  ... 
       [(ngModel)]="dataaa" 
       (input)="iReplaceToGEO($event,'dataaa')" >

Your [forked stackblitz][1]

NOTE: If you use Reactive Forms you should use `form.get(name).setValue(this.replacer(value))` instead of `this[name]=this.replacer(value);`

NOTE2: using (input) work also CTRL+V

[1]: https://stackblitz.com/edit/angular-material-mat-form-fields-4pkshc?file=src%2Fapp%2Fapp.component.ts
英文:

It's because yo need "wait" to Angular redraw the value before position the cursor.

I simply a bit your code

public iReplaceToGEO(event:any,name:string): void {
const item=event.target;
const pos = item.selectionStart;
const value = item.value;
this[name]=this.replacer(value);
setTimeout(()=&gt;{
item.selectionStart=item.selectionEnd=pos;
})
}

Where you define your function replacer outside the function:

 replacer(val:string): string {
return val
.replace(/a/g, &#39;ა&#39;)
.replace(/b/g, &#39;ბ&#39;)
...
}

You use not using (keyup) else (input) event

&lt;input matInput  ... 
[(ngModel)]=&quot;dataaa&quot; 
(input)=&quot;iReplaceToGEO($event,&#39;dataaa&#39;)&quot; &gt;

Your forked stackblitz

NOTE: If you use Reactive Forms you should use form.get(name).setValue(this.replacer(value)) instead of this[name]=this.replacer(value);

NOTE2: using (input) work also CTRL+V

huangapple
  • 本文由 发表于 2023年2月6日 21:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定