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

强尼

我正在尝试从Custom DataGrid示例中提取所需的代码

以实现如上述示例所示的可扩展行(单击“显示朋友”单元格),但是提供的源太过膨胀,以至于看起来并不那么容易。

没有人有更直接的示例如何在DataGrid上实现可扩展的行吗?

宾语

我遇到了与您相同的问题,并开发了以下解决方案:

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;
    }

}

现在,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);
    }
}

这个想法是跟踪打开的行,因此我们使用HashSet openedRows如果用户单击打开/关闭,则将子级添加到所单击的条目下方,否则将删除可见的子级。

这种方法的缺点是无法对表进行排序,因为这会混淆子项和父项。我也没有测试删除/操作/插入行时会发生什么。

可能有更好或更简单的方法(我不知道),但是这种方法相对简单,如果不需要修改,则应该可以可靠地工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章