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

Tree View Fails to load when complex _id data types are present #2515

Open
tnaum-ms opened this issue Dec 5, 2024 · 1 comment
Open

Tree View Fails to load when complex _id data types are present #2515

tnaum-ms opened this issue Dec 5, 2024 · 1 comment
Assignees
Milestone

Comments

@tnaum-ms
Copy link
Collaborator

tnaum-ms commented Dec 5, 2024

When a dataset contains _id fields with more complex data types, the Tree View fails to render. This could be caused by:

  • Erroneous _id extraction logic.
  • Incorrect assumptions that _id is always of a specific type.

Task:

  • Investigate the cause of the issue.
  • Implement a fix to handle complex _id data types correctly.

Data in the Table View:

Image

after switching to the Tree View:

Image

@tnaum-ms tnaum-ms added this to the 0.24.2 milestone Dec 5, 2024
@tnaum-ms tnaum-ms self-assigned this Dec 5, 2024
@ghiscoding
Copy link
Contributor

ghiscoding commented Dec 9, 2024

I was curious about this one, so I tried replicating this demo in Slickgrid-React with just what I see in the print screen above. The issue might be caused by the fact that the SlickGrid DataView requires unique "id" and I'm not sure if your Id in this case is _id? If it is then multiple entries as objects instead of strings might, most probably, cause the issue. In order to replicate a working demo, I added a formatter in the _id column to deal with the string or object data and the biggest change to get a working demo was to modify the data provided to the grid by using a map to provide truly unique Ids as string.

Note that my formatter below just display a stringified version when the _id is a complex object, but depending on your need, you could instead choose to return just the final Id string instead.

So here's the 2 modifications I've done

columnDefinitions = [
  {
        id: '_id', name: '_id', field: '_id', width: 220,
        filterable: true, sortable: true, exportWithFormatter: false,
        formatter: Formatters.multiple,
        params: {
          formatters: [
            // 1st formatter for ID
            ((_row, _cell, val) => {
              if (typeof val === 'object') {
                return JSON.stringify(val); // display stringified object
              }
              return val;
            }) as Formatter,
            // 2nd formatter for Tree Data icons
            Formatters.tree
          ]
        }
      },
      {
        id: 'type', name: 'type', field: '_id', minWidth: 90, filterable: true,
        formatter: (_row, _cell, val) => typeof val === 'string' ? 'ID' : '{}'
      },
];

and the use of map to add a unique Id as string

loadData() {
    const data = [
      { _id: '667591e', parentId: null },
      { _id: '67224b2', parentId: null },
      { _id: { mySpecialID: '223344' }, parentId: null },
      { _id: { mySpecialID: '424242' }, parentId: '667591e' },
    ];

    // make sure that the "id" is unique by using map
    const output = data.map((entry) => ({
      _id: entry._id,
      id: typeof entry._id === 'string' ? entry._id : entry._id['mySpecialID'],
      parentId: entry.parentId,
    }));

    console.log('data', output);
    return output;
  }

You can see a working Stackblitz of this change at this link (I modified the Slickgrid-React Example 27, so you'll need to route to that example to see it live)
https://stackblitz.com/edit/github-bnyowcpe?file=src%2Fexamples%2Fslickgrid%2FExample27.tsx

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants