How to force the redisplay of a Table after (programatically) reordering?

I’ve got a detail table that has sequenced items, and I’ve got a pair of buttons for moving items up/down in the table. The button works (I’m just working on the Up button first). The sequences swap correctly (2 becomes 1, 1 becomes 2 for instance)… but the table doesn’t fully redisplay.

What I mean is if I have 2 items: sequence 1, TEST 1, sequence 2, TEST2, I hit the Up button on item #2, 1 and 2 swap correctly (we now have sequence 1 TEST2, sequence 2 TEST 1)… but then the table is displayed in the order sequence 2 TEST 1, sequence 1 TEST 2. If I confirm all the changes, going all the way out to the browse screen, and go back in, it displays in the correct order, but it will not while “live.”

I’ve tried table.repaint() and also table.sort(“sequence”, Table.SortDirection.ASCENDING) followed by table.repaint() - but it just doesn’t redisplay with the new, correct order.

Hi,

You can swap items in the same way as it is done in OrderEdit.java : determine the index of the currently selected item and then swap the items using collectionContainer.getMutableItems().set(idx, item). In this case, a table should update its view. As far as I understand, you’re implementing Up/Down buttons in a browser screen, so in this case, you need to commit changes manually either after each swap or by a custom button.

Regards,
Gleb

No; I’m sorry my original message was unclear.

The table in question is a detail table on an edit screen for its master. It represents diagnoses on a patient’s registration. The order is important as to primary DX, secondary DX, etc - so the user needs to be able to easily order/reorder them. The entity has 3 attributes - a ref to the parent/master (the registration), the sequence, and a ref to the DX code table (lookup). Of course, only the sequence and DX code are displayed in the table.

The up/down buttons manipulate the sequence - that all works fine and the sequences themselves update visually correctly but the table does not redisplay in the new order unless I do the following:

                diagnosesTable.sort("sequence", Table.SortDirection.ASCENDING);
                diagnosesTable.repaint();

… at the end of each button listener. Just the .sort doesn’t work, and just the .repaint doesn’t work; it didn’t do what I wanted until I used both methods.

Nevertheless, it seems that the code from this sample cuba-sample-sort-composition might fit your needs. If it’s not, could you please attach a demo project or at least share a code snippet on how you manipulate the sequence.

Gleb

A sample project might take quite some time to whittle down, but here’s the code for the up button. Yeah it’s crude; it’s just what I came up with while working through the problem.

    @Subscribe("dxUpButton")
    public void onDxUpButtonClick(Button.ClickEvent event) {
        if (diagnosesTable.getSingleSelected() != null) {
            RegistrationDiagnoses rdWereOn = diagnosesTable.getSingleSelected();
            List<RegistrationDiagnoses> theDXs = diagnosesDc.getMutableItems();

            int position = theDXs.indexOf(rdWereOn);
            if (position != 0) {
                int prevRDSeq = theDXs.get(position-1).getSequence();
                theDXs.get(position-1).setSequence(rdWereOn.getSequence());
                rdWereOn.setSequence(prevRDSeq);
                diagnosesTable.sort("sequence", Table.SortDirection.ASCENDING);
                diagnosesTable.repaint();
            }
        }
    }

Hi,

as this (or similar statements come from time to time from various community members) I wanted to point to an older answer that I gave: Entity change does not get reflected in visual Component - #8 от пользователя mario - CUBA.Platform


A sample / demo project is crucial to have when talking about a problem in order to provide a solution. It is not intended to take your existing production project. Create a new example project, take a similar domain (or just exchange domain names), put as little entities in it as possible, reproduce the bug.

If you can reproduce it, great - put it in here and we will try to figure out.
If you can’t reproduce - great. Try to identify what is different in your example. If you find something: repeat the process.

This has some major upsides:

  1. you will learn a lot. Isolating a problem is a very important skill to have. You will learn about the framework, about the patterns of your domain model, some internals of the framework and so on.
  2. You will increase the likelihood that you will get an answer. Think about it: this is a free service. A lot of people don’t get paid to answer your question. Those who get, they can still pick the question that will give them a quick win. Since you probably don’t pay for the answer - if you do your very best to help the people to help you, that increases your chances to get a proper answer fast…
  3. You created an abstract example that fulfills a community purpose. If the example is not polluted with the domain you are in but instead with a domain everyone understands, you do the community a favor, because of the likelihood that other people have a similar question.
  4. You are a good role model for other community members. When other people see you creating great questions, doing good examples, etc. They will start mirroring your behavior. This leads to overall better examples, faster answers, etc.

See also Ответы на вопросы - CUBA.Platform.

Looking forward to your great example :wink:

Cheers
Mario

I agree with all of this; you’re right. I’ll see what I can come up with for a bare-metal example.

According to your code sample, it seems that you don’t inform data container that the sequence has been changed. As I suggested above, you need to do the following:

The full Up/Down buttons code can be found in the demo project I referred above that is fully fits your needs.

Regards,
Gleb

@gorelov that code (of course!) works perfectly. Is there any practical advantage to using that approach vs. the approach I’d hacked out before (that being .sort() followed by .repaint())?

@mario thanks again for the reminder about community. I’ll come up with an example project the next time something like this arises. Luckily the framework is so very straightforward and logical that it doesn’t happen too often.

A table column has three possible states: sorted acs, sorted des, unsorted. You force a column to be sorted acs, but if user interact with a column and make it unsorted, then your sequence will be broken. If you change the items order in the data container, then the unsorted state will have the required order.

Aha - that makes sense. I have user-controlled sorting disabled here because there is just sequence, code - and there is a max of 12 so the user resorting doesn’t make sense in this case.