问题
Sadly, I don't find any focus event for sap.m.Input/.TextArea to which I can register handlers in XML view.
What are your best practices e.g. if you have nearly 100 fields and most of them should handle focus event so that the text within the input field gets automatically selected?
Normally, I try to avoid to register the focusin handler in controller on every input field (also looping)... but it seems that there are no other possibilities available isn't it?
What I want is a possibility, that when I navigate e.g. with keyboard through a table with input fields, every time when I press the tab or up/down arrow keys to jump to next input field, the whole content of the input field should be selected.
Example: https://ui5.sap.com/#/entity/sap.m.Table/sample/sap.m.sample.TableEditable (Click on Edit). When tabbing, it automatically selects the text. But it doesn't work with up/down arrow and with sap.m.Input class.
回答1:
Here is a working example of an extended sap.m.Input that selects the text on focus: https://embed.plnkr.co/98BIbMEIujbzBXqU
Input.extend("demo.control.Input", { onfocusin: function() { if (typeof Input.prototype.onfocusin == "function") { Input.prototype.onfocusin.apply(this, arguments); } this.getDomRef("inner").select(); }, // ... });
Note: sap.m.InputBase provides the API selectText(iStart, iEnd). However, that API doesn't support Input controls with type Number according to the HTML spec as well as API reference:
selectTextOnly supported for input control's type of
Text,Url,TelandPassword.
Since our goal is to select all text within the input field (not a range) regardless of the type, domElement.select()api can be used instead.
回答2:
If you really have a form with -- gasp! -- 100 fields, I would then extend the standard sap.m.Input and attach the onfocus browser event in that extended control using sap.ui.core.Control's attachBrowserEvent method.
来源:https://stackoverflow.com/questions/58934859/best-practice-to-handle-focus-event-for-input-fields