Make some rows of a Grid non editable

Hi,

I am trying to make a grid, from which some rows (based on a property of the row item) are not editable. Is there a straightforward way to achieve this?

I have thought of adding an item click listener that “cancels” the item click event. Is it possible to stop the event from propagating? How?

Thanks in advance.
Best regards,
Pau.

I’m interested in this too. What’s the proper way of achieving this?

Howdy. It seems there is no specific API to fit this purpose, but I could achieve the same effect with the following code:

grid.addItemClickListener(new ItemClickListener() {
            @Override
            public void itemClick(ItemClickEvent event) {
                if (isEditable(event.getItem()) {
                    grid.setEditorEnabled(true);
                    grid.editItem(event.getItemId());
                } else {
                    grid.setEditorEnabled(false);
                }
            }
        });

Hopefully this helps.

Hi Pekka,

Thank you very much for your solution, I have two issues though.

The first one is that sometimes the itemClick is triggered twice, in that cases I am getting an error because we are trying to setEditorEnabled (true) something that is already editable. I solved this by adding this check:

} else {
    if (!tasksGrid.isEditorEnabled()) {
        tasksGrid.setEditorEnabled(true);
    }
    if (!tasksGrid.isEditorActive()) {
        tasksGrid.editItem(event.getItemId());
    }
}

The second issue I don’t know why it happens and I haven’t been able to solve it. When I click on an editable row the first time, it works fine. The non-editable rows also work fine. But if I click on a non-editable row, and afterwards on an editable row, the editable expansion appears, but all the fields to edit are missing, see images attached.

If I don’t add that listener, the editor always works fine. If I add the listener but I never click on a non-editable row, the editor also works always fine. Until I press on a non-editable row, then the editor never comes back right.

Do you have a clue of what could be the issue and how to solve it?

Thanks!
21032.jpg
21033.jpg

I don’t have this issue, are you sure you’re not adding the ItemClickListener twice? How could the editor be triggered twice, since ItemClickEvents are not fired until the editor is closed. Also what Vaadin version you are using ? I can’t reproduce it with 7.5.2.

Also not able to reproduce this one. Either I’ve misunderstood something, or there is something else in your code causing this.

Yes, I am adding it only once. And you are right that normally it’s not possible. I realized it’s only happening when I am debugging with a breakpoint there, I am able to click on the row more than once, and that is why is triggered twice. But without the breakpoint, it only gets triggered once. So, yeah, not really an issue.

Regarding the second one, I am using 7.4.3. I realized that there is an error in the js console when I click the editable-row after clicking the non-editable row.

SEVERE: Hierarchy claims that 670 is a child for 589 (com.vaadin.client.connectors.GridConnector) but no connector with id 670 has been registered. More information might be available in the server-side log if assertions are enabled However, I have started the server with -ea but still I see nothing in the server-side. Any idea what that could mean? I have googled this error without much luck.

Thanks again!

Seems like there is some issue with attaching/detaching the editor fields.

Because you’re running an old version and there has been about a ton of bugfixes for the Grid after that, I’d recommend you to update your Vaadin version at least to the latest 7.4 which is 7.4.8. as it is compatible with 7.4.3.
Or you might even consider updating to the the latest Vaadin which is 7.5.4.

If you still want continue debugging the issue, you could also see from the browser’s console if any client side errors are logged there, maybe compile the widgetset with pretty style and enable break on exceptions…

Thanks for your help Pekka, I’ll continue investigating following that directions.

I updated to 7.5.4 and it works!

Thanks! :smiley:

Awesome to hear that!

thanks for the code Pekka :stuck_out_tongue:

grid.addItemClickListener(new ItemClickListener() { @Override public void itemClick(ItemClickEvent event) { if (isEditable(event.getItem()) { grid.setEditorEnabled(true); grid.editItem(event.getItemId()); } else { grid.setEditorEnabled(false); } } }); This code kind of works. Once the editor is enabled, it stays enabled. Does anyone have an updated solution for version 7.6.4? Thanks.

Solution from Vaadin support:

grid.addItemClickListener(new ItemClickListener() {
    @Override
    public void itemClick(ItemClickEvent event) {
        if (event.isDoubleClick()) {
            if (isEditable(event.getItem()) && !grid.isEditorActive()) {
                grid.setEditorEnabled(true);
                grid.editItem(event.getItem());
            }
        }
    }
});

grid.addEditorCloseListener(new EditorCloseListener() {
    @Override
    public void editorClosed() {
        if (grid.isEditorEnabled()) {
            grid.setEditorEnabled(false);
        }
    }
});