【问题标题】:Bind Default value to Combox from OData response从 OData 响应将默认值绑定到 Combox
【发布时间】:2018-09-13 11:49:27
【问题描述】:

我是 UI5 的新手,并试图对某人 3 年前编写的现有代码进行更改。我想在视图显示给用户之前向组合框添加默认值。我的问题是,视图被加载而没有显示默认值。您能否建议在用户更改选择之前如何显示默认值?我想从 Odata 响应中添加第一个 KOSTL 字段(如果有)。我尝试了类似下面的方法并将其放入下面的“dynamicData”函数中。但它不起作用。

在 Odata 中,使用 mCostCenter 模型通过提供登录用户作为输入来获取成本中心列表。如果结果不为空,我们需要在下拉列表中显示第一个 KOSTL 值。

var oData = this.getView().getModel("mCostCenter").getData();

sap.ui.getCore().byId("idDropdown1").setSelectedKey(OData.results[0].Kostl);

下拉:

![下拉][1]

查看代码:

var oDropdownBox1 = new sap.m.ComboBox(this.createId("idDropdown1"), {

        //items:[oItemTemplate1]

        maxWidth: "100%",

        placeholder: "Select Cost Center to continue",

        selectionChange: jQuery.proxy(oController.onDropdownPress, this.getController()),

        items: {

            path: "mCostCenter>/",

            template: new sap.ui.core.ListItem({

                key: "{mCostCenter>Kostl}",

                text: "{mCostCenter>Ltext}"

            })

        }

    }).addStyleClass("idDropdown1");

控制器代码:

onInit: function() {

    var oUserData = {};

    var y = "/sap/bc/ui2/start_up";

    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();

    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

            oUserData = JSON.parse(xmlHttp.responseText);

            //console.log(oUserData);

        }

    };

    xmlHttp.open("GET", y, false);

    xmlHttp.send(null);

    //oUserData.id = "nouser";

    this.currUser = oUserData.id;

    //new code for URL

    this.sUrl = "https://ZZZ.ZZZ.com:8443/"; //default value

    if (window.cordova && sap.Logon) {

        sap.Logon.core.getContext(jQuery.proxy(conSuccess, this), conError);

        function conSuccess(objContext) {

            if (objContext.registrationContext) {

                var context = objContext.registrationContext;

                if (context.https) {

                    this.sUrl = "https://" + context.serverHost + ":" + context.serverPort + "/";

                } else {

                    this.sUrl = "http://" + context.serverHost + ":" + context.serverPort + "/";

                }

            }

            //call data now

            if (oUserData.id == null) {

                sap.m.MessageToast.show("Error fetching user. Please contact Administrator.");

            } else {

                //sap.m.MessageToast.show("User is: "+oUserData.id);

                this.dynamicData();

            }

        }

        function conError(errorInfo) {

            //default value will be used

            //call data now with default

            if (oUserData.id == null) {

                sap.m.MessageToast.show("Error fetching user. Please contact Administrator.");

            } else {

                //sap.m.MessageToast.show("User is: "+oUserData.id);

                this.dynamicData();

            }

        }

    } else {

        if (window.location && window.location.hostname != "localhost") {

            this.sUrl = "https://" + window.location.hostname + ":" + window.location.port + "/";

        }

        //call data now

        if (oUserData.id == null) {

            sap.m.MessageToast.show("Error fetching user. Please contact Administrator.");

        } else {

            //sap.m.MessageToast.show("User is: "+oUserData.id);

            this.dynamicData();

        }

    }

    //new code URL end here

    /*if(oUserData.id == null){



        sap.m.MessageToast.show("Error fetching user. Please contact Administrator.");



    } else {



        //sap.m.MessageToast.show("User is: "+oUserData.id);



        this.dynamicData();



    }*/

},

onDropdownPress: function(evt) {

    //alert("hi");

    //alert(JSON.stringify(evt.getSource().getBindingContext()));

    //      debugger;

    var selString = null;

    //var selText = evt.getSource().getSelectedItem();

    var selString1 = evt.getSource().getSelectedKey();

    var oData = this.getView().getModel("mCostCenter").getData();

    for (var i = 0; i < oData.length; i++) {

        if (oData[i].Kostl == selString1) {

            selString = parseFloat(oData[i].AvBubdget).toFixed(2) /*+ oData[i].Ltext*/ ;

            break;

        }

    }

    var dataJSModel = new sap.ui.model.json.JSONModel();

    dataJSModel.setData({
        "selD": selString,
        "IDSel": selString1
    });

    this.getView().setModel(dataJSModel, "mCostSel");

    var jsonData = this.getView().getModel("mfullReportees").getData();

    var reportees = {

        value: []

    };

    for (var i = 0; i < jsonData.length; i++) {

        if (jsonData[i].Kostl == selString1) {

            if (reportees.value.length == 0) {

                //new Date(data.results[i]["SUBMISSION_DATE"].match(/\d+/)[0] * 1)

                if (jsonData[i].Begda && jsonData[i].Endda) {

                    var begdate = new Date(jsonData[i].Begda.match(/\d+/)[0] * 1);

                    var enddate = new Date(jsonData[i].Endda.match(/\d+/)[0] * 1);

                    var today = new Date();

                    if (today >= begdate && enddate >= today) {

                        reportees.value.push({

                            "ImUser": jsonData[i].ImUser,

                            "Pernr": jsonData[i].Pernr,

                            "Endda": jsonData[i].Endda,

                            "Begda": jsonData[i].Begda,

                            "Kostl": jsonData[i].Kostl

                        });

                    }

                } else {

                    reportees.value.push({

                        "ImUser": jsonData[i].ImUser,

                        "Pernr": jsonData[i].Pernr,

                        "Endda": jsonData[i].Endda,

                        "Begda": jsonData[i].Begda,

                        "Kostl": jsonData[i].Kostl

                    });

                }

            } else {

                for (var k = 0; k < reportees.value.length; k++) {

                    if (reportees.value[k]["Pernr"] == jsonData[i].Pernr) {

                        break;

                    } else if (k == (reportees.value.length - 1)) {

                        /*reportees.value.push({



                                    "ImUser": jsonData[i].ImUser,



                                    "Pernr": jsonData[i].Pernr,



                                    "Endda": jsonData[i].Endda,



                                    "Begda": jsonData[i].Begda,



                                    "Kostl": jsonData[i].Kostl



                                });*/

                        if (jsonData[i].Begda && jsonData[i].Endda) {

                            var begdate = new Date(jsonData[i].Begda.match(/\d+/)[0] * 1);

                            var enddate = new Date(jsonData[i].Endda.match(/\d+/)[0] * 1);

                            var today = new Date();

                            if (today >= begdate && enddate >= today) {

                                reportees.value.push({

                                    "ImUser": jsonData[i].ImUser,

                                    "Pernr": jsonData[i].Pernr,

                                    "Endda": jsonData[i].Endda,

                                    "Begda": jsonData[i].Begda,

                                    "Kostl": jsonData[i].Kostl

                                });

                            }

                        } else {

                            reportees.value.push({

                                "ImUser": jsonData[i].ImUser,

                                "Pernr": jsonData[i].Pernr,

                                "Endda": jsonData[i].Endda,

                                "Begda": jsonData[i].Begda,

                                "Kostl": jsonData[i].Kostl

                            });

                        }

                    }

                }

            }

        }

    }

    var dataJSModel1 = new sap.ui.model.json.JSONModel();

    dataJSModel1.setData(reportees);

    this.getView().setModel(dataJSModel1, "mReportees");

    var empData = this.getView().getModel("mfullResults").getData();

    var employees = {

        value: []

    };

    var reporteeslen = dataJSModel1.oData.value.length;

    for (var i = 0; i < empData.length; i++) {

        for (var j = 0; j < reporteeslen; j++) {

            if (empData[i].Pernr == dataJSModel1.oData.value[j].Pernr) {

                employees.value.push(empData[i]);

            }

        }

    }

    var dataJSModel2 = new sap.ui.model.json.JSONModel();

    dataJSModel2.setData(employees);

    this.getView().setModel(dataJSModel2, "mEmployees");

    this.onSelectionChange();

},

    getselectedfn: function(evt) {

    //      debugger;

    sap.ui.core.BusyIndicator.show(0);

    var contexts = sap.ui.getCore().byId("idTable").getSelectedContexts();

    var items = contexts.map(function(c) {

        return c.getObject();

    });

    //alert(JSON.stringify(items)); - only one item

    var ApprovedBudget = parseFloat(this.getView().getModel("mCostSel").getData().selD);

    var selectedCC = this.getView().getModel("mCostSel").getData().IDSel;

    //alert(ApprovedBudget);

    /*if(ApprovedBudget < parseFloat(items[0].Amount)){



                sap.m.MessageBox.alert("Selected amount is higher than budget", {



                    title: "Not enough budget" 



                });



                return;



            }*/

    //var sUrl = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/";

    var sUrl = this.sUrl + "sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/";

    var oHeaders = {};

    oHeaders['Authorization'] = "Basic " + btoa("nouser:nopass");

    oHeaders['X-CSRF-TOKEN'] = "FETCH";

    oHeaders['Accept'] = "application/json";

    var request =

        {

            headers: oHeaders,

            requestUri: sUrl,

            method: "GET"

        };

    OData.read(request, jQuery.proxy(readSuccessCallback, this), errorCallback);

    function readSuccessCallback(data, response)

    {

        header_xcsrf_token = response.headers['x-csrf-token'];

        //oHeaders['X-CSRF-TOKEN'] = header_xcsrf_token;

        var oHeaders = {

            "X-CSRF-TOKEN": header_xcsrf_token, //most js frameworks use this to identify ajax request.

            //  "Content-Type" : "application/atom+xml",

            //  "Content-Type" : "multipart/mixed; boundary=batch",

            //  "Accept" : "application/atom+xml"

        };

        //var sUrl = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_Action('"+this.currUser+"')";

        var sUrl = this.sUrl + "sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/";

        //ET_OT2_Action('"+this.currUser+"')

        var approvalChanges = [];

        var oModel = new sap.ui.model.odata.ODataModel(sUrl, true, null, null, oHeaders);

        //beginning for loop

        //             debugger;

        for (var i = 0; i < items.length; i++) {

            var iworkdate = new Date(items[i].Workdate.match(/\d+/)[0] * 1);

            var strWorkDate = iworkdate.toISOString();

            strWorkDate = strWorkDate.substring(0, (strWorkDate.length - 2));

            var iCreatedon = new Date(items[i].Createdon.match(/\d+/)[0] * 1);

            var strCreatedon = iCreatedon.toISOString();

            strCreatedon = strCreatedon.substring(0, (strCreatedon.length - 2));

            var data2 = {
                "ImAction": "APPROVE",

                "ImAvailableBudget": ApprovedBudget.toString(),

                "ImReason": "",

                "ImUser": items[i].ImUser,

                "Pernr": items[i].Pernr,

                "Workdate": strWorkDate,

                "Awart": items[i].Awart,

                "Counter": items[i].Counter,

                "Catshours": items[i].Catshours,

                "Meinh": items[i].Meinh,

                "Status": items[i].Status,

                "Createdby": items[i].Createdby,

                "Createdon": strCreatedon,

                "Createdat": items[i].Createdat,

                "Name": items[i].Name,

                "Atext": items[i].Atext,

                "Grade": items[i].Grade,

                "Shift": items[i].Shift,

                "Versl": items[i].Versl,

                "OtCompensation": items[i].OtCompensation,

                "Amount": items[i].Amount,

                "Waers": items[i].Waers,

                "Gjahr": items[i].Gjahr,

                "Period": items[i].Period,

                "Kostl": selectedCC

            };

            var jsonrequest = {

                "d": data2

            };

            ApprovedBudget = ApprovedBudget - parseFloat(items[i].Amount);

            //alert(JSON.stringify(items));

            var sPathApproval = "ET_OT2_Action('" + this.currUser + "')";

            //var sPathApproval = "ET_OT2_Action('abapdev4')";

            jsonrequest["d"]["__metadata"] =

            {
                "uri": sUrl,

                "type": "ZMOB_HCM_OVERTIME_SRV.ET_OT2_ACTION"
            };

            approvalChanges.push(oModel.createBatchOperation(sPathApproval, "PUT", jsonrequest));

        }

        oModel.addBatchChangeOperations(approvalChanges);

        //end of for loop

        //              debugger;

        oModel.submitBatch(jQuery.proxy(function(oData, oResponse, aErrorResponses) {

                //               debugger;

                var count = 0;

                //               if (typeof(oData.__batchResponses[0].__changeResponses[i]) === 'undefined' && typeof(oData.__batchResponses[0].response.statusCode)!=='undefined')

                //                   {

                //                   

                //                   

                //                       var message = "Please contact Administrator";

                //

                //                   

                //                  sap.m.MessageBox.alert(message, {

                //

                //                        title: "Error occurred" 

                //

                //                    });

                //                   

                //                   

                //              }

                if (aErrorResponses.length > 0) {

                    var errMsg = JSON.parse(oData.__batchResponses[0].response.body).error.message.value;

                    sap.m.MessageBox.alert(errMsg, {

                        title: "Error ocurred",

                        icon: sap.m.MessageBox.Icon.ERROR,

                    });

                } else {

                    for (var i = 0; i < items.length; i++) {

                        debugger;

                        if (typeof(oData.__batchResponses[0].__changeResponses) !== 'undefined' && oData.__batchResponses[0].__changeResponses[i].statusCode ===
                            "204")

                        {

                            count = count + 1;

                        }

                    }

                    sap.m.MessageBox.alert(count + " out of " + items.length + " records has been approved.", {

                        title: "Overtime has been approved successfully",

                        icon: sap.m.MessageBox.Icon.SUCCESS,

                    });

                }

                //               if(typeof(oData.__batchResponses[0].response.statusCode) !== 'undefined' && oData.__batchResponses[0].response.statusCode==="500"){

                //                   

                //               }

                this.dynamicData();

            }, this),

            function(e) {

                //                  debugger;

                //alert("Error occurred", err); 

                var message = "Please contact Administrator";

                if (e.response && e.response.body) {

                    // var obj =  JSON.parse(e.response.body);

                    // if(obj.error && obj.error.message && obj.error.message.value){

                    message = e.response.body; //obj.error.message.value;

                    // }

                }

                sap.ui.core.BusyIndicator.hide();

                sap.m.MessageBox.alert(message, {

                    title: "Error occurred",

                    icon: sap.m.MessageBox.Icon.ERROR

                });

            }

        );

        //              var newrequest =

        //

        //              {

        //

        //              headers : oHeaders,

        //

        //              requestUri : sUrl,

        //

        //              method : "PUT",

        //

        //              data : jsonrequest

        //

        //              };

        //

        //    

        //

        //              OData.read(newrequest,jQuery.proxy(readSuccessCallbackPOST,this), errorCallbackPOST);

    }

    function errorCallback(e)

    {

        sap.ui.core.BusyIndicator.hide();

        sap.m.MessageBox.alert("Server not reachable", {

            title: "Please try again later",

            icon: sap.m.MessageBox.Icon.ERROR

        });

    }

    function errorCallbackPOST(e)

    {

        //.indexOf("Remote function call module exception") != -1)

        var message = "Please contact Administrator";

        if (e.response && e.response.body) {

            var obj = JSON.parse(e.response.body);

            if (obj.error && obj.error.message && obj.error.message.value) {

                message = obj.error.message.value;

            }

        }

        sap.m.MessageBox.alert(message, {

            title: "Error occurred",

            icon: sap.m.MessageBox.Icon.ERROR

        });

    }

    function readSuccessCallbackPOST(data, response) {

        //var app = this.nav.getView().app;

        sap.m.MessageBox.alert("Approved", {

            title: "Overtime has been approved successfully",

            icon: sap.m.MessageBox.Icon.SUCCESS

        });

        this.dynamicData();

    }

},

dynamicData: function() {

    //var sURL = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_CostCenters?$filter=ImUser eq '"+this.currUser+"'";

    sap.ui.getCore().byId("idApprove").setEnabled(false);

    sap.ui.getCore().byId("idReject").setEnabled(false);

    var sURL = this.sUrl + "sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_CostCenters?$filter=ImUser eq '" + this.currUser + "'";

    var oHeaders = {};

    oHeaders['Authorization'] = "Basic " + btoa("nouser:nopass");

    oHeaders['contentType'] = 'application/json; charset=utf-8';

    sap.ui.core.BusyIndicator.show(0);

    var request = {

        headers: oHeaders,

        requestUri: sURL,

        method: "GET"

    };

    OData.read(request, jQuery.proxy(readSuccessCallback, this), errorCallback);

    /*sURL = " https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_ot_repSet?$filter=ImUser eq '"+this.currUser+"'";



    request['requestUri'] = sURL;



    OData.read(request, jQuery.proxy(readSuccessCallback1,this), errorCallback);



    sURL = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_ot_resultSet?$filter=ImUser eq '"+this.currUser+"'";



    request['requestUri'] = sURL;



    OData.read(request, jQuery.proxy(readSuccessCallback2,this), errorCallback);*/

    function readSuccessCallback(data, response) {

        this.boolNoCostCenter = false;

        this.boolNoReportees = false;

        this.boolNoActivePeriod = false;

        for (var i = 0; i < data.results.length; i++) {

            if (data.results[i].Kostl == "ERR_CC") {

                this.boolNoCostCenter = true;

                data.results.splice(i, 1);

                i = i - 1;

                //sap.m.MessageBox.error("There are no cost centers assigned against your user.");

                continue; //continue to remove other erros from the array

            }

            if (data.results[i].Kostl == "ERR_REP") {

                this.boolNoReportees = true;

                data.results.splice(i, 1);

                i = i - 1;

                //sap.m.MessageBox.error("There are no cost centers assigned against your user.");

                continue;

            }

            if (data.results[i].Kostl == "ERR_PAY") {

                this.boolNoActivePeriod = true;

                data.results.splice(i, 1);

                i = i - 1;

                //sap.m.MessageBox.error("There are no cost centers assigned against your user.");

                continue;

            }

        }

        if (this.boolNoCostCenter) {

            sap.ui.core.BusyIndicator.hide();

            sap.m.MessageBox.alert("There are no cost centers assigned against your user.", {

                icon: sap.m.MessageBox.Icon.WARNING

            });

        } else if (this.boolNoReportees) {

            sap.ui.core.BusyIndicator.hide();

            sap.m.MessageBox.alert("There are no reportees against your user.", {

                icon: sap.m.MessageBox.Icon.WARNING

            });

        } else if (this.boolNoActivePeriod) {

            sap.ui.core.BusyIndicator.hide();

            sap.m.MessageBox.alert("Payroll is in progress. Overtime Approval is closed for the month", {

                icon: sap.m.MessageBox.Icon.WARNING

            });

        } else {

            //go ahead and read

            //sURL = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_Reportees?$filter=ImUser eq '"+this.currUser+"'";

            sURL = this.sUrl + "sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_Reportees?$filter=ImUser eq '" + this.currUser + "'";

            request['requestUri'] = sURL;

            OData.read(request, jQuery.proxy(readSuccessCallback1, this), errorCallback);

            //sURL = "https://ZZZ.ZZZ.com:8443/sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_OvertimeRecords?$filter=ImUser eq '"+this.currUser+"'";

            sURL = this.sUrl + "sap/opu/odata/SAP/ZMOB_HCM_OVERTIME_SRV/ET_OT2_OvertimeRecords?$filter=ImUser eq '" + this.currUser + "'";

            request['requestUri'] = sURL;

            OData.read(request, jQuery.proxy(readSuccessCallback2, this), errorCallback);

        }

        //console.log("json result " + JSON.stringify(data.results));

        var dataJSModel = new sap.ui.model.json.JSONModel();

        dataJSModel.setData(data.results);

        this.getView().setModel(dataJSModel, "mCostCenter");

        //sap.ui.core.BusyIndicator.hide();

    }

    function readSuccessCallback1(data, response) {

        var dataJSModel1 = new sap.ui.model.json.JSONModel();

        dataJSModel1.setData(data.results);

        this.getView().setModel(dataJSModel1, "mfullReportees");

        //sap.ui.core.BusyIndicator.hide(); 

    }

    function readSuccessCallback2(data, response) {

        var dataJSModel1 = new sap.ui.model.json.JSONModel();

        dataJSModel1.setData(data.results);

        this.getView().setModel(dataJSModel1, "mfullResults");

        var selString1 = this.getView().byId("idDropdown1").getSelectedKey();

        if (selString1 != "" && selString1 != null) {

            var oData = this.getView().getModel("mCostCenter").getData();

            for (var i = 0; i < oData.length; i++) {

                if (oData[i].Kostl == selString1) {

                    selString = parseFloat(oData[i].AvBubdget).toFixed(2) /*+ oData[i].Ltext*/ ;

                    break;

                }

            }

            var dataJSModel = new sap.ui.model.json.JSONModel();

            dataJSModel.setData({
                "selD": selString,
                "IDSel": selString1
            });

            this.getView().setModel(dataJSModel, "mCostSel");

            var jsonData = this.getView().getModel("mfullReportees").getData();

            var reportees = {

                value: []

            };

            for (var i = 0; i < jsonData.length; i++) {

                if (jsonData[i].Kostl == selString1) {

                    if (reportees.value.length == 0) {

                        /*reportees.value.push({



                                            "ImUser": jsonData[i].ImUser,



                                            "Pernr": jsonData[i].Pernr,



                                            "Endda": jsonData[i].Endda,



                                            "Begda": jsonData[i].Begda,



                                            "Kostl": jsonData[i].Kostl



                                        });*/

                        if (jsonData[i].Begda && jsonData[i].Endda) {

                            var begdate = new Date(jsonData[i].Begda.match(/\d+/)[0] * 1);

                            var enddate = new Date(jsonData[i].Endda.match(/\d+/)[0] * 1);

                            var today = new Date();

                            if (today >= begdate && enddate >= today) {

                                reportees.value.push({

                                    "ImUser": jsonData[i].ImUser,

                                    "Pernr": jsonData[i].Pernr,

                                    "Endda": jsonData[i].Endda,

                                    "Begda": jsonData[i].Begda,

                                    "Kostl": jsonData[i].Kostl

                                });

                            }

                        } else {

                            reportees.value.push({

                                "ImUser": jsonData[i].ImUser,

                                "Pernr": jsonData[i].Pernr,

                                "Endda": jsonData[i].Endda,

                                "Begda": jsonData[i].Begda,

                                "Kostl": jsonData[i].Kostl

                            });

                        }

                    } else {

                        for (var k = 0; k < reportees.value.length; k++) {

                            if (reportees.value[k]["Pernr"] == jsonData[i].Pernr) {

                                break;

                            } else if (k == (reportees.value.length - 1)) {

                                /*reportees.value.push({



                                                    "ImUser": jsonData[i].ImUser,



                                                    "Pernr": jsonData[i].Pernr,



                                                    "Endda": jsonData[i].Endda,



                                                    "Begda": jsonData[i].Begda,



                                                    "Kostl": jsonData[i].Kostl



                                                });*/

                                if (jsonData[i].Begda && jsonData[i].Endda) {

                                    var begdate = new Date(jsonData[i].Begda.match(/\d+/)[0] * 1);

                                    var enddate = new Date(jsonData[i].Endda.match(/\d+/)[0] * 1);

                                    var today = new Date();

                                    if (today >= begdate && enddate >= today) {

                                        reportees.value.push({

                                            "ImUser": jsonData[i].ImUser,

                                            "Pernr": jsonData[i].Pernr,

                                            "Endda": jsonData[i].Endda,

                                            "Begda": jsonData[i].Begda,

                                            "Kostl": jsonData[i].Kostl

                                        });

                                    }

                                } else {

                                    reportees.value.push({

                                        "ImUser": jsonData[i].ImUser,

                                        "Pernr": jsonData[i].Pernr,

                                        "Endda": jsonData[i].Endda,

                                        "Begda": jsonData[i].Begda,

                                        "Kostl": jsonData[i].Kostl

                                    });

                                }

                            }

                        }

                    }

                }

            }

            /*for(var i=0; i<jsonData.length; i++){



                            if(jsonData[i].Kostl == selString1){



                                reportees.value.push({



                                    "ImUser": jsonData[i].ImUser,



                                    "Pernr": jsonData[i].Pernr,



                                    "Endda": jsonData[i].Endda,



                                    "Begda": jsonData[i].Begda,



                                    "Kostl": jsonData[i].Kostl



                                });



                                }



                            };*/

            var dataJSModel1 = new sap.ui.model.json.JSONModel();

            dataJSModel1.setData(reportees);

            this.getView().setModel(dataJSModel1, "mReportees");

            var empData = this.getView().getModel("mfullResults").getData();

            var employees = {

                value: []

            };

            var reporteeslen = dataJSModel1.oData.value.length;

            for (var i = 0; i < empData.length; i++) {

                for (var j = 0; j < reporteeslen; j++) {

                    if (empData[i].Pernr == dataJSModel1.oData.value[j].Pernr)

                        employees.value.push(empData[i]);

                }

            }

            var dataJSModel2 = new sap.ui.model.json.JSONModel();

            dataJSModel2.setData(employees);

            this.getView().setModel(dataJSModel2, "mEmployees");

        }

        sap.ui.core.BusyIndicator.hide();

    }

    function errorCallback(e) {

        sap.ui.core.BusyIndicator.hide();

        //alert("An error occurred ");

        if (e.response.statusCode == "400" && e.response.body.indexOf("Remote function call module exception") != -1) {

            sap.m.MessageToast.show("You have no Cost Center or Reportees assigned. Kindly check");

        } else if (e.response.statusCode == "403" || e.response.statusCode == "0") {

            sap.m.MessageToast.show("Server not reachable. Please try again later");

        } else if(e.response && e.response.body) {
            var message = "Please contact Administrator";

            var obj = JSON.parse(e.response.body);

            if (obj.error && obj.error.message && obj.error.message.value) {

                message = obj.error.message.value;

            }

            sap.m.MessageToast.show(message);

        }  else {

            sap.m.MessageToast.show("An error occurred. Please contact administrator");

        }

        console.log("An error occurred " + JSON.stringify(e));

    }

}

【问题讨论】:

    标签: sapui5


    【解决方案1】:

    在我看来,一旦你有了数据,你应该做类似的事情:

    var defaultItem = this.getView().byId("idDropdown1").getItems()[0]; 
    this.getView().byId("idDropdown1").setSelectedItem(defaultItem, true);
    

    这将获取模型中的第一个元素,然后将其设置为 ComboBox 中的选定元素。

    在本例中,它将 ComboBox 的第一个设置为选定项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多