如何从GWT中的动态FlexTable获取行值?

时间:2017-09-12 10:54:54

标签: java gwt flextable

我正在尝试开发一个使用jdbc从数据库动态创建flex表的应用程序。我想实现两个方法,一个用于更新行值,另一个用于通过每行插入两个按钮来删除当前行。问题是我无法获得当前行值。

Here is a picture

创建FlexTable的FishTable方法

public void FishTable(){
    if(service == null){
        service = GWT.create(Service.class);
    }

    service.getFishes(FishTableCallBack);
}

AsyncCallback FishTableCallBack = new AsyncCallback(){

    @Override
    public void onFailure(Throwable caught) {
        String details = caught.getMessage();

        System.out.println(details);
    }

    @Override
    public void onSuccess(FishTable result) {

        table.removeAllRows();          
        tablePanel.clear();

        table.setText(0, 0, "id");
        table.setText(0, 1, "Fish");
        table.setText(0, 2, "Price");
        table.setCellPadding(6);
        table.getCellFormatter().addStyleName(0, 0, "header1_3");
        table.getCellFormatter().addStyleName(0, 1, "header2");
        table.getCellFormatter().addStyleName(0, 2, "header1_3");

        tablePanel.add(table);

        for (int i = 0; i < result.getIdArray().length; i++) {

            Button updateBtn = new Button("update");
            Button deleteBtn = new Button("delete");

            id = new TextBox();
            id.setText(result.getIdArray()[i]);
            id.addStyleName("fishId");
            id.setReadOnly(true);

            name = new TextBox();
            name.setText(result.getNameArray()[i]);

            price = new TextBox();
            price.setText(result.getPriceArray()[i]);
            price.addStyleName("fishId");


            int row = table.getRowCount();

            table.setWidget(row, 0, id);
            table.setWidget(row, 1, name);
            table.setWidget(row, 2, price);
            table.setWidget(row, 3, updateBtn);
            table.setWidget(row, 4, deleteBtn);


            updateBtn.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    UpdateFish(id.getText() , name.getText() , price.getText());

                }
            });

            deleteBtn.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    index = table.getCellForEvent(event).getRowIndex()-1;                       

                    DeleteFish(table.getText(index, 0));

                }
            });
        }

    }


}; 

updateBtn处理程序中的代码不起作用,我从最后一行获取值。我尝试使用index = table.getCellForEvent(event).getRowIndex() - 1获取当前行,但我无法在updateBtn处理程序中获取第0列的值。我想将这些值发送到数据库。那么我如何才能从单击这些按钮的行中获取值?

UpdateFish和DeleteFish方法:

public void DeleteFish(String id){
    if(service == null){
        service = GWT.create(Service.class);
    }

    service.deleteFish(id, deleteFishCallBack);
}

public void UpdateFish(String fishId , String fishName , String fishPrice){
    if(service == null){
        service = GWT.create(Service.class);
    }

    service.updateFish(fishId , fishName , fishPrice , updateFishCallBack);
}

AsyncCallback<DeleteFish> deleteFishCallBack = new AsyncCallback<DeleteFish>() {

    @Override
    public void onSuccess(DeleteFish result) {          
        FishTable();
    }

    @Override
    public void onFailure(Throwable caught) {
        String details = caught.getMessage();

        System.out.println(details);

    }
};

AsyncCallback<UpdateFish> updateFishCallBack = new AsyncCallback<UpdateFish>() {

    @Override
    public void onSuccess(UpdateFish result) {
        FishTable();
    }

    @Override
    public void onFailure(Throwable caught) {
        String details = caught.getMessage();

        System.out.println(details);

    }
};

1 个答案:

答案 0 :(得分:0)

替换

int row = table.getRowCount();

final int row = table.getRowCount();  

然后再做

deleteBtn.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        TextBox tb = (TextBox) table.getWidget(row - 1, 0);
        DeleteFish(tb.getText());
    }
});