Cassio
Cassio

Reputation: 147

GWT RPC match data for CellTable

I have a GWT 2.4 project using a CellTable. It has columns like this (actually more):

LastName --- FirstName --- Departments
Smith        Tom           Research, Management

The names I get from a "User" object which is created on the server from my Database.

The DB looks like this:

users:
  userID
  firstName
  lastName

departments:
  departmentID
  departmentName

user_in_department:
  userID
  departmentID

So what is the best way to get my departments show up in the table?

At the moment I fetch the User-list and the Department-list from the server using a RPC.

I thought about a 3rd RPC to get me the user-department relation and then match the names to the users on the client. What would be a good way to match that btw?

But even if I had the departments matched to my users, how would I add that info to the table?

For the names I can just do that:

    TextColumn<User> firstNameColumn = new TextColumn<User>() {

        @Override
        public String getValue(User object) {
            return object.getFirstName();
        }
    };

But as the departments aren't stored in the "User" object, I have no idea how to get them in the correct column and row.

I hope I've explained my issue good enough for you to understand :)

Upvotes: 1

Views: 346

Answers (2)

Jasper Sprengers
Jasper Sprengers

Reputation: 531

In addition to the first answer: since a CellTable is parameterized to display a list of objects of a given type in its rows that object should include the user name as well as a list of departments. You'll have to somehow make that data available through the User object, or create a new one (e.g. UserDepartmentDTO) as the elements in the underlying model.

Upvotes: 0

Strelok
Strelok

Reputation: 51481

Assuming that your User object has a list of departments like so:

public ArrayList<Department> getDepartments() {
  // ...
}

You can create a column to list the departments like so:

TextColumn<User> departmentsColumn = new TextColumn<User>() {

    @Override
    public String getValue(User object) {
        StringBuilder departments = new StringBuilder();
        for(int i=0; i < object.getDepartments().size(); i++) {
            if (i>0) {
              departments.append(",");
            }
            departments.append(object.getDepartments().get(i).getDepartmentName());
        }
        return departments.toString();
    }
};

Upvotes: 0

Related Questions