cancel
Showing results for 
Search instead for 
Did you mean: 

setText doesn't work in attachRequestCompleted

UlisesCasal
Explorer
0 Kudos

Hello everyone!!, I’m developing in sapui5 and I want to be able to setText to an element of my View, but when I run it, it gives me the following error:

UlisesCasal_0-1708699381855.png

The code below:

_onObjectMatched: function(oEvent) {
			var oModel = new JSONModel(
				sap.ui.require.toUrl("Ejercicio1Tabla/jg.json")
			);
			this.getView().setModel(oModel, "jg");

			var cc = oEvent.getParameter("arguments").equipo1;
			var cc1 = oEvent.getParameter("arguments").equipo2;
			var textId1 = this.getView().byId("tl");
			var strTitle = 'Jugadores del encuentro ' + cc + ' VS ' + cc1;
			textId1.setText(strTitle);
			var jugadores = [];
			var that = this;
			oModel.attachRequestCompleted(function() {
				var data = oModel.getData();

				data.jg.forEach(function(partido) {
					if (partido.local === cc && partido.visitante === cc1) {
						jugadores.push(partido.jugador1);
						jugadores.push(partido.jugador2);
						jugadores.push(partido.jugador3);
					}
				});
				var txtJugadores = that.getView().byId("tx1");
				txtJugadores.setText(jugadores);
			});

		}

Regards

Ryan-Crosby
Active Contributor
0 Kudos
You need to check the result on line 24 because it indicates that txtJugadores is undefined. You cannot call a method setText on an undefined object.

Accepted Solutions (1)

Accepted Solutions (1)

Edrilan_Berisha
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

 

what exactly do you try to achieve here? You want to bind the table, no? If so then what you try to do is not correct.

You should bind the table with the data you get from some backend OData service for instance.

 

 

<Text id="tx1" text="{jg>/items}"/> 

 

 

You should not set the text of each column and row yourself with the setText() method. This is not how the setText() method was intended to be used.

 

You should read into the topic of binding sap.m.table, there are also questions already and blogs about this topic in this community.

https://community.sap.com/t5/technology-q-a/how-to-bind-json-model-to-sap-ui5-m-table/qaq-p/11896079

 

Best,

Edrilan Berisha

SAP S/4HANA Cloud Financials Development

 

Answers (1)

Answers (1)

Edrilan_Berisha
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

 

as already pointed out, you didn't fetch the element with that

 

that.getView().byId("tx1");

 

 

Can you share how your view looks like?

 

Best,

Edrilan Berisha

SAP S/4HANA Cloud Financials Development

 

 

 

UlisesCasal
Explorer
0 Kudos
Hi
UlisesCasal
Explorer
0 Kudos
<mvc:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" controllerName="Ejercicio1Tabla.controller.Jugadores" xmlns:html="http://www.w3.org/1999/xhtml" id="jugadores"> <App> <pages> <Page title="Title"> <content> <Table items="{jg>/items}" id="tabla"> <headerToolbar> <OverflowToolbar id="ov"> <content> <Title id="tl" text="Jugadores del encuentro {cc} vs {cc1}" level="H2"/> <ToolbarSpacer id="tb"/> </content> </OverflowToolbar> </headerToolbar> <columns> <Column width="12em" id="c1"> <Text text="Nombre" id="nombre"/> </Column> <Column id="c2" hAlign="Center"> <Text text="Apellido" id="apellido"/> </Column> <Column id="c3" minScreenWidth="Desktop" demandPopin="true"> <Text text="Cantidad de goles" id="goles"/> </Column> </columns> <items> <ColumnListItem vAlign="Middle" id="cl1"> <cells> <Text id="tx1"/> </cells> </ColumnListItem> </items> </Table> </content> </Page> </pages> </App> </mvc:View>
Ryan-Crosby
Active Contributor
0 Kudos
@UlisesCasal you cannot retrieve a text field from the view directly that is part of an aggregation like that.