简体   繁体   English

GWT单元格表单击新行可得到先前选择的行的值

[英]GWT Cell Table Clicking on a new row gives value of Previously selected row

In GWT 2.6 CellTable, I'm writing a single click event to perform some operation. 在GWT 2.6 CellTable中,我正在编写一个单击事件以执行某些操作。 I can not get the correct Row Index while clicking on the CellTable Row; 单击“ CellTable行”时,我无法获得正确的行索引; only a double click event returns the row correctly. 只有双击事件才能正确返回该行。

final SingleSelectionModel<PatientDTO> selectionModel = 
    new SingleSelectionModel<PatientDTO>();

patientsTable.setSelectionModel(selectionModel);  

patientsTable.addDomHandler(new ClickHandler()  
{  
    @Override  
    public void onClick(ClickEvent event)  
    {
        PatientDTO selected = selectionModel.getSelectedObject();
        if (selected != null) 
        {
            RootLayoutPanel.get().clear();
            RootLayoutPanel.get().add(new PatientPanel(selected));
        }
    }
}, ClickEvent.getType());

Either use SingleSelectionModel or MultiSelectionModel and add SelectionChangeHandler on it that will be fired when selection is changed in the CellTable 使用SingleSelectionModelMultiSelectionModel并在上面添加SelectionChangeHandler ,当在CellTable更改选择时将触发该CellTable

Sample code: 样例代码:

final SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>();
//final MultiSelectionModel<Contact> selectionModel = new MultiSelectionModel<Contact>();
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        Set<Contact> selected = selectionModel.getSelectedSet();
        if (selected != null) {
            for (Contact contact : selected) {
                System.out.println("You selected: " + contact.name);
            }
        }
    }
});

OR alternatively try with CellPreviewHandler 或者,也可以尝试使用CellPreviewHandler

table.addCellPreviewHandler(new Handler<Contact>() {

    @Override
    public void onCellPreview(CellPreviewEvent<Contact> event) {
        int row = event.getIndex();
        int column = event.getColumn();

        if ("focus".equals(event.getNativeEvent().getType())) {
           //..
        }
        if ("blur".equals(event.getNativeEvent().getType())) {
            //...
        }
        if ("mousedown".equals(event.getNativeEvent().getType())) {
            //..
        }
        if ("mouseover".equals(event.getNativeEvent().getType())) {
            //..
        }
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM