2015-04-22 56 views
0

我在wicket中创建了一个web应用程序,并创建了一个表格来显示用户的一些信息。现在我想操作这个表格,所以如果单元格包含“N”,背景色将是红色,并且如果它包含“Y”,背景色将是绿色。目前我无法确定电池内部的实际情况。我通过以下方式创建我的表格:导叶操作DefaultDataTable

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"));但不知道如何分辨单元格内部的内容

+0

'item.getModelObject()'?或者直接看一下'model'参数。 – biziclop

+0

但它返回类似于“org.apache.w[email protected]29a198c9” – Attiq

+0

它不应该这样做。 – biziclop

回答

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添加到标签。

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

+0

我的java不是很强大。我将如何编辑properycolumn?我需要创建自己的班级吗? – Attiq

+0

是的。扩展它并覆盖此方法。 –

0

本示例在Wicket DataView中单击单元格时提取单元格值。此DataView的型号是MapString键和Integer值:Map<String,Integer>。 “阿尔法”, “测试”, “伽马”:

PropertyColumn列表与列标题( “ALPHA”, “BETA”, “伽玛”)和属性表达式使用创建的。 PropertyColumn使用表达式从地图中检索值。

DataView使用PropertyColumn s和DataProvider的列表创建。 DataView在表格呈现时使用DataProvider填充PropertyColumn,并对点击作出反应以显示单元格值。

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

单元Label的最内层模型是来自PropertyColumnPropertyModel

  1. innerModel.getPropertyExpression():我们的数据映射密钥(String)。 (数据值为Integer)。
  2. innerModel.getInnermostModelOrObject():列表项(Map<String,Integer>)。

检票数据视图:提取单元格值

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}