wicket操纵DefaultDataTable

时间:2015-04-22 17:42:47

标签: datatable wicket

我在wicket中创建了一个Web应用程序,并创建了一个向用户显示一些信息的表。现在我想操作这个表,所以如果单元格包含“N”,则背景颜色为红色,如果包含“Y”,则背景颜色为绿色。目前我无法确定细胞内部究竟是什么。我通过以下方式创建我的表:

dataTable = new DefaultDataTable<TableModalInt, String>("table", columns,
            new TableModalProvider(), 100000){
        @SuppressWarnings("rawtypes")
        @Override
        protected Item newCellItem(String id, int index, IModel model) {
            Item item = super.newCellItem(id, index, model); 
            if (id == "3"){
                item.add(AttributeModifier.replace("align", "center"));
            }
            return item;
        }
    };

我能够确定我想要检查用户正在显示的内容的单元格。有关我如何做到这一点的任何帮助?改变颜色,我知道我必须添加item.add(AttributeModifier.replace("bgcolor", "red"));,但不知道如何告诉细胞内的什么

2 个答案:

答案 0 :(得分:1)

您应该在IColumn实施中进行检查。 https://github.com/apache/wicket/blob/24e9db6c8af85043ce36e4d25a0e8a2d8dc2f49e/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/PropertyColumn.java#L94使用标签填充项目。您需要将一个AttributeModifier添加到Label。

您还可以在客户端使用纯JavaScript和/或CSS实现目标。

答案 1 :(得分:0)

此示例在Wicket DataView中单击单元格时提取单元格值。此DataView的模型是Map String个密钥和Integer值:Map<String,Integer>

使用列标题(“ALPHA”,“BETA”,“GAMMA”)和属性表达式:“alpha”,“beta”,“gamma”创建PropertyColumn列表”。 PropertyColumn使用表达式从地图中检索值。

使用DataViewPropertyColumn列表创建DataProviderDataView在呈现表格时使用DataProvider填充PropertyColumn,并对点击作出反应以公开单元格值。

通过覆盖newCellItem(String,int,IModel)方法并调用超类方法来获取单元格来公开单元格。此示例添加了一个行为以响应“onclick”事件。在事件中,单元格的第一个子组件应该是用于显示单元格值的Label

单元格Label的最内层模型是PropertyModel中的PropertyColumn

  1. innerModel.getPropertyExpression():我们的数据映射键(String)。
  2. innerModel.getObject():数据值(Integer)。
  3. innerModel.getInnermostModelOrObject():列表项(Map<String,Integer>)。
  4. Wicket DataView:提取单元格值

    public class MessageLogStatus
        extends WebPage
    {
        /** Represents serialVersionUID. */
        private static final long serialVersionUID = 20150701L;
        private static final Logger log = LoggerFactory.getLogger(MessageLogStatus.class);
    
        static final String A = "alpha";
        static final String B = "beta";
        static final String C = "gamma";
    
        public MessageLogStatus()
        {
            super();
    
            final List<String> keys = Arrays.asList(A, B, C);
    
            final List<Map<String,Integer>> data = Arrays.asList
            (
                map(A, 1).put(B, 11).put(C, 21).toMap(),
                map(A, 2).put(B, 12).put(C, 22).toMap(),
                map(A, 3).put(B, 13).put(C, 23).toMap(),
                map(A, 4).put(B, 14).put(C, 24).toMap(),
                map(A, 5).put(B, 15).put(C, 25).toMap(),
                map(A, 6).put(B, 16).put(C, 26).toMap(),
                map(A, 7).put(B, 17).put(C, 27).toMap(),
                map(A, 8).put(B, 18).put(C, 28).toMap(),
                map(A, 9).put(B, 19).put(C, 29).toMap()
            );
    
            // Using a DefaultDataTable
            ISortableDataProvider<Map<String,Integer>,String> dataProvider = new SortableDataProvider<Map<String,Integer>,String>()
            {
                private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
    
                public Iterator<Map<String,Integer>> iterator(long first, long count)
                {
                    int start = Math.max(0, (int) first);
                    int end = Math.min(data.size(), start + (int) count);
                    return data.subList(start, end).iterator();
                }
    
                public long size()
                {
                    return data.size();
                }
    
                public IModel<Map<String,Integer>> model(Map<String,Integer> object)
                {
                    return new CompoundPropertyModel<Map<String,Integer>>(object);
                }
            };
    
            List<PropertyColumn<Map<String,Integer>,String>> columns = new ArrayList<PropertyColumn<Map<String,Integer>,String>>();
            for (String key : keys)
            {
                columns.add
                (
                    new PropertyColumn<Map<String,Integer>, String>(Model.of(key.toUpperCase()), key)
                    {
                        private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
    
                        @Override
                        public void populateItem(Item<ICellPopulator<Map<String, Integer>>> item, String componentId,
                            IModel<Map<String, Integer>> rowModel)
                        {
                            super.populateItem(item, componentId, rowModel);
                            Map<String, Integer> entity = rowModel.getObject();
                            String px = getPropertyExpression();
                            PropertyModel<Object> propModel = new PropertyModel<Object>(rowModel, px);
                            log.info("Add Label to Cell: PropEx="+px+", Value="+propModel.getObject()+", entity="+entity);
                        }
                    }
                );
            }    
    
            //
            // Wicket: <table wicket:id="dataTable"></table>
            //
            DataTable<Map<String,Integer>,String> dataTable = 
                new DataTable<Map<String,Integer>,String>("dataTable", columns, dataProvider, 5)
                {
                    private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
    
                    @Override
                    protected Item<IColumn<Map<String, Integer>, String>> newCellItem(final String id, final int index,
                        final IModel<IColumn<Map<String, Integer>, String>> model)
                    {
                        final Item<IColumn<Map<String,Integer>, String>> cell = super.newCellItem(id, index, model);
                        cell.add
                        (
                            new AjaxEventBehavior("onclick")
                            {
                                private static final long serialVersionUID = MessageLogStatus.serialVersionUID;
                                @SuppressWarnings("unchecked")
                                @Override
                                protected void onEvent(AjaxRequestTarget target)
                                {
                                    if ( (cell.size() > 0) && (cell.get(0) instanceof Label) )
                                    {
                                        Label cellLabel = (Label) cell.get(0);
                                        PropertyModel<Integer> cellLabelModel = (PropertyModel<Integer>) cellLabel.getInnermostModel();
                                        String property = cellLabelModel.getPropertyExpression();
                                        Integer value = cellLabelModel.getObject(); 
                                        Map<String, Integer> entity = (Map<String,Integer>) cellLabelModel.getInnermostModelOrObject();
    
                                        log.info("OnClick: Index="+index+", PropEx="+property+", Value="+value+", Entity="+entity);
                                    }
                                }
                            }
                        );
                        return cell;
                    }
                };
            dataTable.addBottomToolbar(new NavigationToolbar(dataTable));
            dataTable.addTopToolbar(new HeadersToolbar<String>(dataTable, null));
            add(dataTable);
        }
    
        // Make building the data structure a little more fun :)
        private MapBuilder<String, Integer> map(String key, Integer value)
        {
            return new MapBuilder<String, Integer>().put(key, value);
        }
    
        private static class MapBuilder<K, V>
        {
            Map<K, V> map = new HashMap<K, V>();
    
            MapBuilder<K, V> put(K key, V value)
            {
                map.put(key, value);
                return this;
            }
    
            Map<K, V> toMap()
            {
                return map;
            }
        }
    
    }
    

    <强>输出

    OnClick: Index=0, PropEx=alpha, Value=5, Entity={gamma=25, alpha=5, beta=15}
    OnClick: Index=1, PropEx=beta, Value=15, Entity={gamma=25, alpha=5, beta=15}
    OnClick: Index=2, PropEx=gamma, Value=25, Entity={gamma=25, alpha=5, beta=15}