1

enter code hereI have a celltable and the columns contains some numbers. I want to add an extra row at the end of the table which will hold the total for each column. Is there any way to do this?

Following is my code:

   import java.util.*;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListDataProvider;

public class TestProject implements EntryPoint 
{

 private static int totalSalary=0;
 private static class Contact 
 {
        private final String salary;
        private final String name;

        public Contact(String name, String salary) 
        {
          this.name = name;
          this.salary = salary;
        }
 }

private static List<Contact> CONTACTS = Arrays.asList(new Contact("John","100000"), 
                                                      new Contact("Mary", "200000"), 
                                                      new Contact("Zander", "300000"));
/**
 * This is the entry point method.
 */
public void onModuleLoad() 
{
    final CellTable<Contact> table = new CellTable<Contact>();

    // Create name column.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() 
    {
      @Override
      public String getValue(Contact contact) 
      {
        return contact.name;
      }
    };

    // Create address column.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() 
    {
      @Override
      public String getValue(Contact contact) 
      {
        totalSalary+=Integer.parseInt(contact.salary);
        return contact.salary;
      }
    };

    // Add the columns.
    table.addColumn(nameColumn, "Name");
    table.addColumn(addressColumn, "Salary");

    // Create a data provider.
    ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);

    // Add the data to the data provider, which automatically pushes it to the
    // widget.
    final List<Contact> list = dataProvider.getList();
    for (Contact contact : CONTACTS) {
      list.add(contact);
    }

    // We know that the data is sorted alphabetically by default.
    table.getColumnSortList().push(nameColumn);

    Contact total = new Contact("Total: ",totalSalary+"");
    list.add(total);



    // Add it to the root panel.
    RootPanel.get().add(table);
    //RootPanel.get().add(add);
}

}

sap
  • 1,141
  • 6
  • 41
  • 62
  • Is your cell table connected to a data provider? – Chris Aug 18 '11 at 14:06
  • @Chris, I posted my code. Thanks in advance. – sap Aug 18 '11 at 14:17
  • What do you mean by totals though I am posting answer – Chris Aug 18 '11 at 14:24
  • @Chris, I updated my code. The table now contains employee name and salary. The last row should display the total of all the salaries. I've crated a static variable "totalSalary", but it's coming up 0. – sap Aug 18 '11 at 14:43
  • it works if I add a getter for salary and do a for loop to add all the salaries to totalSalary. Why can't I add the salaries when I'm creating the salary coloumn? – sap Aug 18 '11 at 14:52
  • Do this for(Contact c : list){ totalSalary += Integer.parseInt(c.salary); } Instead of in the column do this just after u add eveerything to list – Chris Aug 18 '11 at 14:53
  • Yes this is meant to return a value only there should be no logic within these methods it is typically bad practice and leads to messy and hard to understand code. – Chris Aug 18 '11 at 14:58

3 Answers3

2

Also I would suggest that you use the footer-Argument when you add the column: addColumn(Column col, Header header)

Chris
  • 1,119
  • 1
  • 8
  • 26
1

When you mean totals im not quite sure what you mean but this is similiar to your code but I have added a button that will add the row you howeve can take this out and just add the row.

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestGwt implements EntryPoint {

     private static class Contact {
            private final String address;
            private final String name;

            public Contact(String name, String address) {
              this.name = name;
              this.address = address;
            }
          }

    private static List<Contact> CONTACTS = Arrays.asList(new Contact("John",
    "123 Fourth Road"), new Contact("Mary", "222 Lancer Lane"), new Contact(
    "Zander", "94 Road Street"));
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
         // Create a CellTable.
        final CellTable<Contact> table = new CellTable<Contact>();

        // Create name column.
        TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
          @Override
          public String getValue(Contact contact) {
            return contact.name;
          }
        };

        // Make the name column sortable.
        nameColumn.setSortable(true);

        // Create address column.
        TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
          @Override
          public String getValue(Contact contact) {
            return contact.address;
          }
        };

        // Add the columns.
        table.addColumn(nameColumn, "Name");
        table.addColumn(addressColumn, "Address");

        // Create a data provider.
        ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

        // Connect the table to the data provider.
        dataProvider.addDataDisplay(table);

        // Add the data to the data provider, which automatically pushes it to the
        // widget.
        final List<Contact> list = dataProvider.getList();
        for (Contact contact : CONTACTS) {
          list.add(contact);
        }

        // We know that the data is sorted alphabetically by default.
        table.getColumnSortList().push(nameColumn);

        Button add = new Button("Add Row");
        add.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                list.add(new Contact(Integer.toString(table.getRowCount()),Integer.toString(table.getRowCount())));
            }
        });


        // Add it to the root panel.
        RootPanel.get().add(table);
        RootPanel.get().add(add);

      }



}

Hopefully this will help I am not sure what you mean by total though anything can be added to that final field in the Guise of a contact even though it isnt neccessarily one , the better approach would be to use Generics for the data provider but this will achieve same effects with minimal code

Chris
  • 865
  • 4
  • 15
  • 25
0
public void addNewRow(){                

    List<Contact> newContactLst = Arrays.asList(new Contact("TEST",
              "Sample"));

    int numRows = table.getRowCount();

    table.setRowCount(numRows+1);

    table.setRowData(numRows,newContactLst);

}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • This would be a stronger answer if you explain the code. Don't just hand the OP a fish, teach him (and others with the same question) to fish. – Eric J. Feb 05 '13 at 20:43