Reputation: 43
I want to update a table populated with OData service. I am using this approach:
oModel.update("/Products(999)", data, {/*...*/});
I have the selected index stored in a variable (myVar1
) and I need to pass that variable to the path string. The problem is Products(999)
- this is working with the hard coded row but how to replace 999
with a variable?
Upvotes: 2
Views: 9625
Reputation: 18044
Create the path dynamically via the API createKey
from the V2 ODataModel:
const path = "/" + myODataV2Model.createKey("Products", {
// Key(s) and value(s) of that entity set
"ProductID": myVar1, // with the value 999 for example
"AnotherKeyProperty": "...",
});
myODataV2Model.update(path/*, ...*/); // applies also to .remove
Compared to concatenating strings for the path manually, createKey
offers the following advantages:
ODataUtils.formatValue
internally). E.g.: If ProductID
has the type Edm.Int64
, UI5 appends the character "l"
in the output string aligning with the OData specification: "999"
→ "999l"
encodeURIComponent
api internally). E.g.: ProductID='sp ace'
→ ProductID='sp%20ace'
Since createKey
relies on the information from the service metadata, the API should be performed after the $metadata
is loaded. For this, the promise based API metadataLoaded
can be used.
myODataV2Model.metadataLoaded(true).then(/*createKey*/);
Upvotes: 11
Reputation: 3593
with the new javascript template string syntax inside backticks "``" it would look like this:
var sIndex = "123";
oModel.update(`/Products(${sIndex})`, oData, {success: mySuccessHandler, error: myErrorHandler});
Upvotes: 0
Reputation: 1580
use the javascript concatenation operator +
to merge the value of the variable into the url string:
var sIndex = "123";
oModel.update("/Products(" + sIndex + ")", oData, {success: mySuccessHandler, error: myErrorHandler});
by the way: numeric types convert hereby automatically into strings.
Upvotes: 0