GWT:how to get the value of a checkbox of a cellTable - gwt

I have this situation , i want to delete the objects in this celltable whose checkbox is Check on the clik of this "Delete" Button ,
Any idea how to get those objects whose checkbox is checked in this cellTable, when i click the delte button ..
Thanks

#junaidp, As you haven't provided any code, I'm assuming that you have used CheckBoxCell to generate checkbox column and assigned MultiSelectionModel to your cellTable. You can use the following code:
Set<T> selectedObjects = ((MultiSelectionModel<T>)(cellTable.getSelectionModel())).getSelectedSet();
Here selectedObjects will be of type T, that you should have specified as CellTable<T>.
selectedObjects will be objects, associated to checked rows only.

You can check for a given cell's selected status with:
Column<CellInfo, Boolean> checkColumn =
new Column<MemberInfo, Boolean>(new CheckboxCell(true, false)) {
public Boolean getValue(CellInfo object) {
// Get the value from the selection model.
return selectionModel.isSelected(object);
}
};

If your requirement with delete single row, then You can use SingleSelectionModel otherwise MulitiSelectionModel in celltable. I have written some code with single selection model,It may give some idea. i.e.
selectionModel = new SingleSelectionModel<T>();
cellTable.setSelectionModel(selectionModel) //Set into your cellTable:
When you select a checkbox, then row will auto selected and object will set in to selection model.
CheckboxCell checkboxCell=new CheckboxCell(true, false);
Column<T, Boolean> boolColumn=new Column<T, Boolean>(
checkboxCell) {
#Override
public Boolean getValue(T object) {
return selectionModel.isSelected(object);
}
};
On delete button click,use selected object,It will provide you a object for delete.
selectionModel.getSelectedObject();

Related

How can I get the value of a EditTextCell in GWT when the user finish editting it

I am new at GWT, and I have been trying to get the value of a EditTextCell which is inside a cellTable, So I was wondering what is the best way to accomplish this?
Thanks
When you've got an EditTextCell in a CellTable, I assume you mean that you have an entire column like that:
Column<RowObject, String> editTextCellColumn = new Column<RowObject, String>( new EditTextCell() ){
#Override
public String getValue(RowObject object) {
// return the value that is supposed to be displayed in the EditTextCell
}
};
If that is the case, you can simply add a fieldupdater to the column. The FieldUpdater is triggered every time the user successfully edits the text cell.
editTextCellColumn.setFieldUpdater( new FieldUpdater<RowObject, String>(){
public void update(int index, final RowObject object, final String value) {
// 'value' is the new value the user entered in the EditTextCell.
// 'object' is the object that contains the old row information.
// 'index' is the current row index of 'object'
// Code for performing whatever you want to do with the new value goes here.
}
};

GWT CellTable keep focus on selected row

When I select a row in a CellTable which contains several columns, the whole row gets colored in yellow. It does not depend on which area of the row I click (which column of the row).
What I try to do is to keep the selected row colored in yellow as long as no other row of this very table is selected. At the moment, as soon as I click somewhere else in the browser, the row gets back its original color.
I tried to use a selection model, but this changed nothing. Do you have any advise or is this simply not possible, since the focus is managed by the browser? The behavior is the same in the Google showcase for the CellTable...
The selection model actually does what you want to do: it paints a row blue and the row does not change color if you click elsewhere in the page. (Only when another row is selected)
There are 2 selection models:
One that lets you select only one row, and another one that lets you select multiple rows.
MultiSelectionModel<Row> selectionModel = new MultiSelectionModel<Row>();
table.setSelectionModel(selectionModel);
SingleSelectionModel<Row> selectionModel = new SingleSelectionModel<Row>();
table.setSelectionModel(selectionModel);
The solution of user905374 did actually work. I mentioned in my first post that I already tried the solution with a selectionModel and that it did not work. This was partially true. It does work, but only if the table does NOT contain a CheckboxCell.
Following a working and the not working example. I think this might be a bug, but I am not sure if I miss something.
final CellTable<LicenceDto> licenseTable = new CellTable<LicenceDto>();
final SingleSelectionModel<LicenceDto> selectionModel = new SingleSelectionModel<LicenceDto>();
licenseTable.setSelectionModel(selectionModel);
//--- If I add this column, the selection does work.
Column<LicenceDto, String> workingColumn = new Column<LicenceDto, String>(new TextCell()) {
#Override
public String getValue(LicenceDto object) {
return "Works";
}
};
workingColumn.setFieldUpdater(new FieldUpdater<LicenceDto, String>() {
#Override
public void update(int index, LicenceDto object, String value) {
;
}
});
licenseTable.addColumn(workingColumn);
//--- If I add this column, the selection does NOT work anymore.
Column<LicenceDto, Boolean> notWorkingColumn = new Column<LicenceDto, Boolean>(new CheckboxCell(true, true)) {
#Override
public Boolean getValue(LicenceDto object) {
return object.getEnabled();
}
};
notWorkingColumn.setFieldUpdater(new FieldUpdater<LicenceDto, Boolean>() {
#Override
public void update(int index, LicenceDto object, Boolean value) {
presenter.enableLicense(object, value);
}
});
licenseTable.addColumn(notWorkingColumn);
You can even combine multiple cells and add them to the table (e.g. LinkActionCell etc). As long as there is no CheckboxCell, the blue selection with the SingleSelectionModel does work like a charm. Does anyone see what I do wrong with this CheckboxCell or is there a bug?
UPDATE
It was simply a usage error of me. The problem was that I set handlesSelection to true (second parameter of the CheckboxCell constructor) even thought I don't handle anything. Setting it to false solves the problem.
Bottomline: Use a selection model (e.g. SingleSelectionModel) and do not set the handlesSelection parameter to true of the CheckboxCell constructor to true, if you don't handle the selection by yourself.
You should observe the Showcase demo again. This time use the checkbox on the left most column i.e the first column. On selection the row turns blue indicating the row selection is made. This is when you have SelectionModel set up. Click on the page anywhere outside the CellTable/DataGrid the selection is not changed.
Now, instead of choosing the row via checkbox from first column, you click on a row in any other column. The row turns yellow. Click on the page anywhere outside the CellTable/DataGrid the focus/yellow is lost.
"colored in yellow" indicates row is under focus and being edited and not selected.
Note - you can force row selection by using click events per cell.
Try something like this:
CellTable table;
YourDataObject object = new YourDataObject(...);
SingleSelectionModel<YourDataObject> selectionModel =
new SingleSelectionModel<YourDataObject>();
table.setSelectionModel(selectionModel);
...
table.setSelected(object, true);
Use MultiSelectionModel if you wish more than one line to be highlighted.
Store the selected row's index. When user selects row, change row's style to some "selected-style" appropriate for your case (defined in your css file) and remove selected style from the previously selected row. Also don't forget to update selected row's index.
If you provide some code from the original version I help you out with some code with pleasure.

How to automatically check the checkboxes in the gridview when displaying?

I'm using GXT 3 to build a GridView that will display "incidents".
What I want to do is that when it renders it, I want some checkboxes to be checked, others to empty, according to the boolean in the database.
Below you have my code:
CheckBoxSelectionModel<IncidentDto> isIncidentCM = new CheckBoxSelectionModel<IncidentDto>(incidentProperties.incident());
allColumns.add(isIncidentCM.getColumn());
ColumnModel<IncidentDto> columnModel = new ColumnModel<IncidentDto>(allColumns);
final Grid<IncidentDto> grid = new Grid<IncidentDto>(store, columnModel);
grid.setSelectionModel(isIncidentCM);
add(grid);
And the IncidentProperties value provider:
IdentityValueProvider<IncidentDto> incident();
I'm not sure if you can bind the selection value to a boolean property, but you could add a listener to the Grid to update the checkboxes based on the boolean condition.
grid.addBeforeShowHandler(BeforeShowEvent event) {
#Override
public void onBeforeShow(BeforeShowEvent event) {
List<IncidentDto> itemsToSelect = new ArrayList<IncidentDto>();
for (IncidentDto incident : store.getAll()) {
if (incident.getBooleanProperty()) { //whatever your property is called
itemsToSelect.add(incident);
}
}
isIncidentCM.setSelection(itemsToSelect);
}
}
There may be other implications in using a BeforeShowEvent depending on how/when you populate your store, render the grid, etc. but assuming your store is fully loaded and the property available from your store objects I believe this should accomplish your goal.

GWT: CheckBoxCell and Selection change event

I am using the following constructor to create a checkboxcell in an editable data grid.
CheckboxCell(false, true)
When I use this and click at any place in the row, selection change event does not fire and I am using single selection model .
When I use,
CheckboxCell();
Selection change event fires on the row but,
1) We have click twice to check or uncheck the cell.
2) if we check or uncheck in the checkboxcell, the value will reverted as soon as I click anywhere.
I am trying to figure out the solution, but not successful yet. Any help would be appreciated.
Am using GWT 2.4.0
The problem is because of selection if possible do not use selection model. and add field updater for the column of check box. I have used this:
Column< GridReportFields, Boolean > cb = new Column< GridReportFields, Boolean >(new CheckboxCell() ) {
#Override
public Boolean getValue(GridReportFields object) {
// TODO Auto-generated method stub
return object.getCheckb();
}
};
cb.setFieldUpdater(new FieldUpdater<GridReportFields, Boolean>() {
#Override
public void update(int index, GridReportFields object, Boolean value) {
// TODO Auto-generated method stub
object.setCheckb(value);
dataGrid.redraw();
}
});
here gridReport Field is my model class. and setCheckb is is setter for the boolean variable that holds the value of checkbox.

Get values of GWT Selected checkbox in Celltable

In a GWT celltable I have many rows associated with CheckboxCell.I need to know only the values of the selected checkboxes on a particular event.
you need to use SingleSelectionModel or MultiSelectionModel with ProvidesKey implementation (to return unique keys)
SelectionModel selectionModel = new MuliSelectionModel<T>(new ProvidesKey><T>() {
#Override
public Object getKey(T item) {
// return unique key here
}
});
then you assign it to celltable
table.setSelectionModel(selectionModel);
then you can simply invoke selectionModel.getSelectecSet() and it will return Set of objects which are selected in table.
Set<T> selectedObjects = selectionModel.getSelectedSet();
HTH