GWT CellTable keep focus on selected row

↘锁芯ラ 提交于 2019-12-05 20:56:21

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!