0

我的树表中有两个组合框列,例如组和用户。现在基于在第一列中选​​择的组,我想在第二列中显示该组的用户。这个怎么做?有人有想法吗?

4

1 回答 1

0

一个原始的例子:

    final Map<String, Set<String>> groupMap = new HashMap<String, Set<String>>();
    Set<String> userSet1 = new HashSet<String>();
    userSet1.add( "userA in group1" );
    userSet1.add( "userB in group1" );
    groupMap.put( "group1", userSet1 );
    Set<String> userSet2 = new HashSet<String>();
    userSet2.add( "userX in group2" );
    userSet2.add( "userY in group2" );
    groupMap.put( "group2", userSet2 );

    HierarchicalContainer container = new HierarchicalContainer();
    container.addItem( 1 );
    container.addItem( 2 );
    container.setParent( 2, 1 );

    final TreeTable tt = new TreeTable( null, container );
    tt.addGeneratedColumn( "group", new ColumnGenerator() {
        @Override
        public Object generateCell( Table source, Object itemId, Object columnId ) {
            final ComboBox gCB = new ComboBox();
            gCB.setData( itemId.toString() + ";group" );
            for (String g : groupMap.keySet()) {
                gCB.addItem(g);
            }
            gCB.addValueChangeListener( new ValueChangeListener() {
                @Override
                public void valueChange( ValueChangeEvent event ) {
                    String[] sArray = ((String) gCB.getData()).split( ";" );
                    String id = sArray[0] + ";user";
                    Iterator<Component> iterator = tt.iterator();
                    while ( iterator.hasNext() ) {
                        Component c = iterator.next();
                        if ( c instanceof ComboBox ) {
                            ComboBox cBox = (ComboBox) c;
                            if (cBox.getData().equals(id)) {
                                cBox.removeAllItems();
                                for (String u : groupMap.get(event.getProperty().getValue())) {
                                    cBox.addItem(u);
                                }
                            }
                        }
                    }
                }
            } );
            return gCB;
        }
    } );
    tt.addGeneratedColumn( "user", new ColumnGenerator() {
        @Override
        public Object generateCell( Table source, Object itemId, Object columnId ) {
            ComboBox uCB = new ComboBox();
            uCB.setData( itemId.toString() + ";user" );
            return uCB;
        }
    } );
    tt.setVisibleColumns( "group", "user" );
    addComponent( tt );

如果 GeneratedColumn 不是一个选项,那么您可以覆盖 bindPropertyToField:

    TreeTable tt = new TreeTable( null, container ) {
        @Override
        protected void bindPropertyToField( Object rowId, Object colId, Property property, Field field ) {
            if (field instanceof ComboBox) {
                ComboBox cb = (ComboBox) field;
                cb.setData(rowId.toString() + ";" + colId.toString());
            }
            super.bindPropertyToField( rowId, colId, property, field );
        }
    };
于 2013-11-11T14:53:35.777 回答