Search code examples
gwtuibinder

GWT DataGrid: Unable to display data


I am trying to display data with GWT: Data grid, but it doesn't show up.

image

public class DataGridDemo extends Composite {

interface MyUiBinder extends UiBinder<Widget, DataGridDemo> {}
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
protected static UserServiceAsync userService = GWT.create(UserService.class);

  @UiField
  DataGrid<UserDTO> dataGrid;

  private List<UserDTO> userList;

  @UiField
  Pagination dataGridPagination;

  private SimplePager dataGridPager = new SimplePager();
  private ListDataProvider<UserDTO> dataGridProvider = new ListDataProvider<UserDTO>();


  public DataGridDemo(){

  initWidget(uiBinder.createAndBindUi(this));    
  loadUsers();

  }

  private void loadUsers() {
        AsyncCallback<List<UserDTO>> callback ;
        callback = new AsyncCallback<List<UserDTO>>() {
            public void onFailure(Throwable caught) {}
            public void onSuccess(List<UserDTO> users) {
                if(users!=null && users.size()>0){

                      userList=users;

                      initTable(dataGrid,dataGridPager, dataGridPagination, dataGridProvider);
                      initMockData(dataGridPagination, dataGridPager, dataGridProvider);

                }
            }
        };
        userService.getAllUsers(callback);
    }


  private void initMockData(final Pagination pagination, final SimplePager simplePager, final ListDataProvider<UserDTO> dataProvider) {
      for (UserDTO ud : userList) {
                                                 System.out.println(ud.getFullName());
      dataProvider.getList().add(ud);
      }

      dataProvider.flush();
      pagination.rebuild(simplePager);
      }

  private void initTable(final AbstractCellTable<UserDTO> grid, final SimplePager pager, final Pagination pagination, final ListDataProvider<UserDTO> dataProvider) {
      final TextColumn<UserDTO> col1 = new TextColumn<UserDTO>() {

      public String getValue(final UserDTO object) {
      return String.valueOf(object.getUsername());
      }
      };
      grid.addColumn(col1, "Username");
      final TextColumn<UserDTO> col2 = new TextColumn<UserDTO>() {

      public String getValue(final UserDTO object) {
      return String.valueOf(object.getFullName());
      }
      };
      grid.addColumn(col2, "Full Name");
      final TextColumn<UserDTO> col3 = new TextColumn<UserDTO>() {

      public String getValue(final UserDTO object) {
      return String.valueOf(object.getFundName());
      }
      };
      grid.addColumn(col3, "Fund Name");


      grid.addRangeChangeHandler(new RangeChangeEvent.Handler() {

          public void onRangeChange(final RangeChangeEvent event) {
          pagination.rebuild(pager);
          }
          });

      pager.setDisplay(grid);
      pagination.clear();
      dataProvider.addDataDisplay(grid);
      }

Solution

  • The DataGrid implements RequiresResize. Javadoc here

    That means you have to add to a Container that implements ProvidesResize with explicit height. Try a ScrollPanel or any listed from here

    More about this on here:

    GWT DataGrid automatic height

    GWT: DataGrid - set height 100% not rendering properly

    PS: I'm assuming this since I can't see your ui.xml file.