【问题标题】:gwt - celltable - adding extra rowgwt - celltable - 添加额外的行
【发布时间】:2011-08-18 13:48:12
【问题描述】:

在此处输入代码我有一个单元格表,列中包含一些数字。我想在表的末尾添加一个额外的行,它将保存每列的总数。有没有办法做到这一点?

以下是我的代码:

   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);
}

}

【问题讨论】:

  • 您的单元格表是否连接到数据提供者?
  • @Chris,我发布了我的代码。提前致谢。
  • 虽然我正在发布答案,但您所说的总计是什么意思
  • @Chris,我更新了我的代码。该表现在包含员工姓名和薪水。最后一行应该显示所有工资的总和。我已经创建了一个静态变量“totalSalary”,但它的值为 0。
  • 如果我为薪水添加一个getter并执行一个for循环将所有薪水添加到totalSalary,它就可以工作。为什么我在创建工资栏时不能添加工资?

标签: gwt gwt-2.2-celltable


【解决方案1】:

另外,我建议您在添加列时使用 footer-Argument: addColumn(Column col, Header header)

【讨论】:

    【解决方案2】:

    当您的意思是总计时,我不太清楚您的意思,但这与您的代码相似,但我添加了一个按钮,可以添加行,但您可以将其取出并添加行。

    /**
     * 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);
    
          }
    
    
    
    }
    

    希望这将有助于我不确定您所说的总计是什么意思,尽管任何东西都可以以联系人的名义添加到最后一个字段中,即使它不一定是一个,更好的方法是使用泛型作为数据提供者但这将以最少的代码实现相同的效果

    【讨论】:

      【解决方案3】:
      public void addNewRow(){                
      
          List<Contact> newContactLst = Arrays.asList(new Contact("TEST",
                    "Sample"));
      
          int numRows = table.getRowCount();
      
          table.setRowCount(numRows+1);
      
          table.setRowData(numRows,newContactLst);
      
      }
      

      【讨论】:

      • 如果您解释代码,这将是一个更有力的答案。不要只是给 OP 一条鱼,教他(和其他有相同问题的人)钓鱼。
      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 2023-03-23
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多