Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScrollToItem corrupts tree data #53

Open
andersforsell opened this issue Feb 9, 2024 · 10 comments
Open

ScrollToItem corrupts tree data #53

andersforsell opened this issue Feb 9, 2024 · 10 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@andersforsell
Copy link

andersforsell commented Feb 9, 2024

With Vaadin 24.3.3 and selection-grid-flow version 3.0.3 I sometimes see problems that the tree data is corrupted, for example child nodes are empty.

The below code reproduces the problem if you click the button and then expand the tree data, see also snapshot.

public class MainView extends VerticalLayout {
	public MainView() {
	  SelectionTreeGrid<String> treeGrid = new SelectionTreeGrid<>();
    treeGrid.addHierarchyColumn(String::toString);
    TreeData<String> treeData = new TreeData<>();
    for (int i = 1; i <= 10; i++) {
        String topLevelItem = "Item " + i;
        treeData.addItem(null, topLevelItem);
        for (int j = 1; j <= 3; j++) {
            String subItem = "Subitem " + (i * 10 + j);
            treeData.addItem(topLevelItem, subItem);
            for (int k = 1; k <= 3; k++) {
                String subSubItem = "Subsubitem " + (i * 100 + j * 10 + k);
                treeData.addItem(subItem, subSubItem);
            }
        }
    }
    treeGrid.setDataProvider(new TreeDataProvider<>(treeData));
    Button button = new Button("Select Item", e -> {
      treeGrid.scrollToItem("Subsubitem 512");
      treeGrid.select("Subsubitem 512");
    });
    add(treeGrid, button);
	}
}

Screenshot 2024-02-09 133039

@TatuLund
Copy link

@andersforsell The scrollToItem implementation in SelectionTreeGrid is based on the workaround of missing feature in TreeGrid. This missing feature actually has now been implemented in Vaadin 24. There is now scrollToIndex(int... indeces) method. Most likely fix to your observed problem is to refactor SelcetionTreeGrid implementation to use that as scrollToIndex(int... indeces) will also activate loading of the rows data as per needed.

@TatuLund TatuLund added the bug Something isn't working label Feb 10, 2024
@andersforsell
Copy link
Author

@TatuLund I'm not sure it is due to the scrollToItem method because I see problems with this code as well:

@Route
public class MainView extends VerticalLayout {

	public MainView() {
	  TreeGrid<String> treeGrid = new SelectionTreeGrid<>();
    treeGrid.addHierarchyColumn(String::toString);
    TreeData<String> treeData = new TreeData<>();
    for (int i = 1; i <= 10; i++) {
        String topLevelItem = "Item " + i;
        treeData.addItem(null, topLevelItem);
        for (int j = 1; j <= 3; j++) {
            String subItem = "Subitem " + (i * 10 + j);
            treeData.addItem(topLevelItem, subItem);
            for (int k = 1; k <= 3; k++) {
                String subSubItem = "Subsubitem " + (i * 100 + j * 10 + k);
                treeData.addItem(subItem, subSubItem);
            }
        }
    }
    treeGrid.setDataProvider(new TreeDataProvider<>(treeData));
    Button button = new Button("Select Item", e -> {
      treeGrid.expand("Item 5");
      treeGrid.expand("Subitem 51");
      treeGrid.select("Subsubitem 512");
    });
    
    add(treeGrid, button);
	}
}

Note that I'm only using the TreeGrid interface but somehow the SelectionTreeGrid implementation affects how items are fetched and displayed. Switching to the TreeGrid implementation does not show the problem.

@TatuLund
Copy link

Note, 3.0.3 is not the latest, 3.0.4 is, and there I fixed something like you described. Can you update and check if the problem disappears.

6f6521f

@andersforsell
Copy link
Author

I tested with 3.0.4 but its unfortunately the same problem.

@andersforsell
Copy link
Author

andersforsell commented Feb 11, 2024

@TatuLund the problem is fixed when I changed the _getItemOverriden function in helpers.js to better correspond to the overridden _getItem :

export function _getItemOverriden(idx, el) {
      if (idx >= this._flatSize) {
        return;
      }

      el.index = idx;

      const { item } = this._dataProviderController.getFlatIndexContext(idx);
      if (item) {
        this.__updateLoading(el, false);
        this._updateItem(el, item);
        if (this._isExpanded(item)) {
          this._dataProviderController.ensureFlatIndexHierarchy(idx);
        }
      } else {
        this.__updateLoading(el, true);
        this._dataProviderController.ensureFlatIndexLoaded(idx);
      }    
    
    /** focus when get item if there is an item to focus **/
    if (this._rowNumberToFocus > -1) {
        if (idx === this._rowNumberToFocus) {
            const row = Array.from(this.$.items.children).filter(
                (child) => child.index === this._rowNumberToFocus
            )[0];
            if (row) {
                this._focus();
            }
        }
    }
}

@TatuLund
Copy link

TatuLund commented Feb 12, 2024

I can't reproduce the issues demonstrated by your code samples with version 3.0.4. Can you check browser console log if you see the warnings reported by this ticket #52

If yes, you are still having the old JavaScript, then you need to do full frontend clean by running mvn clean-frontend first and delete fronted/generated folder.

@TatuLund
Copy link

Also your proposed fix looks to be essentially the same as already in version 3.0.4.

@andersforsell
Copy link
Author

Yes, you are right, it must have been the old JavaScript when I was testing.

Can you please release and publish the 3.0.4 version on Maven Central?

@TatuLund
Copy link

Can you please release and publish the 3.0.4 version on Maven Central?

Version 3.0.4 has been published in Vaadin's add-on repository already.

@TatuLund TatuLund added the duplicate This issue or pull request already exists label Feb 12, 2024
@TatuLund
Copy link

Duplicate of #52

@TatuLund TatuLund marked this as a duplicate of #52 Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants