简体   繁体   English

如何在angular8中的tab out后使用shift+tab时选择整个值

[英]How to make the entire values to be selected when shift+tab is used after tab out in angular8

i have an input field as amount, in which currency formatting has been done.我有一个输入字段作为金额,其中货币格式已完成。 Here i enter some values and come out using tab.在这里我输入一些值并使用选项卡出来。 Now i use shift+tab and go to the same field, so in this case i want the cursor is in the last digit i mean first number from right.现在我使用 shift+tab 并转到同一字段,因此在这种情况下,我希望光标位于最后一位数字,我的意思是从右数第一个数字。 Instead i want the present values to be selected by default instead of keeping cursor on the last digit.相反,我希望默认选择当前值,而不是将光标保持在最后一位。 Example: i typed 213.98 it got formatted to $213.98 after tab out, when i again tab in here, the cursor is next to 8, instead i want 213.98 to be selected and based on arrow keys or mouse i can move to number i wish to change.示例:我输入213.98它在标签退出后被格式化为$213.98 ,当我再次在这里标签时,光标在 8 旁边,而不是我想要选择213.98并且基于箭头键或鼠标我可以移动到我想要的数字改变。 Also, when i do ctrl+Z, it has do undo all changes.此外,当我执行 ctrl+Z 时,它会撤消所有更改。

HTML: HTML:

<input type="text" class="form-control" placeholder="Amount in dolars"
            formControlName="amount" autocomplete="off" currencyInput maxDigits="9" [ngClass]="{ 'is-invalid': eoInfo.amount.dirty  && eoInfo.amount.invalid }">

TS: TS:

this.eoInfoForm = this.FB.group({
      amount: ['', Validators.required],
    });

DEMO: DEMO演示: 演示

You can auto select all text by adding onClick="this.select();"您可以通过添加onClick="this.select();"来自动选择所有文本on your input.根据您的输入。 If you want to do be able to do ctrl+z you need to add a listener for your input state like this: (input)="update($event.target.value);"如果您希望能够执行 ctrl+z,您需要为您的输入状态添加一个侦听器,如下所示: (input)="update($event.target.value);" then in the update method you can do this.history.push(value);然后在更新方法中你可以做this.history.push(value); . . So now you should have an array called history with all different values that have been passed through.所以现在你应该有一个名为 history 的数组,其中包含所有已传递的不同值。

Now to listen to ctrl+z keybind to undo a value you add (keyup.control.z)="onKeydown($event)" to the input and in the method you pop your history and set your new value to the the latest item in the history array.现在听 ctrl+z keybind 撤消添加到输入的值(keyup.control.z)="onKeydown($event)"并在方法中弹出历史记录并将新值设置为最新项目在历史数组中。 If you want you can also keep the previous value in a separate property and use that as a step in between.如果需要,您还可以将先前的值保留在单独的属性中,并将其用作中间的步骤。

I hope that this is kinda what you are looking for.我希望这就是你正在寻找的。

EDIT 1:编辑 1:

I do have to mention I commented out the keydown event from the currency validator to show it to you.我不得不提一下,我从货币验证器中注释掉了 keydown 事件以向您展示。 demo演示

You can select the input by simply using the select method on your element during the focus event in the CurrencyInputDirective .CurrencyInputDirective中的focus事件期间,您可以通过简单地在您的元素上使用select方法来选择输入。

@HostListener("focus", ["$event.target.value"])
onFocus(value) {
    // on focus remove currency formatting
    this.el.value = value.replace(/[^0-9.]+/g, '')
    this.el.select();
}

Now you will be able to select the whole option on clicking Shift + Tab .现在您将能够通过单击Shift + Tab来选择整个选项。 As for undoing all the changes using Ctrl + z , you can listen to the keydown event and remove the input accordingly.至于使用Ctrl + z撤消所有更改,您可以监听keydown事件并相应地删除输入。 In your CurrencyInputDirective .在您的CurrencyInputDirective Add a new HostListener to listen to the keydown.control.z event.添加一个新的HostListener来监听keydown.control.z事件。

@HostListener("keydown.control.z", ["$event.target.value"])
onUndo(value) {
    this.el.value = '';
}

StackBlitz Demo StackBlitz 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM