cancel
Showing results for 
Search instead for 
Did you mean: 

How to get model data from table row

Former Member
0 Kudos

Dear all,

I've a JSON Model binded to a Table-Control. The last column of my table control contains a button. How can I access in the Press-Event the data or the row which contains the button fired the press event?

var oTable = new sap.ui.table.Table({

            id: "employeeListTable",

            columns: [

                          new sap.ui.table.Column({

                              label: new sap.ui.commons.Label({text: "Vorname"}),

                            template: new sap.ui.commons.TextView().bindProperty("text", "firstname"),

                            sortProperty: "name",

                            filterProperty: "name",

                          }),

                          new sap.ui.table.Column({

                              label: new sap.ui.commons.Label({text: "Nachname"}),

                            template: new sap.ui.commons.TextView().bindProperty("text", "lastname"),

                            sortProperty: "name",

                            filterProperty: "name",

                          }),

                          new sap.ui.table.Column({

                              label: new sap.ui.commons.Label({text: "Alter"}),

                            template: new sap.ui.commons.TextView().bindProperty("text", "age"),

                            sortProperty: "name",

                            filterProperty: "name",

                          }),

                          new sap.ui.table.Column({

                              label: new sap.ui.commons.Label({text: "Aktionen"}),

                            template: new sap.ui.commons.Button({

                                text: "Bearbeiten",

                                press: oController.onEditButtonClicked                           

                            }),

                            sortProperty: "name",

                            filterProperty: "name",

                          }),

                      ],

        });

       

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

        oModel.loadData("data/employees.json");

        oTable.setModel(oModel);

        oTable.bindRows("/employees");

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Assuming that your oController.onEditButtonClicked function accepts an oEvent argument, you can use:

oEvent.getSource().getBindingContext();

to get the binding context

dj

Former Member
0 Kudos

Thanks, I've used:

oEvent.getSource().getParent().getBindingContext()

Answers (3)

Answers (3)

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hi again

You mentioned in your request that you wanted to access the data or row that contained the button that was pressed. Well if you use the binding context I mentioned in my earlier reply, you can use it in a function (bearbeiten, or oController.onEditButtonClicked) like this:

    function bearbeiten(oEvent) {

        var oContext = oEvent.getSource().getBindingContext();

        console.log(oContext.getObject());

        console.log(oContext.getProperty("firstname"));

    }

This will give e.g.:

Object {firstname: "Joseph", lastname: "Adams", age: "16"}

Joseph

cheers

dj

Former Member
0 Kudos

Hi Adams,

how do I write a value back to the selected element for this scenario?

e.g. I read the property of age and after changing the value (e.g. from 16 to 17) I want to update that row with the new value.


updateAge: function(evt){

    var currAge= evt.getSource().getBindingContext().getProperty("age");

    var newAge = currAge+1;

    //now I want to write the newAge into the row

}

regards

Marco

Former Member
0 Kudos

Thank you, but how can I access with the index or the BindingContext the assigned model object? For example the table can be sorted or filtered?

Former Member
0 Kudos

Hi,

In table define this property,

selectionBehaviour : sap.ui.table.SelectionBehavior.Row

and add this event to table,

rowSelectionChange: function(oEvent)

{

  variable= oEvent.getParameter("rowIndex");

}

Thnaks

Former Member
0 Kudos

Hi Gerrit,

You have to find the index of your table row using 'rowIndex' .

You can use the rowSelectionChange & selectionBehaviour properties of Table to find the rowIndex.

Thanks