Question

I am using smartgwt ( not the paid license version ) and I have a listgrid with three entries. Key, Value, Reset.

The reset-field is a button that should reset any changes to the value and here is where I struggle. I tried to implement it as simple as

@Override
public void onClick(ClickEvent event)
{
    DataSource ds = this.grid.getDataSource();
    ds.removeData(record);
    ds.fetchData();
    this.grid.redraw();
}

grid being my ListGrid and record the row that has been clicked to be reseted.

But this only removes the entry, it is there again if I reload ( even with the right value , because that is what my server does if he gets remove-requests), but I would like that it is there immediately after I click the button and not after clicking around a bit.

I assumed the fetchData and redraw request combined would accomplish this.

edit: Okay some more code, this shows my constructor for the ListGrid and the RevertButton which should remove and add the Record again.

    private static final String REVERT_NAME = "revertField";

    public MyListGrid(final String name)
    {
        this.setDataSource(PropertyListDS.getInstance(name);

        ListGridField keyField = new ListGridField(ConfigurationDataSourceFields.PROPERTY_NAME, "Property");
        ListGridField valueField = new ListGridField(ConfigurationDataSourceFields.PROPERTY_VALUE, "Value");
        ListGridField revertField = new ListGridField(REVERT_NAME, "Revert to Default");

        valueField.setCanEdit(true);

        this.setShowRecordComponents(true);        
        this.setShowRecordComponentsByCell(true);

        this.setAutoFetchData(true);

        this.setFields(keyField, valueField, revertField);
    }

    @Override
    protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum)
    {
        String fieldName = this.getFieldName(colNum);
        Canvas canvas = null;
        if ( REVERT_NAME.equals(fieldName) ) 
        {
            canvas = new RevertButton(this, record);
        }
        return canvas;
    }

    private class RevertButton extends IButton implements ClickHandler
    {
        private final MyListGrid grid;
        private final ListGridRecord record;

        public RevertButton(final MyListGrid grid, final ListGridRecord record)
        {
            super();
            this.setTitle("Revert to Default");
            this.grid = grid;
            this.record = record;
            this.addClickHandler(this);
        }

        @Override
        public void onClick(ClickEvent event)
        {
            DataSource ds = this.grid.getDataSource();
            ds.removeData(record);
            ds.fetchData();
            this.grid.redraw();
        }
    }
Was it helpful?

Solution

Do in this way using DSCallback.

DataSource#removeData() is a async call to the server. Either redraw the grid again or fetch the data again after getting response from server that record has been deleted in DSCallback.

DataSource dataSource = grid.getDataSource();
dataSource.removeData(record,new DSCallback() {

    @Override
    public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest){
        Record[] records=dsResponse.getData();//deleted records

        grid.fetchData();//fetch data again
    }
});

Please have a look at this thread Removing local record from listGrid without committing

Try with ListGrid#saveAllEdits() before fetching the data again.

You can try with ListGrid#removeSelectedData() to remove the currently selected records from this component. If this is a databound grid, the records will be removed directly from the DataSource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top