Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

sap.m.table Selected Row

HI,

I have created one table in SAPUI5 using XML View.

I want to get the selected record for the same and please let me know the how to edit the selected row.

I have done binding in the controller.

Below is my code for XML VIEW.

XML View.

  <mvc:View

  controllerName="xmltabelr.xml"

  xmlns="sap.m"

  xmlns:mvc="sap.ui.core.mvc"

  xmlns:u="sap.ui.unified"

  xmlns:c="sap.ui.core"

  >

  <Page

  showHeader="false"

  enableScrolling="false"

  class="sapUiContentPadding">

  <content>

<Table

  id="table"

  selectionChange="press"

  >

  <columns> <!-- sap.m.Column -->

  <Column>

  <ObjectIdentifier title="sales"/>

  </Column>

  <Column>

  <ObjectIdentifier title="amount"/>

  </Column>

  <Column>

  <ObjectIdentifier title="text"/>

  </Column>

  </columns>

</Table>

</content>

</Page>

</mvc:View>

Controller ,js

Init:function ()

var vData= [

            {assID:"EM123456", name:"Bharath S", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Male", mobile:"9934307162", rating:5},

            {assID:"EM263521", name:"Arun M", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Male", mobile:"9786721460", rating:3},

            {assID:"EM323455", name:"Anitha", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Female", mobile:"9524396759", rating:4},

            {assID:"EM237652", name:"Ganesh", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Male", mobile:"9876543210", rating:1},

            {assID:"EM398454", name:"Ajai", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Male", mobile:"9576113218", rating:4},

            {assID:"EM348092", name:"Pranav", linkText:"Cognizant Technology Solutions", href:"http://www.cognizant.com", gender:"Male", mobile:"9576113218", rating:5}

           ];

  

  var oJson = new sap.ui.model.json.JSONModel();

  oJson.setData(vData);

  this.getView().setModel(oJson);

  var oTable = this.getView().byId("table");

  var oTemplate = new sap.m.ColumnListItem({

  cells: [ new sap.m.Text({

  text: "{assID}"

  }),

  new sap.m.Text({

  text: "{assID}"

  }),

  new sap.m.Text({

  text: "{assID}"

  }),

  ] });

  oTable.setMode(sap.m.ListMode.SingleSelect);

  oTable.bindAggregation("items","/",oTemplate);

)

press: function(ooControlEvent) {

     var oSelectedItem = ooControlEvent.getParameters.listItems; 

    

     var oSelectedItem1 = ooControlEvent.getParameters.listItem;

     var oSelectedItem2 = ooControlEvent.getParameters;

     var oSelectedItem3 = ooControlEvent.getSource;

    

     alert(oSelectedItem3);

    

     var select = ooControlEvent.getSelectedItem();

  },

Please let me know how i can achieve it.

THanks

Nishant

replied

Nishant,

Actually you know what you can scrap that logic onPress (selectionChange) event of table, you can simply write like this -

onPress:function(oEvent){

var oSelectedListItem = oEvent.getParameter("listItem");            //Get Hold of List Item selected.

var oBindingContext = oSelectedListItem.getBindingContext();     //Get Hold Binding Context of Selected List Item.

var oPath = oBindingContext.getPath();              //Get Hold of Binding Context Path

var oModel = sap.ui.getCore().getModel().getProperty(oPath);          //Get the binding model.

alert(JSON.stringify(oModel));          //Alert the model Data

There are many ways to pull the selected table list item data. At high level, either you can pull the data from table control / by model.

It is highly recommended that to pull the data of selected Item using model. I have debugged the above code and see the result what it comes for each statement. Hope you will understand.



Regards,

Sai Vellanki.



1 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question