简体   繁体   English

GWT DataGrid 中可扩展行的简单示例

[英]Straightforward example of expandable row in GWT DataGrid

I'm trying to pull out the required code from Custom DataGrid Example我正在尝试从自定义 DataGrid 示例中提取所需的代码

to achieve expandable rows as shown in said example (click on "show friends" cell) but the provided source is so bloated that it's not as easy as it seems.实现可扩展的行,如上述示例所示(单击“显示朋友”单元格),但提供的源代码过于臃肿,并不像看起来那么容易。

Does anyone have more straightforward example how to achieve expandable rows on DataGrid?有没有人有更直接的例子如何在 DataGrid 上实现可扩展的行?

I had the same problem like you and developed the following solution:我和你有同样的问题,并开发了以下解决方案:

POJO which contains all necessary information about a row, also it contains the children rows which can be opened: POJO 包含有关行的所有必要信息,还包含可以打开的子行:

import java.util.List;

public class MyPojo {

    private String value;

    private List<MyPojo> children;

    public MyPojo(String value, List<MyPojo> children) {

        this.value = value;
        this.children = children;
    }

    public String getValue1() {
        return value;
    }

    public List<MyPojo> getChildren() {
        return children;
    }

}

And now the datagrid which contains rows which can be opened:现在包含可以打开的行的datagrid

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.TextColumn;

public class MyGrid extends DataGrid<MyPojo> {

    private List<MyPojo> allRows = new ArrayList<>();

    private final Set<MyPojo> openedRows = new HashSet<MyPojo>();

    public MyGrid() {
        TextColumn<MyPojo> column1 = new TextColumn<MyPojo>() {

            @Override
            public String getValue(MyPojo object) {
                return object.getValue1();
            }
        };
        addColumn(column1, "Column1");

        SafeHtmlRenderer<String> anchorRenderer = new AbstractSafeHtmlRenderer<String>() {
            @Override
            public SafeHtml render(final String object) {

                if (!object.isEmpty()) {

                    SafeHtmlBuilder sb = new SafeHtmlBuilder();
                    sb.appendHtmlConstant("<a href=\"javascript:;\">").appendEscaped(object)
                            .appendHtmlConstant("</a>");
                    return sb.toSafeHtml();
                }
                return null;

            }
        };

        Column<MyPojo, String> openRowColumn = new Column<MyPojo, String>(
                new ClickableTextCell(anchorRenderer)) {

            public String getValue(final MyPojo object) {

                if (object.getChildren() != null && object.getChildren().size() != 0) {

                    if (openedRows.contains(object)) {
                        return "Hide";
                    }
                    return "Open";
                } else {
                    return null;
                }

            }

        };
        addColumn(openRowColumn, "Open Column");

        openRowColumn.setFieldUpdater(new FieldUpdater<MyPojo, String>() {

            @Override
            public void update(final int index, final MyPojo object, final String value) {
                handleChild(index, object);

            }

        });

        addContent();

    }

    private void handleChild(int index, MyPojo object) {
        List<MyPojo> children = object.getChildren();

        if (children != null && children.size() != 0 && !openedRows.contains(object)) {
            allRows.addAll(index + 1, children);
            openedRows.add(object);
            setRowData(allRows);
        } else if (openedRows.contains(object)) {

            for (int i = 0; i < children.size(); i++) {
                allRows.remove(index + 1);
            }
            openedRows.remove(object);
            setRowData(allRows);

        }

    }

    private void addContent() {

        MyPojo child = new MyPojo("Child 1", null);
        List<MyPojo> children = new ArrayList<>();
        children.add(child);

        for (int i = 0; i < 5; i++) {
            allRows.add(new MyPojo("c" + i, children));
        }
        setRowData(allRows);
    }
}

The idea is to track the rows which are opened, therefore we use the HashSet openedRows .这个想法是跟踪打开的行,因此我们使用 HashSet openedRows If a user clicks on open/close either the children are added below the clicked entry or the the visible children will be removed.如果用户单击打开/关闭,要么将子项添加到单击的条目下方,要么将删除可见子项。

A downside of this approach is that the table can not be sorted, because this will mix up the children and parent entries.这种方法的一个缺点是无法对表进行排序,因为这会混淆子项和父项。 I also did not test what happens when rows are deleted/manipulated/inserted.我也没有测试删除/操作/插入行时会发生什么。

There may be better or simpler approaches (unknown to me), but this one is relatively straightforward and should work reliable if no modification is needed.可能有更好或更简单的方法(我不知道),但这种方法相对简单,如果不需要修改,应该可以可靠地工作。

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

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