# SAPUI5 Data Binding & Models **Source**: Official SAP SAPUI5 Documentation **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) **Last Updated**: 2025-11-21 --- ## Data Binding Overview Data binding connects UI controls to data sources, automatically synchronizing changes bidirectionally. **Key Benefits**: - Automatic UI updates when data changes - Reduced boilerplate code - Clean separation of data and presentation - Type conversion and formatting - Validation support **Binding Types**: 1. **Property Binding**: Single value (text, enabled, visible) 2. **Aggregation Binding**: Collections (table items, list items) 3. **Element Binding**: Object context 4. **Expression Binding**: Inline calculations **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) (search: binding, data-binding) --- ## Binding Modes **One-Way Binding**: - Data flows model → view only - UI updates when model changes - User input doesn't update model - Use for read-only data ```javascript // In manifest.json "models": { "": { "dataSource": "mainService", "settings": { "defaultBindingMode": "OneWay" } } } ``` **Two-Way Binding**: - Data flows model ↔ view bidirectionally - Model updates when user changes input - View updates when model changes - Use for editable forms ```javascript "defaultBindingMode": "TwoWay" ``` **One-Time Binding**: - Data loaded once at initialization - No updates after initial load - Best performance for static data ```xml ``` **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) (search: binding-mode) --- ## Model Types ### JSON Model Client-side model for JavaScript objects. Best for small datasets and local data. **Creation**: ```javascript // In controller var oModel = new JSONModel({ products: [ { id: 1, name: "Product 1", price: 100 }, { id: 2, name: "Product 2", price: 200 } ], selectedProduct: null }); this.getView().setModel(oModel); ``` **From File**: ```javascript var oModel = new JSONModel(); oModel.loadData("model/data.json"); this.getView().setModel(oModel); ``` **Usage**: ```xml ``` **Key Methods**: - `setData(oData)`: Set complete data - `setProperty(sPath, oValue)`: Set single property - `getProperty(sPath)`: Get property value - `loadData(sURL)`: Load from URL **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) (search: json-model) --- ### OData V2 Model Server-side model for OData v2 services. Automatic CRUD operations. **Creation**: ```javascript // In manifest.json { "sap.app": { "dataSources": { "mainService": { "uri": "/sap/opu/odata/sap/SERVICE_SRV/", "type": "OData", "settings": { "odataVersion": "2.0", "localUri": "localService/metadata.xml" } } } }, "sap.ui5": { "models": { "": { "dataSource": "mainService", "settings": { "defaultBindingMode": "TwoWay", "defaultCountMode": "Inline", "useBatch": true } } } } } ``` **Reading Data**: ```javascript // Simple read this.getView().getModel().read("/Products", { success: function(oData) { console.log(oData); }, error: function(oError) { MessageBox.error("Failed to load data"); } }); // With filters and sorters this.getView().getModel().read("/Products", { filters: [new Filter("Price", FilterOperator.GT, 100)], sorters: [new Sorter("Name", false)], urlParameters: { "$expand": "Category" }, success: function(oData) { console.log(oData); } }); ``` **Creating Entries**: ```javascript var oModel = this.getView().getModel(); oModel.create("/Products", { Name: "New Product", Price: 150, CategoryID: 1 }, { success: function() { MessageToast.show("Product created"); }, error: function(oError) { MessageBox.error("Failed to create product"); } }); ``` **Updating Entries**: ```javascript var oModel = this.getView().getModel(); oModel.update("/Products(1)", { Price: 200 }, { success: function() { MessageToast.show("Product updated"); } }); ``` **Deleting Entries**: ```javascript oModel.remove("/Products(1)", { success: function() { MessageToast.show("Product deleted"); } }); ``` **Batch Requests**: ```javascript oModel.setUseBatch(true); oModel.setDeferredGroups(["myGroup"]); // Add to batch oModel.create("/Products", oData, { groupId: "myGroup" }); oModel.create("/Products", oData2, { groupId: "myGroup" }); // Submit batch oModel.submitChanges({ groupId: "myGroup", success: function() { MessageToast.show("Batch successful"); } }); ``` **Key Settings**: - `useBatch`: Enable batch requests - `defaultCountMode`: How to get counts (Inline, Request, None) - `refreshAfterChange`: Auto-refresh after updates - `defaultBindingMode`: One-way or two-way **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) (search: odata-v2-model) --- ### OData V4 Model Modern OData v4 model with improved performance and features. **Creation**: ```javascript // In manifest.json { "sap.app": { "dataSources": { "mainService": { "uri": "/sap/opu/odata4/sap/service/srvd/sap/api/0001/", "type": "OData", "settings": { "odataVersion": "4.0" } } } }, "sap.ui5": { "models": { "": { "dataSource": "mainService", "settings": { "synchronizationMode": "None", "operationMode": "Server", "autoExpandSelect": true, "earlyRequests": true } } } } } ``` **Key Differences from V2**: - Server-side operations (filter, sort, page) - Automatic $expand and $select - Better performance - Stricter adherence to OData standard - No client-side models **Reading with List Binding**: ```xml ``` **Creating Entries**: ```javascript var oListBinding = this.byId("table").getBinding("items"); var oContext = oListBinding.create({ Name: "New Product", Price: 150 }); // Save oContext.created().then(function() { MessageToast.show("Product created"); }); ``` **Updating**: ```javascript var oContext = this.byId("table").getSelectedItem().getBindingContext(); oContext.setProperty("Price", 200); // Save changes oContext.getModel().submitBatch("$auto").then(function() { MessageToast.show("Updated"); }); ``` **Deleting**: ```javascript var oContext = this.byId("table").getSelectedItem().getBindingContext(); oContext.delete("$auto").then(function() { MessageToast.show("Deleted"); }); ``` **Documentation**: [https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials](https://github.com/SAP-docs/sapui5/tree/main/docs/04_Essentials) (search: odata-v4-model) --- ### Resource Model (i18n) Model for internationalization texts. **Setup**: ```javascript // In manifest.json { "sap.ui5": { "models": { "i18n": { "type": "sap.ui.model.resource.ResourceModel", "settings": { "bundleName": "com.mycompany.myapp.i18n.i18n", "supportedLocales": ["en", "de", "fr"], "fallbackLocale": "en" } } } } } ``` **i18n.properties**: ```properties appTitle=My Application appDescription=A sample SAPUI5 application # Buttons btnSave=Save btnCancel=Cancel btnDelete=Delete # Messages msgSaveSuccess=Data saved successfully msgDeleteConfirm=Do you want to delete this item? # Placeholders with parameters msgItemCount=You have {0} items selected msgWelcome=Welcome, {0}! ``` **Usage in XML**: ```xml
``` **With Parameters**: ```xml ``` --- ### Element Binding Sets binding context for entire control: ```xml ``` ```javascript // In controller onProductSelect: function(oEvent) { var oItem = oEvent.getParameter("listItem"); var sPath = oItem.getBindingContext().getPath(); this.byId("detailPanel").bindElement({ path: sPath, parameters: { expand: "Category" } }); } ``` --- ### Expression Binding Inline calculations without formatter: ```xml