使用Vaadin 7将数学运算的结果实时设置到Vaadin网格中(手动插入)

使用Vaadin 7将数学运算的结果实时设置到Vaadin网格中(手动插入),vaadin,Vaadin,我的问题主要是,我想从键盘向网格中插入数据。一些数据是从数据库加载的,用户可以更改(也可以更改加载的数据)或将新数据插入网格(手动)。然后我想查看最后一行中各列的结果(检查图片中的结果)-结果表示行中的数字,其中第一列中的数据为-总和、平均值、最小值、最大值 因此,当我点击第三行,进入第三列,例如Person 3,当我将值从5更改为6时,此时,总和将为16,最大值为6,最小值为1,平均值为3.2 我的网格代码是: Grid grid = new Grid(); IndexedContaine

我的问题主要是,我想从键盘向网格中插入数据。一些数据是从数据库加载的,用户可以更改(也可以更改加载的数据)或将新数据插入网格(手动)。然后我想查看最后一行中各列的结果(检查图片中的结果)-结果表示行中的数字,其中第一列中的数据为-总和、平均值、最小值、最大值

因此,当我点击第三行,进入第三列,例如Person 3,当我将值从5更改为6时,此时,总和将为16,最大值为6,最小值为1,平均值为3.2

我的网格代码是:

Grid grid = new Grid();

IndexedContainer container = new IndexedContainer();
grid.setContainerDataSource(container);

container.addContainerProperty("September", String.class, null);
container.addContainerProperty("Person1", String.class, null);
container.addContainerProperty("Person2", String.class, null);
container.addContainerProperty("Person3", String.class, null);
container.addContainerProperty("Person4", String.class, null);
container.addContainerProperty("Person5", String.class, null);
container.addContainerProperty("Person6", String.class, null);
container.addContainerProperty("Person7", String.class, null);
container.addContainerProperty("Person8", String.class, null);

for(int i = 0; i < 10; i++)
     container.addItem(i);

Item item = container.getItem(1);

item.getItemProperty("September").setValue("1.9.2017 Piatok");

item = container.getItem(2);
item.getItemProperty("September").setValue("2.9.2017 Sobota");

....
问题是当我尝试更改2个或更多单元格时

 container.addValueChangeListener(e -> {


                Item item = container.getItem(6);

                 if(!e.getProperty().equals(item.getItemProperty("Person3")))
                        item.getItemProperty("Person3").setValue(54 + "");

                 if(!e.getProperty().equals(item.getItemProperty("Person4")))
                    item.getItemProperty("Person4").setValue(65 + "");

          });
我怎么能修好它


这就是问题所在,我不希望此页脚在网格中始终可见。我只想在网格结束时看到页脚。你明白吗?

这已经在 原因是相同的,您正在
容器内调用
容器.addValueChangeListener
中的
container.setValue
,从而导致无休止的循环

不能调用
item.getItemProperty(“Person3”).setValue(sum)在侦听器中

您必须检查当前设置的项目是否在上次访问循环时尚未设置。 想象一下:

int otherValue = 10;

public setValue(int newValue) {
  int sum = newValue + otherValue; //this is your sum
  setValue(sum);    //of course this causes a StackOverFlowError
}
以下是一个适用于您的项目的解决方案:

if(!e.getProperty().equals(item.getItemProperty("Person3"))
    item.getItemProperty("Person3").setValue(sum);

这一点已在本报告中得到答复 原因是相同的,您正在
容器内调用
容器.addValueChangeListener
中的
container.setValue
,从而导致无休止的循环

不能调用
item.getItemProperty(“Person3”).setValue(sum)在侦听器中

您必须检查当前设置的项目是否在上次访问循环时尚未设置。 想象一下:

int otherValue = 10;

public setValue(int newValue) {
  int sum = newValue + otherValue; //this is your sum
  setValue(sum);    //of course this causes a StackOverFlowError
}
以下是一个适用于您的项目的解决方案:

if(!e.getProperty().equals(item.getItemProperty("Person3"))
    item.getItemProperty("Person3").setValue(sum);

假设到现在为止,您已经通过使用
ValueChangeListener
来更新sum行,找到了获得SOE的原因,那么您可以使用类似的方法通过使用编辑器来克服这个问题,因此当保存编辑器更改时,sum行将被更新。为简洁起见,下面的示例仅计算总和,但您可以对其余操作应用类似的逻辑:

public class GridWithCalculatedRow extends VerticalLayout {

    public GridWithCalculatedRow() {
        // indexed container allowing the definition of custom properties
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("September", String.class, null);
        container.addContainerProperty("Person1", Integer.class, 0);
        container.addContainerProperty("Person2", Integer.class, 0);
        container.addContainerProperty("Person3", Integer.class, 0);


        // add some dummy data
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            Item item = container.addItem(i);
            item.getItemProperty("September").setValue(String.valueOf(i));
            item.getItemProperty("Person1").setValue(random.nextInt(10));
            item.getItemProperty("Person2").setValue(random.nextInt(10));
            item.getItemProperty("Person3").setValue(random.nextInt(10));
        }

        Item sumItem = container.addItem(6);
        sumItem.getItemProperty("September").setValue("Sum");

        // basic grid setup
        Grid grid = new Grid();
        grid.setContainerDataSource(container);
        grid.getColumn("September").setEditable(false);
        addComponent(grid);

        // initial sum
        updateSum(container, sumItem);

        // disable editing of sum item
        grid.addItemClickListener(event -> {
            if (event.getItemId().equals(6)) {
                grid.setEditorEnabled(false);
            } else {
                grid.setEditorEnabled(true);
            }
        });


        // editor commit handler to update the sum
        grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
            @Override
            public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                // nothing to do here for now
            }

            @Override
            public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                updateSum(container, sumItem);
            }
        });

    }

    // recalculate all sums and update the sum item
    private void updateSum(IndexedContainer container, Item sumItem) {
        Integer person1Sum = 0;
        Integer person2Sum = 0;
        Integer person3Sum = 0;

        // calculate sums
        for (int i = 0; i < 5; i++) {
            Item item = container.getItem(i);
            person1Sum += (Integer) item.getItemProperty("Person1").getValue();
            person2Sum += (Integer) item.getItemProperty("Person2").getValue();
            person3Sum += (Integer) item.getItemProperty("Person3").getValue();

        }

        // update grid item
        sumItem.getItemProperty("Person1").setValue(person1Sum);
        sumItem.getItemProperty("Person2").setValue(person2Sum);
        sumItem.getItemProperty("Person3").setValue(person3Sum);
    }
}
公共类GridWithCalculatedRow扩展垂直布局{
公共网格WithCalculatedRow(){
//允许定义自定义属性的索引容器
IndexedContainer容器=新的IndexedContainer();
container.addContainerProperty(“九月”,String.class,null);
container.addContainerProperty(“Person1”,Integer.class,0);
container.addContainerProperty(“Person2”,Integer.class,0);
container.addContainerProperty(“Person3”,Integer.class,0);
//添加一些虚拟数据
随机=新随机();
对于(int i=0;i<5;i++){
项目=容器。附加项(i);
item.getItemProperty(“九月”).setValue(String.valueOf(i));
item.getItemProperty(“Person1”).setValue(random.nextInt(10));
item.getItemProperty(“Person2”).setValue(random.nextInt(10));
item.getItemProperty(“Person3”).setValue(random.nextInt(10));
}
Item sumItem=容器。附加项(6);
sumItem.getItemProperty(“九月”).setValue(“总和”);
//基本网格设置
网格=新网格();
grid.setContainerDataSource(容器);
grid.getColumn(“九月”).setEditable(false);
添加组件(网格);
//初值
updateSum(容器、sumItem);
//禁用总和项目的编辑
grid.addItemClickListener(事件->{
if(event.getItemId()等于(6)){
grid.setEditorEnabled(false);
}否则{
grid.setEditorEnabled(true);
}
});
//编辑器提交处理程序以更新总和
grid.getEditorFieldGroup().addCommitHandler(新的FieldGroup.CommitHandler()){
@凌驾
public void preCommit(FieldGroup.committeevent committeevent)抛出FieldGroup.committeception{
//现在没什么事可做
}
@凌驾
public void postCommit(FieldGroup.committeevent committeevent)抛出FieldGroup.committeception{
updateSum(容器、sumItem);
}
});
}
//重新计算所有总和并更新总和项目
私有void updateSum(IndexedContainer容器,Item sumItem){
整数person1Sum=0;
整数person2Sum=0;
整数person3Sum=0;
//算数
对于(int i=0;i<5;i++){
Item Item=container.getItem(i);
person1Sum+=(整数)item.getItemProperty(“Person1”).getValue();
person2Sum+=(整数)item.getItemProperty(“Person2”).getValue();
person3Sum+=(整数)item.getItemProperty(“Person3”).getValue();
}
//更新网格项
sumItem.getItemProperty(“Person1”).setValue(person1Sum);
sumItem.getItemProperty(“Person2”).setValue(person2Sum);
sumItem.getItemProperty(“Person3”).setValue(person3Sum);
}
}
结果:

假设您现在已经通过使用
ValueChangeListener
来更新求和行,找到了获得SOE的原因,那么您可以通过使用编辑器的类似方法来克服这个问题,因此当保存编辑器更改时,求和行将被更新。为简洁起见,下面的示例仅计算总和,但您可以对其余操作应用类似的逻辑:

public class GridWithCalculatedRow extends VerticalLayout {

    public GridWithCalculatedRow() {
        // indexed container allowing the definition of custom properties
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("September", String.class, null);
        container.addContainerProperty("Person1", Integer.class, 0);
        container.addContainerProperty("Person2", Integer.class, 0);
        container.addContainerProperty("Person3", Integer.class, 0);


        // add some dummy data
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            Item item = container.addItem(i);
            item.getItemProperty("September").setValue(String.valueOf(i));
            item.getItemProperty("Person1").setValue(random.nextInt(10));
            item.getItemProperty("Person2").setValue(random.nextInt(10));
            item.getItemProperty("Person3").setValue(random.nextInt(10));
        }

        Item sumItem = container.addItem(6);
        sumItem.getItemProperty("September").setValue("Sum");

        // basic grid setup
        Grid grid = new Grid();
        grid.setContainerDataSource(container);
        grid.getColumn("September").setEditable(false);
        addComponent(grid);

        // initial sum
        updateSum(container, sumItem);

        // disable editing of sum item
        grid.addItemClickListener(event -> {
            if (event.getItemId().equals(6)) {
                grid.setEditorEnabled(false);
            } else {
                grid.setEditorEnabled(true);
            }
        });


        // editor commit handler to update the sum
        grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
            @Override
            public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                // nothing to do here for now
            }

            @Override
            public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                updateSum(container, sumItem);
            }
        });

    }

    // recalculate all sums and update the sum item
    private void updateSum(IndexedContainer container, Item sumItem) {
        Integer person1Sum = 0;
        Integer person2Sum = 0;
        Integer person3Sum = 0;

        // calculate sums
        for (int i = 0; i < 5; i++) {
            Item item = container.getItem(i);
            person1Sum += (Integer) item.getItemProperty("Person1").getValue();
            person2Sum += (Integer) item.getItemProperty("Person2").getValue();
            person3Sum += (Integer) item.getItemProperty("Person3").getValue();

        }

        // update grid item
        sumItem.getItemProperty("Person1").setValue(person1Sum);
        sumItem.getItemProperty("Person2").setValue(person2Sum);
        sumItem.getItemProperty("Person3").setValue(person3Sum);
    }
}
公共类GridWithCalculatedRow扩展垂直布局{
公共网格WithCalculatedRow(){
//允许定义自定义属性的索引容器