GWT 单元列表;元素选项之间的编程滚动

发布于 2024-11-07 20:32:26 字数 1883 浏览 0 评论 0原文

我想了解可用于滚动到 CellList 中特定元素的选项?目前,我的列表中有 100 个元素,需要通过单击按钮“跳转”元素,但似乎无法在单元列表(或代码中)中找到提供此功能的任何方法。

有什么想法吗?

非常感谢,

伊恩。

**编辑

下面的工作代码示例;

public class CellListTest implements EntryPoint {

private CellList<String> cellList;
private SingleSelectionModel<String> stringSingleSelectionModel;

/**
 * This is the entry point method.
 */
public void onModuleLoad() {
    cellList = new CellList<String>(new TextCell());
    cellList.setRowData(buildStringList(200));


    cellList.setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.BOUND_TO_SELECTION);

    Button byTen = new Button("Jump Forward 10");
    stringSingleSelectionModel = new SingleSelectionModel<String>();
    cellList.setSelectionModel(stringSingleSelectionModel);
    byTen.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            jumpForward(10);
        }
    });
    byTen.setHeight("30px");
    HTMLPanel htmlPanel = new HTMLPanel("");
    VerticalPanel verticalPanel = new VerticalPanel();
    cellList.setHeight("600px");
    ScrollPanel scrollPanel = new ScrollPanel(cellList);
    verticalPanel.add(byTen);
    verticalPanel.add(scrollPanel);
    htmlPanel.add(verticalPanel);
   RootLayoutPanel.get().add(htmlPanel);
}

final Random random = new Random();
private List<String> buildStringList(int numberToCreate) {
    final ArrayList<String> randomValues = new ArrayList<String>();
    for (int i = 0; i < numberToCreate; i++) {
        randomValues.add(String.valueOf(random.nextInt()));
    }
    return randomValues;
}

private int currentPosition = 0;
private void jumpForward(int toJump) {
    Element rowElement = cellList.getRowElement(currentPosition += toJump);
    rowElement.scrollIntoView();
}

}

I would like to understand the options available to scrolling to a specific element in a CellList? Currently I have a 100 elements in the list and need to "jump" through the elements at the click of a button but can't seems to locate any methods on the celllist (or in the code) that provides this feature.

Any Ideas?

Many Thanks in advance,

Ian.

**EDIT

working code example below;

public class CellListTest implements EntryPoint {

private CellList<String> cellList;
private SingleSelectionModel<String> stringSingleSelectionModel;

/**
 * This is the entry point method.
 */
public void onModuleLoad() {
    cellList = new CellList<String>(new TextCell());
    cellList.setRowData(buildStringList(200));


    cellList.setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.BOUND_TO_SELECTION);

    Button byTen = new Button("Jump Forward 10");
    stringSingleSelectionModel = new SingleSelectionModel<String>();
    cellList.setSelectionModel(stringSingleSelectionModel);
    byTen.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            jumpForward(10);
        }
    });
    byTen.setHeight("30px");
    HTMLPanel htmlPanel = new HTMLPanel("");
    VerticalPanel verticalPanel = new VerticalPanel();
    cellList.setHeight("600px");
    ScrollPanel scrollPanel = new ScrollPanel(cellList);
    verticalPanel.add(byTen);
    verticalPanel.add(scrollPanel);
    htmlPanel.add(verticalPanel);
   RootLayoutPanel.get().add(htmlPanel);
}

final Random random = new Random();
private List<String> buildStringList(int numberToCreate) {
    final ArrayList<String> randomValues = new ArrayList<String>();
    for (int i = 0; i < numberToCreate; i++) {
        randomValues.add(String.valueOf(random.nextInt()));
    }
    return randomValues;
}

private int currentPosition = 0;
private void jumpForward(int toJump) {
    Element rowElement = cellList.getRowElement(currentPosition += toJump);
    rowElement.scrollIntoView();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2024-11-14 20:32:26

我认为 CellList 没有适合您目的的直接方法。

您可以做的是使用 Element 的 scrollIntoView< /a> 方法。该方法调整每个可滚动元素的scrollLeft和scrollTop属性,以确保指定的元素完全在视图中。为了使用该方法,您需要获取包含要显示的单元格的元素。实现此目的的一种方法是使用 CellList public getRowElement(int indexOnPage)。

我还没有尝试过,但我相信下面的代码应该可以工作:

//Ensures cell 22 on page is shown
Element element = myCellList.getRowElement(22);
element.scrollIntoView();

I do not think CellList has a direct method for your purpose.

What you can do is use Element's scrollIntoView method. This method adjusts the scrollLeft and scrollTop properties of each scrollable element to ensure that the specified element is completely in view. In order to use that method you need to get the element containing the cell you want to show. One way to achive this is by using CellList public getRowElement(int indexOnPage).

I have not tryed it, but I believe the following code should work:

//Ensures cell 22 on page is shown
Element element = myCellList.getRowElement(22);
element.scrollIntoView();
一瞬间的火花 2024-11-14 20:32:26

将元素滚动到视图中是一回事;将元素滚动到视图中是一回事;将元素滚动到视图中是一回事。将键盘焦点更改为特定元素是另一回事。经过多次探索,我发现实现这一目标的最佳方法是触发本机事件来模拟用户按键。

private void hitKeyOnList(int keyCode) {
  NativeEvent keyDownEvent = Document.get().createKeyDownEvent(
    false, false, false, false, keyCode);
  list.getElement().dispatchEvent(keyDownEvent);
}

在上面的代码片段中,list 变量是对 CellList 的引用。

因此,要将光标移动到列表末尾,请执行以下操作:

hitKeyOnList(KeyCodes.KEY_END);

将光标移动到当前具有键盘焦点的项目的下一项:

hitKeyOnList(KeyCodes.KEY_DOWN);

此方法应该适用于所有这些键,这些键由 AbstractHasData

KeyCodes.KEY_DOWN
KeyCodes.KEY_UP
KeyCodes.KEY_PAGEDOWN
KeyCodes.KEY_PAGEUP
KeyCodes.KEY_HOME
KeyCodes.KEY_END

Scrolling the element into view is one thing; changing the keyboard focus to a particular element is quite another. After much exploration, I have found that the best way to achieve that is to fire native events to simulate the user pressing keys.

private void hitKeyOnList(int keyCode) {
  NativeEvent keyDownEvent = Document.get().createKeyDownEvent(
    false, false, false, false, keyCode);
  list.getElement().dispatchEvent(keyDownEvent);
}

In the above snippet, the list variable is a reference to a CellList.

So, to move the cursor to the end of the list, do this:

hitKeyOnList(KeyCodes.KEY_END);

to move the cursor to the next item down from the one that currently has keyboard focus:

hitKeyOnList(KeyCodes.KEY_DOWN);

This approach should work for all of these keys, which are handled by AbstractHasData:

KeyCodes.KEY_DOWN
KeyCodes.KEY_UP
KeyCodes.KEY_PAGEDOWN
KeyCodes.KEY_PAGEUP
KeyCodes.KEY_HOME
KeyCodes.KEY_END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文