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.
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!