Mayank Saxena
Mayank Saxena

Reputation: 99

How to get the Selected Item in Sap.m.table and in List

I am new to SAPUI5 and I just created some applications with sap.m.table and sap.m.list. And now I am totally confused with so many ways of getting the selected items in table and list bonded with JSON Model.

So I would like to know how to :

  1. If I have one table and when user click of any row , it should display the selected row item values in alert or console. (onclick of rowevent in controller)

  2. If I have one table and when user click of any row , it should display only one of the selected row item value in alert or console. for example one of the column is "id" so only show the value of ID in alert.

  3. If I have one List and when user click of any item , it should display the selected item properties in alert or console.

4.If I have one list and when user click of any item, it should display only one of the selected item properties in alert or console. for example one of the property is "title" so only show the value of title in alert.

Regards, Mayank

Upvotes: 0

Views: 26257

Answers (1)

jpenninkhof
jpenninkhof

Reputation: 1920

It's actually not that difficult, but can be overwhelming when you just start with UI5. The key for grabbing the entry the user has just selected is the selectionChange event. When the user clicks on a line, this event is fired. You can link this event to your own function and when this function is called, you get enough parameters to check which entry the user has clicked on.

The little code snipped below does just that:

onSelectionChange: function(event) {
    alert(event.getSource().getSelectedItem().getBindingContext().getObject().Name);
    console.log(JSON.stringify(event.getSource().getSelectedItem().getBindingContext().getObject()));
}

The alert shows the name of the line that was selected, while all properties in the line are being printed to the console.

To see this in action, please have a look at this jsbin: http://jsbin.com/dayufor/1/edit?html,output

Upvotes: 3

Related Questions