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

Make sure unhidden columns are put at the correct place #21262

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/components/data-table/dialog-data-table-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,53 @@ export class DialogDataTableSettings extends LitElement {
const columns = this._sortedColumns(
this._params.columns,
this._columnOrder,
this._hiddenColumns
hidden
piitaya marked this conversation as resolved.
Show resolved Hide resolved
);

if (!this._columnOrder) {
this._columnOrder = columns.map((col) => col.key);
} else {
const newOrder = this._columnOrder.filter((col) => col !== column);

// Array.findLastIndex when supported or core-js polyfill
const findLastIndex = (
arr: Array<any>,
fn: (item: any, index: number, arr: Array<any>) => boolean
piitaya marked this conversation as resolved.
Show resolved Hide resolved
) => {
for (let i = arr.length - 1; i >= 0; i--) {
if (fn(arr[i], i, arr)) return i;
}
return -1;
};

let lastMoveable = findLastIndex(
newOrder,
(col) =>
col !== column &&
!hidden.includes(col) &&
!this._params!.columns[col].main &&
piitaya marked this conversation as resolved.
Show resolved Hide resolved
this._params!.columns[col].moveable !== false
piitaya marked this conversation as resolved.
Show resolved Hide resolved
);

if (lastMoveable === -1) {
lastMoveable = newOrder.length - 1;
}

columns.forEach((col) => {
if (!this._columnOrder!.includes(col.key)) {
this._columnOrder!.push(col.key);
if (!newOrder.includes(col.key)) {
if (col.moveable === false) {
newOrder.unshift(col.key);
} else {
newOrder.splice(lastMoveable + 1, 0, col.key);
}

if (col.defaultHidden) {
hidden.push(col.key);
}
}
});

this._columnOrder = newOrder;
}

this._hiddenColumns = hidden;
Expand Down
Loading