0

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

这是一张图片

创建 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);

    }
};
4

1 回答 1

1

代替

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());
    }
});
于 2017-09-12T14:12:10.987 回答