0

I am trying to select the first item of list in Master view. I am doing this from Detail controller. I am able to select the item in the list but it does not fire the event to display the details in Detail view.

Master.view.xml

<List id="idMasterList" mode="{device>/listMode}" select="onSelect" 
                noDataText="{i18nMaster>masterListNoDataText}"  
                items="{  path: '/', sorter: {path: 'name'} }" icon="sap-icon://refresh">
            <items>
              <StandardListItem id="idStandardItem" type="{device>/listItemType}"  
                tooltip="{name}" 
                title="{name}"
                info="{total}/{due}" 
                /> 
            </items>                            
        </List>

Detail.controller.js

sap.ui.getCore().byId("__xmlview2--idMasterList").getItems()[0].setSelected(true);

I could not find any information which can directly solve this (or) i did not get it!!!

Any help is appreciated.

Thanks

Gana
  • 482
  • 3
  • 11
  • 32

2 Answers2

5

You can call the setSelectedItem() method on the List instead of setSelected() on the ListItem. The setSelectedItem() method does have a third undocumented parameter: bFireEvent.

var list = this.byId("idMasterList");
list.setSelectedItem(list.getItems()[0], true /*selected*/, true /*fire event*/);

See JSBin example

But be aware that it's an undocumented parameter. It might change without notice in future releases (but has not up to now).

schnoedel
  • 3,918
  • 1
  • 13
  • 17
  • I have tried it and it is working perfectly fine. You reminded me to look at source code when in need. Since EventBus is providing a solution (better) i prefer use that. Thanks for your time and providing solution. – Gana Feb 28 '16 at 14:07
  • You're welcome! I would also recommend the EventBus for inter-controller-communication instead of accessing the controls of a foreign view via ID. So i go along with deterministicFail. – schnoedel Feb 28 '16 at 19:13
0

As mentioned in the documentation the select event Fires when selection is changed via USER INTERACTION...

https://sapui5.netweaver.ondemand.com/docs/api/symbols/sap.m.ListBase.html#event:select

You will have to select it with your setSelected(true) line and execute your onSelectfunction afterwards.

I dont know which version of ui5 you are using but consider the select event is deprecated. You should use selectionChange instead

deterministicFail
  • 1,271
  • 9
  • 28
  • Hi, First of all thanks for responding. I am using 1.28.13. How can i call Master controller from Detail controller?When i call Master controller, will life cycle of the controller trouble me in any way? Will this invocation execute onInit function of Master controller? For some reason selectionChange did not work for me. I will have to try again. – Gana Feb 26 '16 at 09:05
  • for communication between controllers, take a look here (and feel free to upvote it) http://stackoverflow.com/questions/34000949/ui5-passing-data-between-views/34001318#34001318 – deterministicFail Feb 26 '16 at 10:18