2

I need to find all Input elements (sap.m.Input) on a website. I know I can find an element by its ID via sap.ui.getCore().byId() but this does not help me here.

I am looking for something like sap.ui.getCore().getByType("sap.m.Input"). How do I do this?

Edit: I do not have access to the source code of the website, I am injecting a javascript via a chrome extension on websites which use SAPUI5

3 Answers3

1

From the current page

<mvc:View ...>
  <Input fieldGroupIds="myInputs" />
</mvc:View>
// In the Controller
this.getView().getControlsByFieldGroupId("myInputs").filter(c => c.isA("sap.m.Input"));

From the whole app

Leveraging this solution in Get list of all instantiated controls in registry:

const allRegisteredControls = sap.ui.getCore().byFieldGroupId(""); // From https://stackoverflow.com/a/54227512/5846045
const inputControls = allRegisteredControls.filter(c => c.isA("sap.m.Input"));

API reference: sap.ui.base.Object#isA

This returns all registered instances of the given type. Please not that already destroyed elements won't be included.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • I do not have access to the source code of the website, therefore I can't add custom fieldGroupIds to every Input. I do like your second solution but could you clarify what "For the whole app" means here? – Konstantin Dobler Jan 16 '19 at 22:28
  • @KonstantinDobler By "app" I mean the UI5 web app the user is currently using. I used the term _app_ to indicate that the second solution is to retrieve **all** the controls (in our case, instances of `sap.m.Input`) that have been created, and not yet destroyed, since the start of the web app. Since UI5 apps usually consist of multiple views/pages, you might get false positives that were created at some point but not included at the current view/page user is viewing. – Boghyon Hoffmann Jan 16 '19 at 22:39
  • I found [this method](https://stackoverflow.com/a/48626590/10917436) to check whether a given control is currently visible to the user. Using this I would be able to modify the "For the whole App" solution to only include controls on the current page. – Konstantin Dobler Jan 16 '19 at 23:06
1

I found this solution:

I get all elements of the class sap.m.Input from the DOM-Tree with document.getElementsByClassName("sap.m.Input")`.

I then get the corresponding UI5-Elements by calling sap.ui.getCore().byId() on the ID of each element of that array.

  • I also prefer this way, since the performance is not good when getting all UI5 elements by fieldgroup "". Class name is usually something like sapMDialog when using css classes and not the ui5 element class sap.m.Dialog – tietze111 Feb 14 '22 at 09:58
0

what you can do is to set each Input field to a specific Field Group like this

<input fieldGroupIds="MyGroup" />

and then get all those fields using the byFieldGroupId method like this

var aMyGroupControls = sap.ui.getCore().byFieldGroupId("MyGroup");

aMyGroupControls will be filled with an Array of Controls

StErMi
  • 5,389
  • 5
  • 48
  • 71