How to get row values from dynamic FlexTable in GWT?

669 views Asked by At

I'm trying to develope an application that creates a flex table dynamically from database with jdbc. I want to implement two methods, one to update row values and one to delete current row by inserting two buttons per row. The problem is that i can't get current row values.

Here is a picture

FishTable method that creates FlexTable

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

                }
            });
        }

    }


}; 

Code inside updateBtn handler doesn't work, i takes values from the last row. I tried index = table.getCellForEvent(event).getRowIndex()-1 to get the current row but i can't get the value of column 0 inside updateBtn handler. I want to send these values to database. So how could i get the values from the row that i click these buttons?

UpdateFish and DeleteFish methods:

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

There are 1 answers

2
Knarf On BEST ANSWER

replace

int row = table.getRowCount();

with

final int row = table.getRowCount();  

and then do

deleteBtn.addClickHandler(new ClickHandler() {

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