Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a row-number column to GWT CellTable

Tags:

row-number

gwt

I need to insert a new first-column into a CellTable, and display the RowNumber of the current row in it. What is the best way to do this in GWT?

like image 859
Opal Avatar asked Dec 03 '10 15:12

Opal


2 Answers

Get the index of the element from the list wrapped by your ListDataProvider. Like this:

final CellTable<Row> table = new CellTable<Row>();
final ListDataProvider<Row> dataProvider = new ListDataProvider<Starter.Row>(getList());
dataProvider.addDataDisplay(table);

TextColumn<Row> numColumn = new TextColumn<Starter.Row>() {

    @Override
    public String getValue(Row object) {
        return Integer.toString(dataProvider.getList().indexOf(object) + 1);
    }
};

See here for the rest of the example.

like image 58
z00bs Avatar answered Nov 15 '22 07:11

z00bs


Solution from z00bs is wrong, because row number calculating from object's index in data List. For example, for List of Strings with elements: ["Str1", "Str2", "Str2"], the row numbers will be [1, 2, 2]. It is wrong.

This solution uses the index of row in celltable for row number.

public class RowNumberColumn extends Column {

    public RowNumberColumn() {
        super(new AbstractCell() {
            @Override
            public void render(Context context, Object o, SafeHtmlBuilder safeHtmlBuilder) {
                safeHtmlBuilder.append(context.getIndex() + 1);
            }
        });
    }

    @Override
    public String getValue(Object s) {
        return null;
    }
}

and

cellTable.addColumn(new RowNumberColumn());
like image 40
Andrey Zhdankin Avatar answered Nov 15 '22 07:11

Andrey Zhdankin