Search code examples
javascriptodatasapui5sap-fiorijsonmodel

Complete data not getting bind to view in Fiori


I have 2 views in Fiori. From the first view the app is navigated to the second where an OData service is called and the data is bind to a JSONModel which is then bind to the XML and then a create service is called to complete the transaction and the user is navigated back to the first page. The issue comes when one transaction is done and then for another transaction when the user again goes to the second screen, complete data doesn't get bind. Below is the code :

XML :

<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns:html="http://www.w3.org/1999/xhtml" xmlns="sap.m" controllerName="com.fm.zwmlabels.controller.ThirdPage" displayBlock="true">
<App id="idAppControl">
    <pages>
        <Page title="{i18n>title}">
            <content>
                <sap.ui.layout.form:SimpleForm xmlns:sap.ui.layout.form="sap.ui.layout.form" xmlns:sap.ui.core="sap.ui.core" editable="true" layout="ResponsiveGridLayout" id="form0" class="sapUiLargeMarginBegin">
                    <sap.ui.layout.form:content>
                        <Label text="{i18n>MaterialNumber}" id="label0"/>
                        <Input width="80%" value="{infoModel>/info/Material}" id="materialnumber" editable="false"/>
                        <Label text="{i18n>PalletLabelNumber}" id="label1"/>
                        <Input width="80%" value="{path : 'infoModel>/info/PalletLabelNumber' , formatter:'com.fm.zwmlabels.controller.formatter.removeLeadingZeroes'}" id="PalletLabelNumber"/>
                         <Label text="{i18n>Noofpistonsperpallet}" id="label2"/>
                          <Input width="80%" value="{path : 'infoModel>/info/pistonsperpallet', formatter:'com.fm.zwmlabels.controller.formatter.removeLeadingZeroes'}" id="Noofpistonsperpallet" editable="false"/>
                          <Label text="{i18n>Noofboxlabels}" id="label3"/>
                          <Input width="80%" id="Noofboxlabels" value="{path : 'infoModel>/info/Nrboxlabel', formatter:'com.fm.zwmlabels.controller.formatter.removeLeadingZeroes'}" editable="false"/>
                    <Label text="{i18n>Noofpistonsperbox}" id="label4"/>
                          <Input width="80%" value="{path : 'infoModel>/info/Nrpistonsbox', formatter:'com.fm.zwmlabels.controller.formatter.removeLeadingZeroes'}" id="Noofpistonsperbox" editable="false"/>
                          <Label text="{i18n>Outputdevicepallet}" id="label5"/>
                          <Input width="80%" value="{infoModel>/info/Outputdevpal}" id="Outputdevicepallet" showValueHelp="true" valueHelpOnly="true" valueHelpRequest="showOutputDeviceValueHelp"/>
                           <Label text="{i18n>Outputdevicebox}" id="label6"/>
                          <Input width="80%" id="Outputdevicebox" value="{infoModel>/info/Outputdevbox}" showValueHelp="true" valueHelpOnly="true" valueHelpRequest="showOutputDeviceValueHelp"/>
                           <Label text="{i18n>Noofpalletlabels}" id="label7"/>
                          <Input width="80%" value="{path : 'infoModel>/info/Noofpallab', formatter:'com.fm.zwmlabels.controller.formatter.removeLeadingZeroes'}" id="Noofpalletlabels"/>
                        </sap.ui.layout.form:content>
                </sap.ui.layout.form:SimpleForm>
            </content>

            <footer>
            <Bar id="bar">
                <contentRight>
                
                    <Button id="_IDGenButton2" text="{i18n>Confirm}" type="Emphasized" tooltip="{i18n>Continue}" enabled="true" press="confirmPress"/>
                </contentRight>
            
            </Bar>
        </footer>
        </Page>
    </pages>
</App>

</mvc:View>

Controller.js :

sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/m/Dialog",
"sap/m/Button",
"sap/m/Label",
"sap/m/library",
"sap/m/MessageToast",
"sap/m/Text",
"sap/m/TextArea",
"com/fm/zwmlabels/controller/formatter",
'sap/ui/model/Filter',
'sap/ui/model/FilterOperator'
 ], function (Controller, Dialog, Button, Label, mobileLibrary, MessageToast, Text, TextArea, formatter, Filter, FilterOperator) {
"use strict";

return Controller.extend("com.fm.zwmlabels.controller.ThirdPage", {


    onInit: function () {
        this.router = sap.ui.core.UIComponent.getRouterFor(this);
        this.router.attachRoutePatternMatched(this._handleRouteMatched, this);

        this.infoModel = new sap.ui.model.json.JSONModel();
        this.getView().setModel(this.infoModel, "infoModel");

        t
    },

    
   //This is where the service is called and data is bind to the view
    
    callInformationSet: function () {
        var that = this;
        var oModel = this.getView().getModel();
        this.defineBusyIndicator(oModel);
        oModel.refresh();
        oModel.read("/ZLABEL_INFORMATIONSet('" + this.selectedVariant + "')", {
            success: function (oData, oResponse) {
                that.infoModel.setProperty("/info", oData);
                that.HUExists=oData.Huexist;
                that.callOutputDeviceData();
                console.log(oData);
            },
            error: function (error) {
                console.log(error);
            }
        });
    },

    

    callOutputDeviceData: function () {
        var oModel = this.getView().getModel();
        this.defineBusyIndicator(oModel);
        var that = this;
        oModel.read("/OuputDevicePalletSearchHelpSet", {
            success: function (oData, oResponse) {
                that.infoModel.setProperty("/output", oData.results);
                console.log(oData);
            },
            error: function (error) {
                console.log(error);
            }
        });

    },

    

    

    
    

    onConfirm: function () {
        var Material = this.getView().byId("materialnumber")
        var PalletLabelNumber = this.getView().byId("PalletLabelNumber");
        var pistonsperpallet = this.getView().byId("Noofpistonsperpallet");
        var boxlabels = this.getView().byId("Noofboxlabels");
        var pistonsperbox = this.getView().byId("Noofpistonsperbox");
        var outputdevicepallet = this.getView().byId("Outputdevicepallet");
        var outputdevicebox = this.getView().byId("Outputdevicebox");
        var palletlabels = this.getView().byId("Noofpalletlabels");

        if (this.checkEmptyValues(Material)===1 && this.checkEmptyValues(PalletLabelNumber)===1 && this.checkEmptyValues(pistonsperpallet)===1 && this.checkEmptyValues(boxlabels)===1 && this.checkEmptyValues(pistonsperbox)===1 && this.checkEmptyValues(outputdevicepallet)===1 && this.checkEmptyValues(outputdevicebox)===1 && this.checkEmptyValues(palletlabels)=== 1) {
            var data = {};
            data.Material = Material.getValue();
            data.BapiMsg = "";
            data.Variant = this.selectedVariant;
            data.PalletLabelNumber = PalletLabelNumber.getValue();
            data.pistonsperpallet = pistonsperpallet.getValue();
            data.Nrboxlabel = boxlabels.getValue();
            data.Nrpistonsbox = pistonsperbox.getValue();
            data.Outputdevpal = outputdevicepallet.getValue();
            data.Outputdevbox = outputdevicebox.getValue();
            data.Noofpallab = palletlabels.getValue();

            

        var that = this;
        var oModel = this.getView().getModel();
        oModel.create("/ZLABEL_INFORMATIONSet", data, {
            success: function (oData, oResponse) {

                if (oData.BapiMsg.length !== 0) {
                    that._showSuccess(oData.BapiMsg);
                } else {
                    //BusyDialog close
                    // that._oBusyDialog.close();
                    // //Show Fail Message
                    // that._showSendFailure(oData, "");
                }

            },
            error: function (error) {
                //BusyDialog close
                that._oBusyDialog.close();
                that._showSendFailure("", error);
            }
        });
        }
        else
            return;
    },

    checkEmptyValues : function(elementId)
    {
        if(elementId.getValue()==="")
        {
            elementId.setValueState("Error");
            return 0;
        }
        else
        {
            elementId.setValueState("None");
            return 1;
        }

        

    },

    

    onResetPress : function()
    {
        this.getView().byId("materialnumber").setValue("");
        this.getView().byId("PalletLabelNumber").setValue("");
        this.getView().byId("Noofpistonsperpallet").setValue("");
        this.getView().byId("Noofboxlabels").setValue("");
        this.getView().byId("Noofpistonsperbox").setValue("");
        this.getView().byId("Outputdevicepallet").setValue("");
        this.getView().byId("Outputdevicebox").setValue("");
        this.getView().byId("Noofpalletlabels").setValue("");
    }


});

});


Solution

  • Adding 'this.getView().getModel().refresh(true);' resolved the issue.