1

i am trying to attach the itemPress event for SmartTable control in sap UI5. The view is defined in XML and bind to OData.

<mvc:View
   controllerName="myapp.controller.App"
 ...>
   <App>
      <pages>
         <Page title="title">
            <content>
                <smartTable:SmartTable
                    id="kubas"
                    ...
                    tableType="ResponsiveTable" 
                    ...>
                </smartTable:SmartTable>
            </content>
         </Page>
      </pages>
   </App>
</mvc:View>

Since for the ResponsiveTable the table behind is sap.m.Table i was trying to attach the itemPress event in the onAfterRendering event of controller. It did not work. Then i tried to override the onAfterRendering of the table itself and there attach the event - same effect, the event did not trigger.

onAfterRendering : function(){
            var tTable = this.byId("kubas");
            var oTable = this.byId("kubas").getTable(); //sap.m.table

            console.log(oTable.getMetadata().getName());

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

            oTable.onAfterRendering = function(){

                console.log("OnAfterRendering");

                this.attachItemPress(function(oEvent){
                    console.log("Pressed!!");
                });

            }

Do i do something wrong here, any suggestions ? Is there any way to register it in XML for SmartTable ? I would like not to switch to sap.m.table in the XML view but leave it as it is. Would appreciate your help Gurus.

Jaro
  • 1,757
  • 1
  • 15
  • 36
Kubas
  • 91
  • 2
  • 12

2 Answers2

1

that's because the items are "inactive". Check document here

> attachItemPress(oData?, fnFunction, oListener?): sap.m.ListBase Attaches event handler fnFunction to the itemPress event of this sap.m.ListBase. When called, the context of the event handler (its this) will be bound to oListener if specified, otherwise it will be bound to this sap.m.ListBase itself.

> Fires when an item is pressed unless the item's type property is Inactive.

Please use the below code and attachDataReceived of SmartTable is working.

var fnItemPress = function(){alert("press")};   
tTable.attachDataReceived(function(){
    var aItems = oTable.getItems();
    if(aItems.length === 0 ) return;
    $.each(aItems, function(oIndex, oItem) {
        oItem.detachPress(fnItemPress);
        oItem.setType("Active");
        oItem.attachPress(fnItemPress);
    });
});

Thank you!

Haojie
  • 5,665
  • 1
  • 15
  • 14
  • What about detaching this event? Otherwice there will be as many attached events as the "updateFinished" events fired on the table, which will lead to multiple event triggerings :) – Andrew Naumovich Aug 04 '17 at 08:53
  • @AndriiNaumovych I think you are right about this. I have changed my code. Now it is working even the table is refreshed. – Haojie Aug 06 '17 at 14:03
0

I think that no chance to do that without the table definition in XML. But I guess you can omit "columns" aggregation definition and in "items" include only "ColumnListItem" with the needed event handler (without "cells"). Smart Table should automatically inject neeeded columns/cells.

Andrew Naumovich
  • 1,441
  • 1
  • 12
  • 15