简体   繁体   English

Nattable中的Treeview

[英]Treeview in Nattable

I'm looking for a way to create a treeTable view in Nattable. 我正在寻找一种在Nattable中创建treeTable视图的方法。 I already have a NatTable implemented with filter, sorting, ... 我已经有一个用过滤器,排序等实现的NatTable ...

But now I'm looking into a TreeTable like the TreeGridWithCheckBoxFieldsExample from the Nattable examples. 但是现在我正在从Nattable示例中查找像TreeGridWithCheckBoxFieldsExample这样的TreeTable。 The only requirement is that I do not change my datamodel for the tree. 唯一的要求是,我不要更改树的数据模型。

I have two different objects Company and role. 我有两个不同的对象Company和Role。 Every company does has all the roles. 每个公司都扮演着所有角色。 So in this situation I need a tree with all the companies as root object and all the roles beneath all the companies. 因此,在这种情况下,我需要一棵树,其中所有公司都是根对象,而所有公司下的所有角色都应作为树。

From the example it looks like I need to create a format class that implements the TreeList.Format but they are using the model to link the parent (I will not do this because it's a violation of the MVC principle. 从该示例看来,我需要创建一个实现TreeList.Format的格式类,但是他们使用该模型链接父级(我不会这样做,因为它违反了MVC原理。

Can someone get me on track to create a treetable view in NatTable? 有人可以引导我在NatTable中创建树表视图吗?

After checking some example of Natable I got a working treeTable. 在检查了Natable的一些示例之后,我得到了一个工作的treeTable。 But have only one problem left. 但是只剩下一个问题。 The parent items are not correctly shown. 父项未正确显示。

The treeFormat looks like: treeFormat看起来像:

public class TreeFormat implements TreeList.Format<PermissionViewModel> {

public TreeFormat() {
}

@Override
public Comparator getComparator(int depth) {    
    return new Comparator<PermissionViewModel>() {

        @Override
        public int compare(PermissionViewModel object1, PermissionViewModel object2) {

            return object1.getModuleName().compareTo(object2.getModuleName());

        }
    };
}

@Override
public void getPath(List<PermissionViewModel> path, PermissionViewModel element) {
     path.add(element);
     PermissionViewModel parent = element.getParent();
        while (parent != null) {
            path.add(parent);
            parent = parent.getParent();
        }
        Collections.reverse(path);  
}

@Override
public boolean allowsChildren(PermissionViewModel element) {
    return true;
}

The model that I use is a viewModel and is a one to one map to the normal model 我使用的模型是viewModel,并且是与正常模型的一对一映射

public class PermissionViewModel implements Comparable {

    private PermissionViewModel parent;
    private ArrayList<PermissionViewModel> children = new ArrayList();

    private Integer permissionId;
    private String moduleName;
    private String permissionName;
    private boolean active;
    private boolean on;

    public PermissionViewModel(PermissionViewModel parent, Permission permission) {
        this.parent = parent;
        if (parent != null) {
            parent.addChild(this);
        }

        if(parent == null && permission != null)
        {
            this.permissionId = 0;
            this.moduleName = "";
            this.permissionName = permission.getModuleName();
            this.active = false;    
        }           
        else
        {
            this.permissionId = permission.getPermissionId();
            this.moduleName = permission.getModuleName();
            this.permissionName = permission.getPermissionName();
            this.active = permission.isActive();
        }
    }

    public PermissionViewModel getParent() {
        return this.parent;
    }

    public void addChild(PermissionViewModel child) {
        this.children.add(child);
    }

    public List getChildren() {
        return this.children;
    }

    public PermissionViewModel getSelf() {
        return this;
    }

    public boolean isOn() {
        if (this.children.size() == 0) {
            return this.on;
        } else {
            return getCheckBoxState() == CheckBoxStateEnum.CHECKED;
        }
    }

    public void setOn(boolean on) {
        if (this.children.size() == 0) {
            this.on = on;
        } else {
            for (PermissionViewModel child : this.children) {
                child.setOn(on);
            }
        }
    }


    public CheckBoxStateEnum getCheckBoxState() {
        if (this.children.size() == 0) {
            return this.on ? CheckBoxStateEnum.CHECKED
                    : CheckBoxStateEnum.UNCHECKED;
        } else {
            boolean atLeastOneChildChecked = false;
            boolean atLeastOneChildUnchecked = false;

            for (PermissionViewModel child : this.children) {
                CheckBoxStateEnum childCheckBoxState = child.getCheckBoxState();
                switch (childCheckBoxState) {
                    case CHECKED:
                        atLeastOneChildChecked = true;
                        break;
                    case SEMICHECKED:
                        return CheckBoxStateEnum.SEMICHECKED;
                    case UNCHECKED:
                        atLeastOneChildUnchecked = true;
                        break;
                }
            }

            if (atLeastOneChildChecked) {
                if (atLeastOneChildUnchecked) {
                    return CheckBoxStateEnum.SEMICHECKED;
                } else {
                    return CheckBoxStateEnum.CHECKED;
                }
            } else {
                return CheckBoxStateEnum.UNCHECKED;
            }
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    public Integer getPermissionId() {
        return permissionId;
    }

    public String getModuleName() {
        return moduleName;
    }

    public String getPermissionName() {
        return permissionName;
    }

    public boolean isActive() {
        return active;
    }

}

Putting data in the tree table will be done with following source: 将数据放入树表将通过以下来源完成:

ArrayList<PermissionViewModel> permissionViewModelsList = new ArrayList<>();
    String previousModule = "";
    PermissionViewModel currentParent = null;

    for (Permission element : repo.getAllData()) {

        if(!previousModule.equals(element.getModuleName()))
        {
            previousModule = element.getModuleName();

            currentParent = new PermissionViewModel(null, element);
            permissionViewModelsList.add(currentParent);

            permissionViewModelsList.add(new PermissionViewModel(currentParent, element));
        }
        else
        {
            permissionViewModelsList.add(new PermissionViewModel(currentParent, element));
        }

    }
    Collections.reverse(permissionViewModelsList);


    permissionTable.setItems(permissionViewModelsList);
    permissionTable.refresh(true);

But when I look at the table the root elements are viewed but the childeren of the root elements are wrong. 但是,当我查看表格时,查看了根元素,但是根元素的子元素是错误的。 I viewed the list of elements and there I can't find any issues. 我查看了元素列表,但找不到任何问题。 Can someone find the issue that I have? 有人可以找到我遇到的问题吗?

在此处输入图片说明

I changed to order of the columns. 我更改为列的顺序。 I placed the "Module Name" before the "Name" column. 我将“模块名称”放在“名称”列之前。 And the parent module name has been placed in the module name column. 父模块名称已放置在“模块名称”列中。 This fixed my problem. 这解决了我的问题。

The issue in this case was that the comperator that I used is not working on mixed data in the same fields. 在这种情况下,问题在于我使用的编译器无法处理相同字段中的混合数据。 Tnx to Dirk Fauth I found it. Tnx到Dirk Fauth我找到了。

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

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