Tree scrolls to blank at bottom of list when data refreshed dynamically

chrisj
30 1 1 1

I am using a tree to display a list of search results. When the user runs a new search, I completely replace all data in the tree with new results.

The problem I am having occurs when the new tree data has fewer results than the previously displayed tree. The tree object does not resize, but retains its full length, and it scrolls to the blank lines at the bottom of the list.

I can make the tree scroll to the top by using "setSelectedItem", but I don't want to do this. The application uses select events to retrieve more leaves for the tree, and this event will not trigger if the user clicks on an item that has been programmatically selected. (My users will be untrained members of the general public, so I cannot reasonably expect them to cope with this concept.)

My question is - Is there any other way I can force the tree to scroll to a place that actually contains some data? Alternatively, can I force the tree object to resize so that it does not contain blank pages?

Here is the zul definition of my tree:

<tree id="productTree" forward="onSelect=productfinder.onNodeSelect()" rows="10" width="360px">
</tree>

Here is how the tree is declared in java.

  ArrayList allProducts = new ArrayList();
Tree productTree;
SimpleTreeModel treeModel;
SimpleTreeNode rootNode;

rootNode = new PFTreeNode("Products", allProducts);

//create the tree model
treeModel = new SimpleTreeModel(rootNode);


Here is how I populate the tree model. The "places" list is retrieved using a call to a web service. (not shown)

        {
for (int i = 0; i <
places.size(); i++)
{
NamedPlace place = places.get(i);
//if place name not in list of feature types to filter
if (!this.placeNameFilterList.contains(place.getFeatureType()))
{
PFTreeNode placeNode = new PFTreeNode(place, new ArrayList());
}
}
}
productTree.setModel(treeModel);


This works correctly the first time the tree is displayed, but when I repopulate the tree with a new search, it scrolls to the bottom, showing only blank lines below the retrieved data.

Before I refresh with a new query, I clear the model like this:

    //clear the products list
allProducts.clear();

//update the tree
productTree.setModel(treeModel);

The actual data displayed is good. The tree contains all of the data that is expected. My only problem is that ZK forces the tree to scroll to the bottom and display a blank page. Can anyone please suggest how I can avoid this?

Thanks,
Chris

delete retag edit

13 Replies

samchuang
4084 4

Hi

Do you mean you will have a big tree, and it will focus to the end? maybe you could consider using paging mode.

or you could use tree.selectItem()

chrisj
30 1 1 1

Hi samchuang, and thank you for your suggestions.

samchuang wrote:
>Do you mean you will have a big tree, and it will focus to the end?

It focuses to blank pages, past the end of the tree data.

>maybe you could consider using paging mode.
A possibility, but I would like to fix the problem without this if I can.

>or you could use tree.selectItem()
This will position the tree correctly (I tried and it works), but setting the item to selected causes another problem in my application, so I don't want to do this.

samchuang
4084 4

Hi

I have another idea, how about using "Clients. moveTo(x, y)"

chrisj
30 1 1 1

samchuang wrote
>I have another idea, how about using "Clients. moveTo(x, y)"

That sounds interesting. Is there a way to make it scroll the Tree object rather than the whole browser window?

(My browser window is in the right place, only the tree object is focused outside the range of its data)

samchuang
4084 4

Hi

you could call tree.getLeft() tree.getTop() to get X, and Y position

chrisj
30 1 1 1

I don't understand. That would let me move the browser window to the position of the tree, yes?

The browser window already shows the tree. I need to scroll the Tree to the top of its data.

samchuang
4084 4

Hi

I see, then, maybe you could try tree.setTop();

chrisj
30 1 1 1

Hi samchuang.

I apologise for my slow response to your last post. I have been swamped with a lot of other problems.

Thank you for the suggestion, I will try it as soon as I can.

Chris

jaikarthik
69

Hi I'm having similar problem.. I'm rebuilding the tree when new data is added. when i rebuild the the tree horizontal scroll bar gets reset to left position.. i want to maintain the scroll bar in same position after rebuilding the tree. I tried getTop and getLeft both are not returning scroll bar position :( and Clients. moveTo(x, y) moves the browser and not the scroll bar in tree component :( any help is appreciated...

samchuang
4084 4

Hi

I found another way of solving the problem, using Clients.scrollIntoView(component), this function will display the compoent you like to display, refer to javadoc

the example using grid, but you could change to use tree,

<zk>
<zscript><![CDATA[
String[] data = new String[100];
for(int j=0; j < data.length; ++j) {
data = "option "+j;
}
ListModel strset = new SimpleListModel(data);

]]></zscript>
<intbox id="intbox"/>
<button label="scroll" >
<attribute name="onClick"><![CDATA[
int index = intbox.getValue();
Row row = (Row)grid.getRows().getChildren().get(index);
Clients.scrollIntoView(row);
]]></attribute>
</button>
<grid id="grid" model="\${strset}" width="100px" height="100px">
<columns>
<column label="options" />
</columns>
</grid>
</zk>


[hide preview]