GXT : Find cell's value in a grid by coordinates

1.2k views Asked by At

I have the coordinates of the cell (row, column indexes) of a grid. How can I retrieve the cell's value?

I don't want to retrieve the whole row, just that one cell with (x,y) coordinates.

I'm using gwt 2.6.1, gxt 3.x

1

There are 1 answers

0
Colin Alworth On BEST ANSWER

The easiest way is to get the row first, and from that, get the cell. The Grid<T> is not a spreadsheet - it is not just a bag of cells, or rows and columns, it is instead a fixed set of columns that describe how to read each row.

Finding the row (of type T) is pretty easy - using the row index, ask the ListStore<T> for the item with that index.

ListStore<MyRowData> store = ...
MyRowData row = store.get(rowIndex);

Then, we need the ColumnConfig<T, ?> that matches the column you want. This is where things get a bit interesting - what is that ?? Your specific usecase might mean that all columns are the same type, but this is not always the case, so the grid doesn't assume it. Let's say in our case though that we know that column with index 4 holds String values - then we can cast any ColumnConfig<T, ?> to ColumnConfig<T, String>.

Grid<MyRowData> grid = ...
ColumnModel<MyRowData> columnModel = grid.getColumnModel();
ColumnConfig<MyRowData, String> columnFour = columnModel.getColumn(columnIndex);

Now we have the row, and we have the column - the intersection of these is the specific cell you are after. To read out the value of that cell, we take the ValueProvider in the column (when you build the column, you provided this as an argument), and use it to read out the value in that column, for the row model.

MyRowData row = store.get(rowIndex);
//...
ColumnConfig<MyRowData, String> columnFour = columnModel.getColumn(columnIndex);

String cellValue = columnFour.getValueProvider().getValue(row);