Best practice to handle focus event for input fields

Deadly 提交于 2019-12-31 05:00:08

问题


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:

selectText

Only supported for input control's type of Text, Url, Tel and Password.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!