-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] getCellElement
does not work with pinnedColumns
.
#7093
Comments
getCellElement
does not work with pinned column.getCellElement
does not work with pinnedColumns
.
Thanks @yaredtsy Given that the row is rendered by multiple DOM elements when using pinned columns, we should use a combined query selector rather than two separate selectors when querying the DOM node: diff --git a/packages/grid/x-data-grid/src/utils/domUtils.ts b/packages/grid/x-data-grid/src/utils/domUtils.ts
index d27463a..1357bff 100644
--- a/packages/grid/x-data-grid/src/utils/domUtils.ts
+++ b/packages/grid/x-data-grid/src/utils/domUtils.ts
@@ -35,18 +35,18 @@ export function getGridColumnHeaderElement(root: Element, field: string) {
);
}
+function getGridRowElementSelector(id: GridRowId): string {
+ return `.${gridClasses.row}[data-id="${escapeOperandAttributeSelector(String(id))}"]`;
+}
+
export function getGridRowElement(root: Element, id: GridRowId) {
- return root.querySelector<HTMLDivElement>(
- `.${gridClasses.row}[data-id="${escapeOperandAttributeSelector(String(id))}"]`,
- );
+ return root.querySelector<HTMLDivElement>(getGridRowElementSelector(id));
}
export function getGridCellElement(root: Element, { id, field }: { id: GridRowId; field: string }) {
- const row = getGridRowElement(root, id);
- if (!row) {
- return null;
- }
- return row.querySelector<HTMLDivElement>(
- `.${gridClasses.cell}[data-field="${escapeOperandAttributeSelector(field)}"]`,
- );
+ const rowSelector = getGridRowElementSelector(id);
+ const cellSelector = `.${gridClasses.cell}[data-field="${escapeOperandAttributeSelector(
+ field,
+ )}"]`;
+ return root.querySelector<HTMLDivElement>(`${rowSelector} ${cellSelector}`);
}
|
Playing the devil's advocate here. What should |
do you mean something like this? https://codesandbox.io/s/zen-benz-e7bky2?file=/demo.tsx. |
Check this CodeSandbox: https://codesandbox.io/s/wild-pine-49uown?file=/demo.tsx The main grid doesn't have an Age column but |
oh yeah, it's returning the first element when I change the order of age and email column it's returning the first element that comes first. could this be solved by generating a unique id every time a Datagrid is created. and store it in |
Right, we can exclude elements from nested grids with something like this: const selector = `${rowSelector} ${cellSelector}`;
return root.querySelector(`${selector}:not(.${gridClasses.root} .${gridClasses.root} ${selector})`); |
this means that's not in nested DataGrid right? what if I am working in the nested DataGrid |
I have tested giving Datagrid a unique id and it seems to be working I have exposed |
Yeah, might be. I thought it would work because of |
using the unique id for Datagrid also solves the datagrid in datagrid problem #4243 After: https://codesandbox.io/s/festive-hypatia-y96003?file=/demo.tsx should I create a PR for it? |
We can use |
It seems to me that passing export function getGridCellElement(root: Element, { id, field }: { id: GridRowId; field: string }) {
const isChildOfRoot = (element: HTMLElement) => {
let parent = element.parentElement;
let found = false;
while (!found && parent !== null) {
if (parent?.classList.contains('MuiDataGrid-root')) {
found = true;
} else {
parent = parent?.parentElement;
}
}
return parent === root;
};
const candidates = root.querySelectorAll<HTMLDivElement>(
`.${gridClasses.row}[data-id="${escapeOperandAttributeSelector(String(id))}"] > .${
gridClasses.cell
}[data-field="${escapeOperandAttributeSelector(field)}"]`,
);
const filteredCandidates = Array.from(candidates).filter(isChildOfRoot);
return filteredCandidates.length ? filteredCandidates[0] : null;
} Something similar could be done in |
Also, |
@cherniavskii Cool, I didn't know about this API. The only problem is that it doesn't seem to have a easy way to know the distance from a node to the root, so we'll need something similar to |
what about passing the const cell = (event.target as HTMLDivElement).closest(
`[data-parentid="${apiRef.current.instanceId}"] > .${gridClasses.cell}`,
); or add the id to
|
I prefer to not add new attributes to DOM elements unless there's no other solution. |
I found another way. if we calculate the exact depth of the Datagrid we can query for cells in that depth only. bt excluding the datagrids that are above and below let depth = 0;
let current: Element = root;
while (current?.parentElement != null) {
if (current?.parentElement?.classList.contains(gridClasses.root)) {
depth += 1;
}
current = current?.parentElement;
} we can query let parent = `.${gridClasses.root}`;
for (let i = 0; i < depth; i += 1) {
parent += ` .${gridClasses.root}`;
}
return root.querySelector<HTMLDivElement>(
`${parent} ${selector}:not(${parent} .${gridClasses.root} ${selector})`,
);
} and for let parent = `.${gridClasses.root} .${gridClasses.root}`;
for (let i = 0; i < depth; i += 1) {
parent += ` .${gridClasses.root}`;
}
return elem.closest(`.${className}:not( ${parent} .${className})`); I have tested it out everything works. |
Here are the Demos Nested DataGrid : https://codesandbox.io/s/festive-hypatia-y96003?file=/demo.tsx |
should I create a PR as a draft so you can guys can review the implementation? |
@yaredtsy are there some specific use cases where you could reproduce this issue? |
hey @cherniavskii , no I do not have any use cases for it. it can wait |
We just got the first report for this issue in #7819 |
Co-authored-by: Andrew Cherniavskyi <[email protected]> Fixes #7093 Fixes #7819
Co-authored-by: Andrew Cherniavskyi <[email protected]> Fixes mui#7093 Fixes mui#7819
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example: https://codesandbox.io/s/zen-benz-e7bky2?file=/demo.tsx
Steps:
print active element
button.Current behavior 😯
getCellElement
is returning null when I use it withpinnedColumns
.Expected behavior 🤔
it should return an HTML element.
Context 🔦
No response
Your environment 🌎
npx @mui/envinfo
Order ID 💳 (optional)
No response
The text was updated successfully, but these errors were encountered: