cancel
Showing results for 
Search instead for 
Did you mean: 

UI5 Table getRows() returns rows array size below the visibleRowCount limit

Former Member

Hi,

When my table rows # exceed the numbers of visibleRowCount (be the rows then available thru scrollbar or navigation pages), i can't access programmatically the rows objects selected (and no access to model, binding path, etc...);

Indeed, it is the API Table.getRows() api that only returns the first # of rows < visibleRowCount, whatever the current table view status is (scrolled, paginated, or not)

I've written a short sample on JSBin here

[...]

  var idx = oTable.getSelectedIndex();

  var row = oTable.getRows()[idx];

  //row returns undefined if idx > visibleRowCount

   [...]

I'm using version 1.24.4.

Does anybody have a clue of what i'm missing here to get access to this row objects  ?

Is it an expected behaviour ?

Note that, as workarounds, i tried:

a) access these by using

Table.attachRowSelectionChange{function (oEvent)}

oEvent.mParameters.rowContext.sPath

oEvent.mParameters.rowContext.oModel

etc...

But that is very cumbersome as i'd have to manage my own maps for multiple selections & selecting/unselecting...

b) don't use the rows to get back to the model, directly hardcode mybindingPath="/model/" + idx;

But that does not match if the index list is altered thru a table sorting for example.

Thanks a lot for any clue!

Marc

Accepted Solutions (1)

Accepted Solutions (1)

Qualiture
Active Contributor

Why do you want the rows anyway? Isn't it just more convenient to get the table's binding context (i.e. the bound objects for the rows) based on the selected rows?

As an added benefit, you will also have access to fields which are not displayed in your table

Former Member
0 Kudos

Thanks Robin for your help,

Retrieving the binding context is precisely what i want to achieve;

See my answer to Maximilian above for what i'm trying to achieve...

Qualiture
Active Contributor

Hi Marc,

I think the following will be *much* easier for you, and it works regardless of client-side sorting/filtering:


var aSelectedIndices = oTable.getSelectedIndices();

for (var i=0; i<aSelectedIndices.length; i++) {

    var oData = oTable.getContextByIndex(aSelectedIndices[i]);

    // do whatever you want with the current oData object

}

EDIT: In your case, if you need the ID of the current object, simply use this in the for-loop:


var myId = oData.getObject().id;

Message was edited by: Robin van het Hof

Former Member
0 Kudos

Wonderfull !

This was exactly the API I was looking for, thanks so much !!

Was there a specific sample which showcased this ? I've not seen this until your post...

And, as a sidenote, i really wonder what the getRows() API would be usefull for :-), if anybody got a clue...

Kind regards,

Marc

Qualiture
Active Contributor
0 Kudos

Hi Marc,

I'm not aware of any examples, but it is in the sap.ui.table API: SAPUI5 SDK - Demo Kit

Personally, I have never found any use for the getRows() / addRow() / etc methods, I like to do everything model-based

Answers (2)

Answers (2)

maximilian_lenkeit
Participant
0 Kudos

We'll need to subtract the first visible row.

var idx = oTable.getSelectedIndex() - oTable.getFirstVisibleRow();

var row = oTable.getRows()[idx];

This due to the implementation of the sap.ui.table.Table control. For this control, getRows is the getter for the rows aggregation which will never contain more elements than visibleRowCount but instead binds the elements in the aggregation against a certain range of records in the model. The range is always [firstVisibleRow; firstVisibleRow + visibleRowCount].

- Max

P.S.

What are you trying to achieve anyway?

Former Member
0 Kudos

Thanks Maximilian,

However, i just retested to be sure, but the getRows() always return the first ones, and does not update dynamically given thenavigation; hence, getRows() is always in the range [0;visibleRowCount]), and not [firstVisibleRow; firstVisibleRow + visibleRowCount].

Maybe I'm missing something ?

To answer the questions, here is the larger picture of what i'm trying to achieve:

I just want to retrieve the model bindings of a multi-selection on a list that is larger than the visibleRowCount. I actually do not care about the rows objects, I only need them to retrieve rows[selectedIndex].getBindingContext(), which gives me the path to get the needed model properties.

See below my code for removing the selected entries of the table:

new sap.ui.commons.Button({

  text: "Remove Entries",

  press: function() {

  var idxs = oJobsTable.getSelectedIndices();

  var rows = oJobsTable.getRows();


  for (i=0;i <idxs.length;i++) {

  var row = rows[idxs[i]];


  var aPath = row.getBindingContext();   

  var entryID = aModel.getProperty(aPath + "/id");

  oController.removeEntry(entryID);

  }


  oController.refreshData();

  }


If any idea to make this simplier (without going into the rows api) i'm fine 🙂 !

However, keep in mind i need to support the table sorting (hence why it seemed to me i needed to really get the bindingContext from the row)...

Thanks !

former_member207744
Contributor
0 Kudos

Hi,

oTable.getRows() will return only visible rows count.

I have written code in rowSelectionchange. Not sure you are looking like this, but pls have a look.

Former Member
0 Kudos

Thanks for the quick answer Sriram,

However, this is the a) workaround i described in my initial question.

The problem with this approach, is that i'll have to manage yet another structure to know what is unselected/selected for multiple selections...

former_member207744
Contributor
0 Kudos

Could you please let us know what are you trying to achieve by getting rows? It will help to address your question.

Former Member
0 Kudos

See my answer to Maximilian below on what i'm trying to achieve..