1

I was creating a web application in wicket and had created a table which shows the user some information. Now I wanted to manipulate this table so if the cell contained "N" the background color would be RED and if it contained "Y" the background color would be GREEN. At the moment I was having trouble to determine what is actually inside the cell. I create my table by the following:

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;
        }
    };

I am capable of determining the cell which I wanna now check what is being displayed to the user. Any help on how i can do this? to change the color i know I'll have to add item.add(AttributeModifier.replace("bgcolor", "red")); but don't know how to tell whats inside the cell

Attiq
  • 155
  • 1
  • 3
  • 8

3 Answers3

1

Item is extending list item, so you can try .getModelObject and validate it if it is "X" or "Y"

http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/repeater/Item.html

http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/html/list/ListItem.html#getModelObject()

Arek Wołk
  • 21
  • 3
  • how would i validate though? getModelObject gives me something like "org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn@1b7291b0" – Attiq Apr 22 '15 at 19:14
1

You should do your checks in the IColumn implementation. https://github.com/apache/wicket/blob/24e9db6c8af85043ce36e4d25a0e8a2d8dc2f49e/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/PropertyColumn.java#L94 populates the Item with a Label. You need to add a AttributeModifier to the Label.

You can also achieve your goal with pure JavaScript and/or CSS at the client side.

martin-g
  • 17,243
  • 2
  • 23
  • 35
0

This example extracts the cell value when a cell is clicked in a Wicket DataView. The model for this DataView is a Map with String keys and Integer values: Map<String,Integer>.

The PropertyColumn list is created using with column headers ("ALPHA", "BETA", "GAMMA") and property expressions: "alpha", "beta", "gamma". PropertyColumn uses the expressions to retrieve the values from the map.

The DataView is created with the list of PropertyColumns and a DataProvider. DataView uses the DataProvider to populate the PropertyColumn when the table is rendered and reacts to clicks to expose the cell values.

Cells are exposed by overriding the newCellItem(String,int,IModel) method and calling the super-class method to get the cell. This example adds a behavior to react to "onclick" events. Within the event, the cell's first child component should be the Label used to display the cell value.

The innermost model of the cell Label is the PropertyModel from the PropertyColumn.

  1. innerModel.getPropertyExpression(): Our data map key (String).
  2. innerModel.getObject(): The data value (Integer).
  3. innerModel.getInnermostModelOrObject(): The list item (Map<String,Integer>).

Wicket DataView: Extract Cell Value

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;
        }
    }

}

Output

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}
Rick O'Sullivan
  • 351
  • 3
  • 7