Reputation: 103
I have the following json data:
{
"medios":[
{
"Medio":"Cheque",
"Key":"5"
},
{
"Medio":"Transferencia Bancaria",
"Key":"6"
}
]
}
And I bind this data using a json model:
var oModelTest = new sap.ui.model.json.JSONModel();
var MediosPagoPromesa = [];
var MedioObj = {
Medio: proMedioPagoCP, //a variable I fill inside a loop
Key: i.toString() //because it is inside a loop
}
MediosPagoPromesa.push(MedioObj);
oModelTest.setData({
'medios': MediosPagoPromesa
});
sap.ui.getCore().setModel(oModelTest, "Pagos");
into a MultiComboBox:
var test = sap.ui.getCore().getModel("Pagos");
var oMultiSelect = new sap.m.MultiComboBox({
items: {
path: "/medios",
template: new sap.ui.core.ListItem({
key: '{Key}',
text: '{Medio}'
}),
templateShareable: true
},
selectedKeys: ?, //here is my problem
});
oMultiSelect.setModel(test);
What I don't know, is how can I set as selected Items, all the items I am binding in the MultiComboBox, So they can be automatically displayed as selected even from the first time, any idea idea how can I achieve this?
Upvotes: 2
Views: 7535
Reputation: 198
add a new array of the selected element filed in the the loop
var oModelTest = new sap.ui.model.json.JSONModel();
var MediosPagoPromesa = [];
var selected = [];
var MedioObj = {
Medio: proMedioPagoCP, //I variable I fill inside a loop
Key: i.toString() //because it is inside a loop
}
selected.push(i.toString); //inside the loop
MediosPagoPromesa.push(MedioObj);
oModelTest.setData({
'medios': MediosPagoPromesa,
'selected' : selected
});
sap.ui.getCore().setModel(oModelTest, "Pagos");
In MultiComBox use bindProperty to bind the selectedKeys property
var test = sap.ui.getCore().getModel("Pagos");
var oMultiSelect = new sap.m.MultiComboBox({
items: {
path: "/medios",
template: new sap.ui.core.ListItem({
key: '{Key}',
text: '{Medio}'
}),
templateShareable: true
},
});
oMultiSelect.bindProperty("selectedKeys", "/selected");
oMultiSelect.setModel(test);
Here is jsbin with clear example : https://jsbin.com/murural/1/edit?html,js,output
Upvotes: 2