cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 cannot get the value of the Input Tag

AJeB
Participant
0 Kudos

Hi

Why the alert is not displaying the text, I think my code is correct, but I don't know why its not displaying the text if i add {ProductID}

heres my code

Odata: Northwind from services.odata.org

Home.xml

                                <VBox id="_IDGenVBox3" class="sapUiSmallMargin">
                                    <Title id="_IDGenTitle1" text="ProductID: {ProductID}" wrapping="true" />    
                                    <Label id="_IDGenLabel7" text="Name: {ProductName}" wrapping="true" class="sapUiTinyMarginBottom" />
                                    <Label id="_IDGenLabel8" text="Available Stock: {UnitsInStock}" wrapping="true" class="sapUiTinyMarginBottom" />
                                    <Text id="_IDGenText1" text="A great description with useful information about this project. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy." wrapping="true" />
                                </VBox>
                                
                                <VBox id="_IDGenVBox4" class="sapUiSmallMargin">
                                    <OverflowToolbar id="_IDGenOverflowToolbar1" design="Solid" class="sapContrast">
                                        <!-- <ToolbarSpacer id="_IDGenToolbarSpacer2" /> -->
                                        <Title id="_IDGenTitle2" text="Price: {UnitPrice}" wrapping="true" />
                                        <Button id="_IDGenButton1" icon="sap-icon://cart-4" type="Transparent" press="onAddToCart()" />
                                        <Button id="_IDGenButton2" icon="sap-icon://credit-card" type="Transparent" press="onBuy()" />
                                    </OverflowToolbar>
                                </VBox>

 Home.controller.js

            onAddToCart: function (){
                var productID = this.getView().byId("_IDGenTitle1").getText();
                alert(productID);

            }       

Result

AJeB_0-1716403840860.png

but if i remove the {ProductID} it is working fine

AJeB_1-1716403939566.png

AJeB_2-1716404009483.png

 

View Entire Topic
junwu
Active Contributor
0 Kudos
press="onAddToCart" 
0 Kudos

I did a quick try of this example. I am able to see the text correctly. May be can you check your binding, if the data is available.

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel",
    "sap/m/MessageBox"
],
    /**
     * @Param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller,JSONModel,MessageBox) {
        "use strict";

        return Controller.extend("com.demo.test1.controller.View1", {
            onInit: function () {
                let model = new JSONModel({
                    product: "Abc"
                });
                this.getView().setModel(model,"test")
            },

            handleAdd: function() {
               let text =  this.getView().byId("idProduct");
               let textValue = text.getText();
               MessageBox.success(textValue);
            }
        });
    });
<mvc:View controllerName="com.demo.test1.controller.View1"
    xmlns:mvc="sap.ui.core.mvc" displayBlock="true"
    xmlns="sap.m">
    <Page id="page" title="{i18n>title}">
        <content>
            <Text id="idProduct" text="Product: {test>/product}" />
            <Button id="idButton" text="Add" press="handleAdd" />
        </content>
    </Page>
</mvc:View>

 

AJeB
Participant
0 Kudos

I changed it to press="onAddToCart" and added a MessageBox but still not working

the productID is binded properly and it is showing in the view

AJeB_0-1716904810152.png

but if i click the add to cart the ID should show in the MessageBox or Alert but its not working

MessageBox

AJeB_0-1716904907850.png

Alert result

AJeB_0-1716904971916.png