0

I want to bind JSON Data to view I have tried as below:

XML:

<m:Select id="Employee" items="{Employee>/EmployeeList}">
<c:Item key="{Employee>key}" text="{Employee>value}" />
<m:layoutData>
    <l:GridData span="L2 M2 S2"/>
</m:layoutData>

This is how my JSON data is :

 var xyz = {
    "Employee": {

        "EmployeeList": [{
                "key": "ram",
                "value": "ram"
            },
            {
                "key": "raj",
                "value": "raj"
            },
            {
                "key": "rani",
                "value": "rani"
            }
        ]
    }
}
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(xyz);
this.getView().setModel(oModel);

I have a select box in view I want to show the employee names as dropdowns in view page.How to bind this XML.Thanks in advance

6
  • It looks to me that you have forgotten the '>' after the model name in your binding. "{Employee/EmployeeList}" should be "{Employee>/EmployeeList}" Commented Mar 19, 2020 at 7:47
  • sorry it was by mistaken i have kept but dropdown values are not showing in xml view Commented Mar 19, 2020 at 7:56
  • Do you get the right amount of empty items in your dropdown? Commented Mar 19, 2020 at 7:58
  • yes i got but the values are not displaying Commented Mar 19, 2020 at 7:59
  • It's because you have to get the values from the model. <c:Item key="{Employee>key}" text="{Employee>Value}" /> should work. Commented Mar 19, 2020 at 8:01

2 Answers 2

5

There are multiple wrong assumptions:

items="{Employee>/EmployeeList}"

Here you assume that you have a model with the name Employee which has a top-level attribute EmployeeList.

In fact you have a model with no name and with a top-level attribute Employee.

You have the choice to:

  • change your binding
  • change your model

Option A: Change your binding:

This is your option if you cannot change the model (because it comes from your backend that way).

Remove the model name from your binding (since your model does not have a name). Build the correct path to your list. At the top of xyz there is a property Employee which is an object that has the property EmployeeList.

<m:Select id="Employee" items="{/Employee/EmployeeList}">
    <c:Item key="{key}" text="{value}" />
    <m:layoutData>
        <l:GridData span="L2 M2 S2"/>
    </m:layoutData>
</m:Select>

Option B: Change your model

If you are not satisfied with your model structure and think that your binding makes sense, you can also alter the model.

First, change your object so that the EmployeeList is your top level structure:

 var xyz={   
   "EmployeeList":[  
      {  
        "key":"ram",
        "value":"ram"
      },
      {  
        "key":"raj",
        "value":"raj"
      },
      {  
        "key":"rani",
        "value":"rani"
      }
   ]
}

When setting your model to your view, also provide the name that you expect in the binding:

this.getView().setModel(oModel, "Employee");
Sign up to request clarification or add additional context in comments.

1 Comment

I have changes the binding and got the values thankyou
1

Ok, you are setting your model on your view as unnamed model. So, the correct binding is:

<m:Select id="Employee" items="{/Employee/EmployeeList}">
    <c:Item key="{key}" text="{value}" />
         <m:layoutData>
                <l:GridData span="L2 M2 S2"/>
         </m:layoutData>
</m:Select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.