1

So I am a little confused with how to set the css of an individual row in my Datagrid (which as i understand it can use everything the cellTable can use).

First I have a double click and single click handler which do get correctly called in 'onCellPreview'(had to remove selectionModel for this to work). I then can get the row number through event.getIndex().

Next is where I fail in attempt to set the CSS for the selected row, my code:

int Row= event.getIndex();

myDataTable.setRowStyles(new RowStyles<String[]>(){
   @Override
   public String getStyleNames(String [] rowObject, int theRow){
      if (row== theRow){
          return "myDataGridSelectedRow";
      }
      else 
          return "myDataGrid";
   }
});

However I don't understand how this is supposed to work (which is perhaps why I can't get it to work)

  1. How does getStyleNames() get called? It just steps over it in my debugger.
  2. where should the setRowStyles method call be located in my code?
  3. I suppose for this to work properly i must remove the call

     myDataTable.setStyleName("myDataGrid");
    
  4. There is also a pre-existing css class in my DataGridOverride.css(which is different from the main css file where "myDataGrid" is located). The class is ".dataGridSelectedRowCell" and has a background color set (uses !important) but it does not work.
TheJavaBeast
  • 163
  • 3
  • 16

1 Answers1

2

This method setRowStyles() is called only when a table is rendered.

You can set a style dynamically this way:

myDataTable.getRowElement(i).getStyle()...

or

myDataTable.getRowElement(i).setClassName("myDataGridSelectedRow");

You do not need to remove style name from your grid.

EDIT:

An alternative approach is to override the standard DataGrid CSS Resource:

How do I style a gwt 2.1 CellTables headers?

I would recommend this approach if you want to make many changes to default GWT DataGrid styles.

Community
  • 1
  • 1
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Well that does explain why setRowStyles() was never called, thanks for clearing that up! However, I tried myDataTable.getRowElement(row).setClassName("myDataGridSelectedRow"); and event though it got the corrected row Element information and everything, nothing changed. I thought maybe some css elsewhere could be overwriting it, so i removed all the other CSS and still no effect. Any idea what could cause this? – TheJavaBeast Mar 21 '14 at 00:41
  • DataGrid has its own CSS. Add "!important" to your CSS properties. Alternatively, you can pass a CSSResource when constructing your DataGrid - I will update my response with this option. – Andrei Volgin Mar 21 '14 at 01:10
  • I already have css resources and tried calling: myDataTable.getRowElement(row).setClassName("DataGridSelectedRow"); and it did not work. Furthermore i removed any of the other background styling i could find for the data grid to ensure it wasnt an overwritting issue (the cell table background is now defulted to background color of page). I also have been using !important the whole time and left it in my css for these changes... Still nothing works! – TheJavaBeast Mar 21 '14 at 17:06
  • Try to set the background color directly, using .getStyle().setBackgroundColor("red"). If it does not work, then something totally different is at work. Note that I use this code in production app, and it works as expected in all browsers. – Andrei Volgin Mar 21 '14 at 17:18
  • I tried that too and nothing... However i did finally find a solution! I removed the cellPreview double click counter i was using and tried the DOM solution with the SingleSelectionModel. Somehow as soon as i did that the on selection finally started to work! So while Your answer did not solve my problem completely, It certainly helped quite a bit. Thank You! – TheJavaBeast Mar 21 '14 at 22:16
  • It's possible that the code you removed caused the table to refresh. As a result, the styles you were setting were being lost. If you need to put this code back in, consider a different solution that does not cause constant refreshing. I am guessing here, but it's worth another look. – Andrei Volgin Mar 21 '14 at 22:18