0

我有一个CellTable并且想将键盘选择策略设置为KeyboardSelectionPolicy.BOUND_TO_SELECTION. 但是,似乎第一次单击TextCell第一行中的 a 并没有像预期的那样完全选择该行。为了说明,它看起来像这样: 在此处输入图像描述 而不是这样: 在此处输入图像描述

所有后续单击都按预期选择行,键盘导航也是如此。此外,如果第一次单击是在NumberCellTextCell任何其他行中的 a 上,则选择会按预期工作。该问题仅在第一次单击TextCell第一行中的 a 时出现。

要重现,请考虑以下示例代码:

public class WebApp implements EntryPoint {

    private static class Model {
        private final int num;
        private final String name;
        public Model(String name, int num) {
            super();
            this.name = name;
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public int getNum() {
            return num;
        }
    }

    @Override
    public void onModuleLoad() {

        final CellTable<Model> table = new CellTable<Model>();
        table.setWidth("100%", true);  // Truncate cell contents as needed.

        // Causes bug on first row first click TextCell selection.
        table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

        // Add a column to show the name.
        TextCell textCell = new TextCell() {
            @Override
            public Set<String> getConsumedEvents() {
                return Sets.newHashSet(BrowserEvents.CLICK);
            }
        };
        Column<Model, String> termColumn = new Column<Model, String>(new TextCell()) {
            @Override
            public String getValue(Model entry) {
                return entry.getName();
            }
        };
        termColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        table.addColumn(termColumn);

        // Add a column to show the num.
        Column<Model, Number> numberColumn = new Column<Model, Number>(new NumberCell()) {
            @Override
            public Number getValue(Model object) {
                return object.getNum();
            }
        };
        numberColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        table.addColumn(numberColumn);

        // Add a selection model to handle user selection.
        final SingleSelectionModel<Model> selectionModel = new SingleSelectionModel<Model>();
        table.setSelectionModel(selectionModel);

        // Data.
        ArrayList<Model> entries = buildEntries();

        // Set the total row count. This isn't strictly necessary, but it affects
        // paging calculations, so its good habit to keep the row count up to date.
        table.setRowCount(entries.size(), true);

        // Push the data into the widget.
        table.setRowData(0, entries);

        // Add it to the root panel.
        RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();
        rootLayoutPanel.getElement().getStyle().setWidth(50, Unit.PCT);
        rootLayoutPanel.getElement().getStyle().setHeight(50, Unit.PCT);
        rootLayoutPanel.getElement().getStyle().setProperty("margin", "auto");
        rootLayoutPanel.add(table);
    }

    private ArrayList<Model> buildEntries() {
        ArrayList<Model> l = Lists.newArrayList();

        for (int i = 0; i < 10; ++i) {
            l.add(buildEntry());
        }

        return l;
    }

    private int currNum = 0;
    private char currChar = 'a' - 1;
    private Model buildEntry() {
        int num = currNum++;
        String term = Character.toString(++currChar);
        for (int i = 0; i < 25; i++) {
            term += currChar;
        }
        return new Model(term, num);
    }
}

这体现在 Chrome、FF、IE 上。任何帮助表示赞赏,谢谢。

4

0 回答 0