Search code examples
javascriptlistsapui5

sap.m.List Scroll to specific Item


I'm using sap.m.List in my SAPUI5 Application. There are 90 Entries, one for each day in the last 3 Month. Looks like this:

enter image description here

Now I want to scroll to a specific CustomListItem, when a Button is clicked. My View is looking like this: enter image description here

The Idea is that I have three Buttons for scrolling directly to an month. For example: Today, last Month, third Month. Clicking the Button directly scrolls to the first entry of that month. I just found sth. like oList.setSelectedItem(oItem, true); which has no effect.

Someone has an idea how this can be done?

Thank you in advance.


Solution

  • I have made few modification and able to achieve you requirement.

    View

    <!-- Buttons  --> 
    <Button text="Previous Month" press="scrollToPreviousMonth" class="sapUiTinyMarginEnd"/> 
    <Button text="Today" press="scrollToCurrentMonth" class="sapUiTinyMarginEnd"/> 
    <Button text="Next Month" press="scrollToNextMonth"/>
    
    <!-- List inside scroll Container -->   
    <ScrollContainer id="oScrollContainer" height="405px" vertical="true">
        <List id="monthList"
            items="{ 
              path: 'dataModel>/items', 
              sorter: { path: 'group', group: true },
              groupHeaderFactory: '.getGroupHeaderMonth' }" 
              includeItemInSelection="true">
            <CustomListItem type="Active">
                <Toolbar>
                    <Label text="{dataModel>title}"/>
                    <ToolbarSpacer />
                    <Label text="{dataModel>day}"/>
                    <core:Icon width="50px" src="sap-icon://edit" />
                </Toolbar>
            </CustomListItem>
        </List> 
    </ScrollContainer>
    

    Controller

    //Setting the data
    setDataToView1: function(sType) {   
        var oData = [
           { "title": "Jan-01", "day": "01-01-2019", "status": "often", "group": "1" },
           { "title": "Jan-02", "day": "02-01-2019", "status": "often", "group": "1" },
           { "title": "Jan-03", "day": "03-01-2019", "status": "often", "group": "1" },
           { "title": "Jan-04", "day": "04-01-2019", "status": "often", "group": "1" },
    
           { "title": "Feb-01", "day": "01-02-2019", "status": "often", "group": "2" },
           { "title": "Feb-02", "day": "02-02-2019", "status": "often", "group": "2" },
           { "title": "Feb-03", "day": "03-02-2019", "status": "often", "group": "2" },
           { "title": "Feb-04", "day": "04-02-2019", "status": "often", "group": "2" },
    
           { "title": "Mar-01", "day": "01-03-2019", "status": "often", "group": "3" },
           { "title": "Mar-02", "day": "02-03-2019", "status": "often", "group": "3" },
           { "title": "Mar-03", "day": "03-03-2019", "status": "often", "group": "3" },
           { "title": "Mar-04", "day": "04-03-2019", "status": "often", "group": "3" }                 
        ];
        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData({ "items" : oData });
        this.getView().setModel(oModel, "dataModel");
    },
    //setting the group header
    getGroupHeaderMonth: function (oGroup) {
        var oMonths = { "1" : "Jan", "2" : "Feb", "3" : "Mar", "4" : "Apr", "5" : "May", "6" : "Jun", "7" : "Jul", "8" : "Aug", "9" : "Sep", "10" : "Oct", "11" : "Nov", "12" : "Dec" };
        var custClass = oMonths[oGroup.key] +"MonthHdr";
        return new sap.m.GroupHeaderListItem({
            title: oMonths[oGroup.key],
            upperCase: false
        }).addStyleClass(custClass);
    },
    //Navigate to previous month list item
    scrollToPreviousMonth: function(){
        this.scrollToItem(".JanMonthHdr");
    },
    //Navigate to current month list item
    scrollToCurrentMonth: function(){
        this.scrollToItem(".FebMonthHdr");
    },
    //Navigate to next month list item
    scrollToNextMonth: function(){
        this.scrollToItem(".MarMonthHdr");
    },
    //Helper function to scrolling to the corresponding list item
    scrollToItem: function(sItem) {
        var oScrollList = this.byId("oScrollContainer"),
            oList = this.byId("monthList"),
            sItemId = jQuery(sItem).attr("id"),
            oItem = sap.ui.getCore().byId(sItemId);
    
        oList.setSelectedItem(oItem);
        oScrollList.scrollToElement(oItem, 2000);       
    },
    

    Output

    enter image description here