cancel
Showing results for 
Search instead for 
Did you mean: 

sap.m.table Selected Row

nishantbansal91
Active Contributor
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

saivellanki
Active Contributor

Hi Nishant,

Will this help? JS Bin - Collaborative JavaScript Debugging

Regards,

Sai Vellanki.

nishantbansal91
Active Contributor
0 Kudos

Hi Sai,

Thanks for your help, Can you please let me know how the logic behind this

I think the the importing parameter is sufficient for getting the parameter. so why we go for this.

var oModel = sap.ui.getCore().getModel().getProperty(oEvent.getSource().getBinding("items").getContexts()[oEvent.getSource().indexOfItem(oEvent.getParameters().listItem)].sPath);
alert(JSON.stringify(oModel));
}

Why we need to use the sap.ui.getCore().

Thansk

Nishant

saivellanki
Active Contributor
0 Kudos

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.



former_member182862
Active Contributor
0 Kudos

I agree that we can simplify the Sai's code like this

JS Bin - Collaborative JavaScript Debugging

Thanks

-D

0 Kudos

Perfect Solution.... Sai

Nigel_James
Active Contributor
0 Kudos

Please mark this as the correct solution . I came here because this was on the unanswered questions list and this is clearly not the case.

mrbass
Participant
0 Kudos

Thank your sir !

Answers (0)