My file controller.js,
Now want to bind json object in array using sapui 5. I am try below code for it.
onInit: function() {
var elementArray=['ID','Name','Description','ReleaseDate','DiscontinuedDate','Rating','Price'];
var oModel = new sap.ui.model.json.JSONModel(elementArray);
sap.ui.getCore().setModel(oModel,'fieldArray');
// var oModel = new sap.ui.model.json.JSONModel('./smartappall/door.josn');
// sap.ui.getCore().setModel(oModel,'approcords');
},
view.js
var elementArray= bind fieldArray json model here
I'm not sure what you are trying to achieve, but since your model only contains the array, in your view you can do the following:
var elementArray = sap.ui.getCore().getModel("fieldArray").getData();
(EDITED: Forgot the getData() part...)
But generally, you don't store objects or arrays in a dedicated model, but rather have one model where you store them in separate properties. In that case, you can do:
In controller:
sap.ui.getCore().getModel("fieldArray").setProperty("/pathToYourArray", elementArray");
In view:
var elementArray = sap.ui.getCore().getModel("fieldArray").getProperty("/pathToYourArray");
The JSONModel data must be a plain javascript object. IE not a javascript array.
You can either follow the previous answer and set your array as a property of the JSONModel, or modify your initial array to wrap it inside an object :
onInit: function() {
var data = { elementArray: [
'ID',
'Name',
'Description',
'ReleaseDate',
'DiscontinuedDate',
'Rating',
'Price'
]};
var oModel = new sap.ui.model.json.JSONModel(data);
sap.ui.getCore().setModel(oModel,'fieldArray');
},
Then you can bind your view to {fieldArray>/elementArray}
Related
I was trying to figure out how to bind a simple form in UI5 (not a list) so I can submit to my oData backend . How do I do that with a simple form?
Here is my controller code (if I were to do in a List) - But I have no clue how to deal with a Form:
var oList = this.byId("addapp"),
oBinding = oList.getBinding("items"),
// Create a new entry through the table's list binding
oContext = oBinding.create({
"application_id": 0,
"product_name_short": 'Test',
});
This is what I have tried so far, but I got undefined value:
var x =this.getView().byId("addapp").getBinding("items");
If you have set a model to your view, you can bind an existing object to a view using Element.bindElement:
this.getView().byId("addapp").bindElement("/path/to/your/object");
If you want to use a temporary model (to use the data once you click "add" or something similar):
onInit: function () {
// Initial values of the form
this.getView().setModel(new sap.ui.model.json.JSONModel({
property1: "",
property2: "",
property3: ""
}), "newEntry");
// Bind the form to the json model
this.getView().byId("addapp").bindElement("newEntry>/");
},
onAddButtonClick: function() {
// Get the form values
var newEntry = this.getView().getModel("newEntry").getData();
// Use the OData model to create a new entity
var oDataModel = ...;
oDataModel.create("/path/to/your/entityset", newEntry, {
success: function() {
// entity was created
}
});
}
This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 4 years ago.
I'm new to SAPUI5 and I'm having problems...
When I use a read function to get a value from an OData service, I'm trying to use another read function inside the success function, using a filter with the value that I obtained from the first read.
Is this even possible?
Up to now, it just seems like it reads successfully, but then it doesn't execute the next read.
var filters = new Array();
var first_Filter = new sap.ui.model.Filter({
path: "userId",
operator: sap.ui.model.FilterOperator.EQ,
value1: userId
});
filters.push(first_Filter);
this.getOwnerComponent().getModel().read("/users", {
filters: la_filters,
success: function(oData, response) {
var data = oData.results[0];
var jobid = data.jobId;
var filters2 = new Array();
var second_Filter2 = new sap.ui.model.Filter({
path: "idJob",
operator: sap.ui.model.FilterOperator.EQ,
value1: jobid
});
filters2.push(second_Filter2);
this.getOwnerComponent().getModel().read("/jobs", {
filters: la_filters2,
success: function(oData2) {
// read odata ,get value and pass it on...
}
});
}
});
the this inside the second read is not the correct one.
save a reference to this (outside the first read) like var that = this; and use it to make the second read like
that.getOwnerComponent().getModel().read("/jobs", {
filters: la_filters2,
success: function(oData2) {
// read odata ,get value and pass it on...
}
});
My issue is that I have a default model specified in my manifest.json file as "".
However, I'm also setting up a view model in my controller.
var oModel = new JSONModel({
questions: [{
order: 1,
title: "",
criteria: "",
}]
});
this.getView().setModel(oModel);
How do I restore set the view model back to the default model?
Is it simply a case of
this.getView().setModel("");
I'm asking because the default model is the service that contains all my read/writes.
You should set named models after setting the default model.So your code should be changed like
var oModel = new JSONModel({
questions: [{
order: 1,
title: "",
criteria: "",
}]
});
this.getView().setModel(oModel,"newModelName");
Now you can get default model by
this.getView().getModel()
and named json model by
this.getView().getModel("newModelName");
The solution was in the way the additional model was initialised and accessed, but naming it and referring to it as 'viewmodel'
This way, the default model can still be setup using
oModel = this.getView().getModel();
In the Controller:
this.getView().setModel(oModel,"viewmodel");
var oContext = oModel.createBindingContext("/questions/0/");
this.getView().setBindingContext(oContext,"viewmodel");
In the view :
title="Question {viewmodel>order}"
I have a userTasteSchema, with a dictionary field favorites composed by
favoriteSchema objects. When I save or update a userTaste, I want to validate that the elements of the dictionary are valid favorite objects.
Is it possible?
thank you
var userTasteSchema = new Schema(
{
favorites : {type: { /* dictionary of favorites */ }, default:{} }
});
var favoriteSchema = new Schema(
{
name : {type:{String}}
});
You have to change your model declaration. According to the docs your code should look like:
var userTasteSchema = new Schema(
{
favorites : [ favoriteSchema ]
});
var favoriteSchema = new Schema(
{
name : {type:String}
});
And that's pretty much it. When you save your parent document, UserTaste, your children validations are run as well. Here is the reference
Validation is asynchronously recursive; when you call Model#save,
sub-document validation is executed as well. If an error occurs, your
Model#save callback receives it
<fb:FilterBar reset="onReset" search="onSearch">
<fb:filterItems>
<fb:FilterItem name="dateRange" label="Date Range" mandatory="true">
<fb:control>
<DateRangeSelection id="dateRange" />
</fb:control>
</fb:FilterItem>
</fb:filterItems>
</fb:FilterBar>
In my onSearch method I get the binding and then use this to set the table's filters:
onSearch : function(oEvt) {
var oFilterBar = oEvt.getSource(), aFilters = [], oParameters;
aFilters = oFilterBar.getFilters();
oParameters = oFilterBar.getParameters();
this.doReload(aFilters, oParameters);
}
First issue here is I get 'undefined is not a function' on the call to oFilterBar.getFilters(). oFilterBar itself is not undefined, so that looks OK at first sight. My view includes xmlns:fb="sap.ui.comp.filterbar".
doReload : function(aFilters, oParameters) {
var oTable = this.byId("clockHistoryTable");
var oBinding = oTable.getBinding("items");
oBinding.filter(aFilters);
}
I have a hard time connecting the two and figure out how to use the selected range as a filter. My model for the table has a list of items and each item has a field called 'date' which can be used to filter the items based on the selected range. But this field has it's own specific date format yyyymmdd.
Side question, best way to set the default from and to dates for the DateRangeSelection control would be in the controller's init function?
Update
After Allen's reponse I did some modifications:
doReload : function(aFilters) {
var oTable = this.byId("clockHistoryTable");
var oBinding = oTable.getBinding("items");
oBinding.filter(aFilters);
},
onReset : function(oEvent) {
this.doReload();
},
onSearch : function(oEvt) {
var oFilterBar = oEvt.getSource(), aFilters = [];
aFilters = oEvt.getParameter("selectionSet");
this.doReload(aFilters);
},
It runs without any errors but when I select a date or range it filters out everything and the table is empty.
There is no method called getFilters for FilterBar, only for SmartFilterBar. You need to construct your own Filters based on oEvent.getParameter("selectionSet").
You should set displayFormat="yyyy/MM/dd" of DateRangeSelection too.
Yes, you should set default from and to dates of DateRangeSelection in the onInit() function.