Skip to Content
0
Aug 05, 2014 at 02:08 PM

Reloading listbox and scrollToIndex

35 Views

I think I'm missing something obvious.

The scrollToIndex works when the list is first loaded. However, when I use the button

to clean out the list and reload the items, the scrolling no longer seems to work.

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<script id="sap-ui-bootstrap"

src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"

data-sap-ui-libs="sap.ui.commons"

data-sap-ui-theme="sap_bluecrystal">

</script>

<script>

// List

var oList1 = new sap.ui.commons.ListBox("listbox");

oList1.setVisibleItems(3);

loadData(oList1);

oList1.placeAt("list");

// Button

var oButton = new sap.ui.commons.Button({text : "Button", press: buttonCB});

oButton.placeAt("button");

function buttonCB(evt) {

loadData(sap.ui.getCore().byId("listbox"));

}

function loadData(oList) {

oList.destroyItems();

oList.addItem( new sap.ui.core.ListItem({text : 'aaa', key: "aaa"}));

oList.addItem( new sap.ui.core.ListItem({text : 'bbb', key: "bbb"}));

oList.addItem( new sap.ui.core.ListItem({text : 'ccc', key: "ccc"}));

oList.addItem( new sap.ui.core.ListItem({text : 'ddd', key: "ddd"}));

oList.addItem( new sap.ui.core.ListItem({text : 'eee', key: "eee"}));

oList.setSelectedKeys(["ddd"]);

oList.scrollToIndex(oList.getSelectedIndex());

}

</script>

</head>

<body class="sapUiBody">

<div id="list"></div>

</br>

<div id="button"></div>

</body>

</html>