Use insertItem after bindAggregation in Sap.m.Select

1.6k Views Asked by At

Is my first time posting here.

I have a sap.m.Select that shows the Years from oDataModel using "bindAggregation" method.

My idea is to create a extra Item in the Select with text: "All Values" and Key: "*", So I tried to use the "insertItem" after the "bindAggregation", but the item that I created didnt appear in the Select list of items, only the Years from oDataModel.

Here is the code:

var yearSelectBox = this.getView().byId("idYearSelectBox");
yearSelectBox.setModel(new sap.ui.model.odata.ODataModel("../../../ui/WebContent/Kpi/services/dates.xsodata", true));
yearSelectBox.bindAggregation("items", "/Years", new sap.ui.core.Item({
            key: "{YEAR}",
            text: "{YEAR}"
        }),0);
yearSelectBox.insertItem(new sap.ui.core.Item({
            key: "*",
            text: "All Values"
        }));

I tried to use the insertItem before the bindAggregation, to use itemIndex = -1, 0, 20, but nothing changed.

1

There are 1 best solutions below

0
On BEST ANSWER

Unfortunately what you're trying to achieve - while making sense to you - doesn't make sense in the context of binding. When you bind a data model to a control, the control then bases all of its rendering on that model. Additionally, any changes via. the control are pushed back to the model - this keeps the model and control in sync. Thus you cannot simply add another item to the aggregation once a binding is in place. The binding controls what items are added (updated and removed), not programmatic interference. You have two simply options (perhaps others too): read your model data into a JSON model, including your All Values dropdown entry, then bind the dropdown control to that JSON Model, or add the All Values entry to the underlying OData database table so it shows up in the dropdown.