Get ColumnListItem index sapui5 - sapui5

How can I get index of pressed ColumnListItem? I want to get and pass to controller method.
View code:
var oTable = new sap.m.Table({
id: "Countries",
mode: sap.m.ListMode.None,
columns: [ new sap.m.Column({
width: "1em",
header: new sap.m.Label({
text: "Name"
})
})
]
});
var template = new sap.m.ColumnListItem({
id: "first_template",
type: "Navigation",
visible: true,
selected: true,
cells: [ new sap.m.Label({
text: "{name}"
})
],
press: [oController.pressListMethod]
});
oTable.bindItems("/eventos", template, null, null);
oPage.addContent(oTable);
Controller code:
pressListMethod: function(index){
var oData = sap.ui.getCore().getModel().getProperty("/eventos/"+index+"/name");
alert(oData);
}

You shouldn´t rely on the index since the index in the table can differ from the index in your model (e.g. due to filtering and sorting).
You can read the bindingContext of the pressed ListItem like this:
pressListMethod: function(event){
var bindingContext = event.getSource().getBindingContext();
}
The bindingContext is an artificial object containing the related model and a path of the object within the model.
You can then read properties of your object like this:
var name = bindingContext.getProperty("name");
To get the whole object you can do it like this:
var myObject = bindingContext.getObject();

To get exact value of product
SelectedRowContext.getObject('PRODUCT_ID')
To get Name of product
SelectedRowContext.getObject('NAME')

Related

Call Dynamic Table values Onselect from listItems

I would like to know if possible to get dynamic table values onSelect from listItems.
controller.js
onPress : function(oEvent){
var oSelectedItem = oEvent.getSource();
var oContext = oSelectedItem.getBindingContext("invoice");
var sPath = oContext.getPath();
var oListItem= this.getView().byId("BoM");
oListItem.bindItems({
path : "invoice>/ProductHeadSet('12345')/ProductHead2BOM",
template : new sap.m.ColumnListItem({
cells: [
new sap.m.Text({
text: "{invoice>Material}"
}),
new sap.m.Text({
text: "{invoice>Component}"
}),
new sap.m.Text({
text: "{invoice>Brand}"
})
]
})
});
}
});
Above is my controller, when i make onPress, i could receive the values from "12345". But when i try to make an dynamic onPress by removing (12345)"invoice>/ProductHeadSet/ProductHead2BOM"". It throws me an error like this "The request URI is invalid. The ProductHeadSet segment refers to an entity set and not to a single entity".
Thanks and Regards.
Your answer is right there
"The request URI is invalid. The ProductHeadSet segment refers to an entity set and not to a single entity".
Int the below code, the number within the brackets help identify which index in the array should be bound to the list.
invoice>/ProductHeadSet('12345')/ProductHead2BOM Without the number, you are trying to bind the entire array to oListItem.
EDIT
This is a little difficult to answer without seeing the model and bindings, But instead of doing path : "invoice>/ProductHeadSet('12345')/ProductHead2BOM"
Do
path : "invoice>"+sPath+"/ProductHead2BOM"

add input row at the beginning ui5

I'm having problems concerning adding a row with input field to add item to the table. I'm trying to use this example
addEntry : function(oEvent) {
var path = oEvent.getSource().getBindingContext().getPath();
var obj = {
fname: null,
lname: null,
desc: null,
createNew: false,
removeNew: false,
saveNew: true
};
var oModel = this.getView().getModel();
oModel.setProperty(path, obj);
},
The only difference that i want is for the row to be visible from the start (without the + icon) so that the user can directly add fields and when OK is clicked, a new row needs to be added.
Thank you
You should remove the addEntry() function as it is just triggering from the Add Icon and instead create the object on your onInit() function.
It will look like this:
onInit : function() {
var dummyData = [{"fname": "",
"lname": "",
"desc": "",
"removeNew": false,
"saveNew": true}];
var oModel = new sap.ui.model.json.JSONModel({data : dummyData});
this.getView().setModel(oModel);
}
When you click on the save button, the saveEntry() function will be triggered and the new entry will be pushed to the model.
A new form will show up by calling the addEmptyObject() function at the end of it.
Here is a working example:
https://jsbin.com/wutefaz/edit?html,js,output

Add specific text in table binding value in sapui5 using sap.m.Table

I am trying to develop an SAPUI5 app but I can't add a specific text before the value in a table column.
onInit : function() {
var oModel = new sap.ui.model.json.JSONModel('add json file ');
sap.ui.getCore().setModel(oModel,'products');
}
In the View I am creating a table and binding all records:
var oTable = new sap.m.Table("productsTable",{
inset: true,
columns: [
//image
new sap.m.Column({
hAlign: "Left",
width: "100px",
demandPopin: true,
popinDisplay: "Block",
minScreenWidth: sap.m.ScreenSize.Medium
}),
]
});
var oTemplate = new sap.m.ColumnListItem({
type: sap.m.ListType.Active,
cells: [
new sap.m.Text({
text: "Title :{products>description} ",
//visible :false,
}),
]
});
oTable.bindAggregation("items","products>App",oTemplate); // Here bind all record
return new sap.m.Page({
title: "App Name",
content: [oTable],
showNavButton: true,
navButtonPress: function() {
oController.navigation();
},
footer: new sap.m.Bar({
contentLeft: [
new sap.m.Text({text: "Smart",})
]
}),
});
My desired output is:
But it's displayed this way:
As #Qualiture said in the comment this looks like you need to enable the complex binding syntax.
You can do that by setting the binding syntax mode explicitly using
data-sap-ui-bindingSyntax="complex" or implicitly by specifying the compatibility version of 1.26 or edge: data-sap-ui-compatversion="edge".
Try add slash (/) caracter before the property.
Sample:
"Title :{products>/description} "
OR
Maybe your binding is incorrect, try this
...
text: { path: "{products>/description}", //with or without slash (/)
formatter: function(desc) {
return "Title" + desc;
}
}
...

How to add combo box events in sap Ui5

I am using combo box in a table having two fields as male and female and if i change that value to be stored in the JSON model data..How to use an event to it..
The following is my code
oTable.addColumn(new sap.ui.table.Column( {
label: new sap.ui.commons.Label({text: "Gender"}),
template: new sap.ui.commons.ComboBox(
{items: [new sap.ui.core.ListItem({text: "Female"}),
new sap.ui.core.ListItem({text: "Male"})]}).bindProperty("value","gender"),
sortProperty: "gender",
filterProperty: "gender",
enabled : true,
change: function(oEvent){
sap.ui.getCore().byId("gender").setValue(oEvent.oSource.getSelectedItemId());
},
width: "75px"
}));
function change(oEvent){
var bEnabled = oEvent.getParameter("enabled");
// modify the enabled property value in the model to reflect changes
oModel.setData({enabled: bEnabled}, true); // true means merge the data instead of replacing
};
But it is not reflecting the values..please correct the code.
you should move your `change` function to the combobox, currently is is attached to the column (which does not have an event like that).
you can put your `bindProperty`-call to the constructor
in the event-function you have to get the model and change its value.
new sap.ui.commons.ComboBox({
value: "{gender}",
items: [
new sap.ui.core.ListItem({
text: "Female"
}), new sap.ui.core.ListItem({
text: "Male"
})
],
change: function(oEvent) {
var newValue = oEvent.getParameter("newValue");
var bindingPath = this.getBindingPath("value");
this.getModel().setProperty(bindingPath, newValue);
}
})

sap.ui.commons.Link doesn't work in Binding

I have a table when I try to bind the column of this table to a json data. It works in one of my tables and in other tables it doesn't show anything. I checked content and the way I get the json data and it is exactly the same in all tables.
oTable.addColumn(new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Names"}),
template:
new sap.ui.commons.Link({
text:"{name}",
})
}));
the table that shows the content sits in shell and other tables that show nothing sit inside sap.ui.ux3.ThingGroup. I am wondering if the reason that I don't see the binding is because of ThingGroup or not? because as soon as I change the sap.ui.commons.Link to sap.ui.commons.TextView it works.
The cut down version of my code is:
addLayerTable: function(){
var jsonData1 = {
"layers" : []
};
$.getJSON(URI, {}).done(function(data) {
if(data.layers !== undefined){
$.each(data.layers, function(i, field) {
jsonData1.layers.push({
"name" : field
});
});
}
layerDetailPage(jsonData1);
}).fail(function() {
alert("Error while reading layers Data or layers do not exist");
});
var oTable = new sap.ui.table.Table({
rows:{
path:"/layers",
},
width: "900px",
visibleRowCount: 10,
navigationMode : sap.ui.table.NavigationMode.Paginator
});
oTable.addColumn(new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Names"}),
template:
new sap.ui.commons.Label({
text:"{name}"
})
}));
function layerDetailPage(layerData){
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(layerData);
sap.ui.getCore().setModel(oModel, "layerModel");
oTable.setModel(oModel);
};
var oFC1 = new sap.ui.ux3.ThingGroup();
oFC1.addContent(oTable);
return oFC1;
}
As soon as I change the sap.ui.commons.Label to TextView, names become visible. but at the moment nothing. Funny enough if I add a press function to my table and try to get the name of the row in the table it works although it is not visible.