6

I have an sap.ui.Table that shows a list of records. I want to get a count of the records in the data.

I have read the post SAP UI 5 how to print total table row , but it hasn't helped me.

This is the code for the table (code for the columns has been removed to make the post smaller):

  <table:Table id="PickRecs" visibleRowCount="10" selectionMode="MultiToggle" visible="true" rows="{/downRecs}" >
    <table:title>
        <txt:Text text="{
      path: '/downRecs',
      formatter: 'Formatter.totalFormatter'
    }">
        </txt:Text>
        <Label text="possible records to export"></Label>

    </table:title>
    <table:columns>
      .......
    </table:columns>
 </table:Table>

This is the formatter.js:

totalFormatter:function(results) {

    return results.length;
 }

I would like to display how many rows there are in the table using the array downRecs as the source of all the records. For example: 3 possible records to export.

This value can change based on some input fields on the screen, for example they can choose to see all records for a product or only records for a certain customer, etc.

How can I get this updated count of the records? This value is shown on the toolbar or title of the table.

Community
  • 1
  • 1
user3861284
  • 241
  • 2
  • 10
  • 22

4 Answers4

10

Binding length is not a property that you can bind. Also what is written at the reference link is not correct because you cannot initialize ListBinding for a property, ListBinding needs template or factory and multiple aggregation cardinality.

To get informed about updated count of the records you should attach to the change event of the binding.

var oBinding = oTable.getBinding("rows"); 
oBinding.attachChange(function(sReason) {
    oYourTextField.setText(oBinding.getLength());
});

see jsbin and press column header for filter menu

http://jsbin.com/kohozenina/1/edit?html,output

We know this is a little bit cumbersome and we are working on a ControlModel which you can bind something that fires change event like binding length or number of the selected items.

aborjinik
  • 723
  • 3
  • 5
  • Tried your example. When I console.log( oBinding) (right after the getBinding call), it shows as 'undefined'. If I look at my table (oTable) and look through, it shows the binding as my JSON array ('/downRecs'). I added the three lines you show, but because oBinding is saying undefined, I get an error for the attachChange. Am I missing something? – user3861284 Dec 11 '14 at 18:32
  • I finally got it to work....I had to put the code into the same area as I had the call to the JSON call. Thanks. – user3861284 Jan 06 '15 at 01:27
1
 xml view 
    <Button text="click" press="click"></Button>
        <Table id="idProductsTable"
    inset="false"
    items="{/ProductCollection}">
    <headerToolbar>
      <Toolbar>
        <Label id="idset" ></Label>
      </Toolbar>
    </headerToolbar>
    <columns>
      <Column 
        width="12em">
        <Text text="Product" />
      </Column>
      <Column
        minScreenWidth="Tablet"
        demandPopin="true">
        <Text text="Supplier" />
      </Column>
    </columns>
    <items>
      <ColumnListItem>
        <cells>
          <ObjectIdentifier
            title="{Name}"
            text="{ProductId}"
            class="sapMTableContentMargin" />
          <Text
            text="{SupplierName}" />
            </cells>
      </ColumnListItem>
    </items>
  </Table>

controller.js
onInit: function() {
    var data= {
        "ProductCollection": [
            {
                "ProductId": "1239102",
                "Name": "Power Projector 4713",
                "SupplierName": "Titanium"

            },
            {
                "ProductId": "2212-121-828",
                "Name": "Gladiator MX",
                "SupplierName": "Technocom"

            }]
            }
    var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData(data);
    this.getView().setModel(oModel);
},
click:function(){
var count= this.getView().byId("idProductsTable").getItems().length;
var id= this.getView().byId("idset").setText("Record="+count);
alert("Record="+count);
}
Abul
  • 119
  • 6
  • ty. I will try that. I was somewhat able to get around it by getting the number of items received each time I make my ajax call. but i will also try what you show although I don't have a button to press, I will see how I can work in something. – user3861284 Dec 15 '14 at 14:11
0

Have you tried binding to {/downRecs/length}? This works for most types of models.

Furthermore, when adding to the table make sure you add to the model and not the table itself. If you do this, databinding should take care of the rest.

It would be something along these lines:

var oSource = oEvent.getSource();
var oItems = oSource.getModel().getProperty("/downRecs");
oItems.push({
    "Property1": "Value1", 
    "Property2": "Value2", 
    ...
});
oSource.getModel().setProperty("/downRecs", oItems);
toxvaerd
  • 3,622
  • 3
  • 24
  • 29
  • so in my XML, I would add the {downRecs/length} where I want it displayed, right? Would that actually be {downRecs.length} (the '.' and not '/')? The contents of downRecs gets re-loaded when the user chooses the various selection criteria and the model is set but not as you show with the setProperty call. – user3861284 Dec 11 '14 at 12:43
  • @toxvaerd where did you hear this? There is no such thing in ui5 binding. – aborjinik Dec 11 '14 at 13:57
  • I tried most of the above and it didn't work - wouldn't load the page. If anyone else has suggestions, I would appreciate it. I have tried quite a bit and so far nothing helped. – user3861284 Dec 11 '14 at 14:11
  • 1
    @aborjinik I have used this with a JSONModel in 1.24 and it works fine :-) Maybe it's just a JSONModel thing? – toxvaerd Dec 11 '14 at 16:27
  • @toxvaerd it never works in any version of ui5 regardless of the model type. – aborjinik Dec 12 '14 at 14:17
-1

there is also one eayse way

oTable._getRowCount()