2015-04-22 31 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}