Column Ordering (Drag & Drop)

This example features drag and drop column reordering using the @angular/cdk drag and drop directives.


Username Account Status Role Avg Session Duration Company Name Last Visited At Visit Count
Aaliyah_Bosco21 suspended admin 116 Thiel Group 08 Nov 2024 06:15:58 PDT 18
Brielle.Brakus suspended user 34 Welch LLC 02 Jul 2025 09:14:22 PDT 2
Theresia_Roob15 pending moderator 68 Franecki - Lueilwitz 17 Oct 2025 08:15:56 PDT 348
Lavina.McClure-Hoeger pending moderator 99 Ebert - Gibson 14 Aug 2025 14:36:43 PDT 62
Sigurd.Koelpin42 active moderator 115 Kerluke and Sons 29 Aug 2025 23:15:01 PDT 2
Michale_Steuber suspended admin 119 Konopelski Inc 28 Aug 2025 14:01:38 PDT 2
Scarlett_Streich45 pending user 58 Schmidt Group 24 Oct 2025 04:18:28 PDT 16
Elsa_Sporer91 suspended moderator 116 Quitzon, Ernser and Keebler 18 Jul 2025 13:42:23 PDT 213
Stella.Reynolds60 active user 82 Gleason - Wunsch 23 Sep 2025 16:44:37 PDT 2
Debra_Bednar80 suspended user 116 Schumm-Mertz - Mertz 09 May 2025 23:04:42 PDT 3
import {
  CdkDrag,
  type CdkDragDrop,
  CdkDragHandle,
  CdkDropList,
} from "@angular/cdk/drag-drop"
import {Component, signal} from "@angular/core"

import {
  type AngularTable,
  createAngularTable,
  TableModule,
} from "@qualcomm-ui/angular/table"
import {type ColumnOrderState, getCoreRowModel} from "@qualcomm-ui/core/table"

import {createUserQuery, type User, userColumns} from "./data"

@Component({
  imports: [TableModule, CdkDropList, CdkDrag, CdkDragHandle],
  selector: "column-dnd-demo",
  template: `
    <div q-table-root>
      <div q-table-scroll-container>
        <table q-table-table>
          <thead q-table-header>
            @for (
              headerGroup of table.getHeaderGroups();
              track headerGroup.id
            ) {
              <tr
                cdkDropList
                cdkDropListOrientation="horizontal"
                q-table-row
                (cdkDropListDropped)="onColumnDropped($event)"
              >
                @for (header of headerGroup.headers; track header.id) {
                  @if (!header.isPlaceholder) {
                    <th cdkDrag q-table-header-cell>
                      <ng-container *renderHeader="header; let value">
                        {{ value }}
                      </ng-container>
                      <button cdkDragHandle q-table-column-drag-handle></button>
                    </th>
                  }
                }
              </tr>
            }
          </thead>
          <tbody q-table-body>
            @for (row of table.getRowModel().rows; track row.id) {
              <tr q-table-row>
                @for (cell of row.getVisibleCells(); track cell.id) {
                  <td q-table-cell>
                    <ng-container *renderCell="cell; let value">
                      {{ value }}
                    </ng-container>
                  </td>
                }
              </tr>
            }
          </tbody>
        </table>
      </div>
    </div>
  `,
})
export class ColumnDndDemo {
  readonly query = createUserQuery(10)

  onColumnDropped(col: CdkDragDrop<any>) {
    this.columnOrder.update((prevOrder) => {
      const columnOrder = [...prevOrder]
      return this.reorderColumn(
        columnOrder[col.previousIndex],
        columnOrder[col.currentIndex],
        columnOrder,
      )
    })
  }

  private reorderColumn(
    draggedColumnId: string,
    targetColumnId: string,
    columnOrder: string[],
  ): ColumnOrderState {
    columnOrder.splice(
      columnOrder.indexOf(targetColumnId),
      0,
      columnOrder.splice(columnOrder.indexOf(draggedColumnId), 1)[0],
    )
    console.debug("new order", columnOrder)
    return columnOrder
  }

  protected readonly columnOrder = signal(
    userColumns.map((column) => column.id!),
  )

  protected table: AngularTable<User> = createAngularTable(() => ({
    columns: userColumns,
    data: this.query.data() || [],
    getCoreRowModel: getCoreRowModel(),
    state: {
      columnOrder: this.columnOrder(),
    },
  }))
}
Updated by Ryan Bower