cancel
Showing results for 
Search instead for 
Did you mean: 

How to bind selectkeys which have items from a different model?

0 Kudos

I build a universal model for all dropdown values:

    var oCaseListDropdownModel = new sap.ui.model.json.JSONModel();
    sap.ui.getCore().setModel(oCaseListDropdownModel, "CaseListDropdownModel");

Then later, within a service call, I put the values in the model:

    oCaseListDropdownModel.setData(data.d);
    oCaseListDropdownModel.refresh(true);

I have a view that has these controls:

    <f:FormElement label="Status" id="__caseInfoStatusForm">
    <f:fields>
            <Input id="__caseInfoStatusDesc" value="{/Status}" editable="false"/>
        </f:fields>
    </f:FormElement>
    <f:FormElement label="Site" id="caseCreateelement11">
        <ActionSelect id="caseCreateStationId" selectedKey="{/StationId}" items="{path: '/results', filters: [ { path: 'Field', operator: 'EQ', value1: 'Site' } ] }" enabled="true">
            <items>
                <sap.ui.core:ListItem text="{Value}" key="{Key}" />
            </items>
        </ActionSelect>
    </f:FormElement>

In the controller for that view, I build the model like this:

    oCaseCreateModel.setData(
                    {
                    "Status" : "E0001", 
                    "StationId" : ""
                    }
        );
    oCaseCreateModel.refresh(true);
    this.getView().setModel(oCaseCreateModel);


Then, for the dropdown, I do this:

    oCaseListDropdownModel = sap.ui.getCore().getModel("CaseListDropdownModel");
    var thisCaseCreate = this;
    thisCaseCreate.getView().byId("caseCreateStationId").setModel(oCaseListDropdownModel);

Later, when it is time to call the POST service, I get the model from the view:

    oCaseCreateViewModel = thisCaseCreate.getView().getModel("undefined");

But when I do that, it does not bring in the key value that the user selected, and instead brings in the key value that was set initially, when the model was populated.
I have found that, for each dropdown, I have to do this:


oCaseCreateViewModel.setProperty("/StationId", thisCaseCreate.getView().byId("caseCreateStationId").getSelectedKey());

Why does the selectedKey for /StationId not get updated in the model automatically?

Accepted Solutions (0)

Answers (2)

Answers (2)

junwu
Active Contributor
0 Kudos
this.getView().setModel(oCaseCreateModel);

 thisCaseCreate.getView().byId("caseCreateStationId").setModel(oCaseListDropdownModel);

you set unnamed model twice. so the second one will be in charge.

so the selected key goes to oCaseListDropdownModel, not the oCaseCreateViewModel
0 Kudos

Thanks for responding.

I wonder if there is a better way to go about doing this.

junwu
Active Contributor
0 Kudos

do what?.............

junwu
Active Contributor
0 Kudos
  oCaseCreateViewModel = thisCaseCreate.getView().getModel("undefined");

"undefined"??