简体   繁体   English

删除GWT Datagrid中的行而不滚动到网格的顶部

[英]Remove a row in a GWT Datagrid without scrolling to the top of the grid

I have a datagrid that may display many rows per page. 我有一个数据网格,每页可能会显示很多行。 Let's say I am displayed 25 rows per page. 假设我每页显示25行。 The viewable area of the grid, however, is only 10 rows. 然而,网格的可视区域仅为10行。 ie There is only 400px for the entire grid and each row is 40px. 即整个网格只有400px,每行为40px。 So there is a scroll bar on the grid. 所以网格上有一个滚动条。

When I remove a single row in the grid, the grid automatically moves to the first row in the grid. 当我删除网格中的单行时,网格会自动移动到网格中的第一行。 If I have scrolled the bottom and deleted the last row, I am once again moved to the 1st row. 如果我滚动了底部并删除了最后一行,我再次移动到第一行。

I have attempted several ways of combatting this, but I can't find a solution that works the way I want it to. 我曾尝试过多种方法来解决这个问题,但我找不到一种能够按照我想要的方式运行的解决方案。

I've tried scrolling the row directly before or after the deleted row into view using the scrollIntoView() method. 我尝试使用scrollIntoView()方法将删除行之前或之后的行直接滚动到视图中。

I've tried figuring out how to determine which rows were in the visible range before the deletion, but the getVisibleRange() method is relevant to the page range, not the actual displayed range. 我已经尝试弄清楚在删除之前如何确定哪些行在可见范围内,但是getVisibleRange()方法与页面范围相关,而不是实际显示的范围。

I've searched the web for this and seems like I'm the only one having this problem. 我在网上搜索过这个,看起来我是唯一有这个问题的人。 What am I missing? 我错过了什么?

我有同样的问题,我发现如果dataGrid有keyboardSelectionPolicy =“BOUND_TO_SELECTION”,则会发生错误

If you use ListDataProvider to manage the DataGrid's data, then the DataGrid will not scroll when removing/adding items. 如果使用ListDataProvider来管理DataGrid的数据,则在删除/添加项时DataGrid将不会滚动。

Here is a minimal example of removing grid rows without any scrolling (all contained within the entry point class): 下面是一个删除网格行而不进行任何滚动的最小示例(全部包含在入口点类中):

Class for DataGrid Rows: DataGrid行的类:



    private class Item{
        public String text;
        public int value;
        public Item(String text, int value){
            this.text = text;
            this.value = value;
        }
    }

Filling the DataGrid: 填充DataGrid:

Here I use a private variable, data , to hold the items for the DataGrid. 这里我使用私有变量data来保存DataGrid的项目。 Note, that we must attach the dataGrid to data via the addDataDisplay method. 注意,我们必须通过addDataDisplay方法将dataGrid附加到data



    ListDataProvider data;
    public void onModuleLoad() {
        // build grid:
        DataGrid dataGrid = new DataGrid();
        BuildColumns(dataGrid);
        dataGrid.setWidth("300px");
        dataGrid.setHeight("300px");
        // add items:
        data = new ListDataProvider();
        for(int i = 1; i < 25; i++){
            data.getList().add(new Item("Item " + i, i));
        }
        data.addDataDisplay(dataGrid);
        // display:
        RootPanel.get().add(dataGrid);
    }

Building the DataGrid: 构建DataGrid:

This private method is used to build the columns for the DataGrid. 此私有方法用于构建DataGrid的列。 Inside of the FieldUpdater for delCol , which is used to listen for click events for button columns, we remove the respective item from data , and call data.refresh() to update the DataGrid display. 在用于监听按钮列的单击事件的delCol的delCol内部,我们从data删除相应的项目,并调用data.refresh()来更新DataGrid显示。



    private void BuildColumns(DataGrid dataGrid){
       Column textCol = new Column(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Item object) {
                    SafeHtmlBuilder sb = new SafeHtmlBuilder();
                sb.appendEscaped(object.text);
                    return sb.toSafeHtml();
            }
        };
        dataGrid.addColumn(textCol);
        dataGrid.setColumnWidth(textCol, 75, Unit.PCT);

        Column deleteCol = new Column(new ButtonCell()) {
            @Override
            public String getValue(Item object) {
                return "Delete " + object.value;
            }
        };
        deleteCol.setFieldUpdater(new FieldUpdater() {

            @Override
            public void update(int index, Item object, String value) {
                data.getList().remove(index);
                data.refresh();
            }
        });
        dataGrid.addColumn(deleteCol);
    }

I put this code in a new GWT project and tested it. 我将此代码放在一个新的GWT项目中并进行测试。 The DataGrid does not scroll when removing rows. 删除行时,DataGrid不会滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM