Skip to content

Commit

Permalink
feat(abc:st): add drag property
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Jan 19, 2025
1 parent e28a21a commit f32ee3d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 5 deletions.
48 changes: 48 additions & 0 deletions packages/abc/st/demo/drag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
order: 4
title:
en-US: Drag sorting
zh-CN: 拖拽排序
---

## zh-CN

使用 `drag` 实现行拖拽排序。

## en-US

Use `drag` to implement row drag sorting.

```ts
import { Component } from '@angular/core';

import { STColumn, STData, STModule } from '@delon/abc/st';

@Component({
selector: 'app-demo',
template: `<st [data]="users" [columns]="columns" drag />`,
imports: [STModule]
})
export class DemoComponent {
users: STData[] = Array(10)
.fill({})
.map((_, idx) => {
return {
id: idx + 1,
name: `name ${idx + 1}`,
age: Math.ceil(Math.random() * 10) + 20
};
});
columns: STColumn[] = [
{ title: '编号4', index: 'id' },
{ title: '编号5', index: 'id' },
{ title: '编号6', index: 'id' },
{ title: '编号7', index: 'id' },
{ title: '编号8', index: 'id' },
{ title: '编号8', index: 'id' },
{ title: '编号8', index: 'id' },
{ title: '编号8', index: 'id' },
{ title: '编号8', index: 'id' }
];
}
```
1 change: 1 addition & 0 deletions packages/abc/st/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ When an exception is thrown when parsing column data, *INVALID DATA* will be for
| `[responsiveHideHeaderFooter]` | Whether to display the header and footer under the small screen | `boolean` | `false` ||
| `[resizable]` | Resize header of the current table, **Multiple headers not supported** | `STResizable, boolean` | - | - |
| `[trackBy]` | `TrackByFunction` function of list loop `@for` | `TrackByFunction<T>` | - | - |
| `[drag]` | Drag soring | `STDragOptions, boolean` | - | - |
| `(change)` | Events | `EventEmitter<STChange>` | - | - |
| `(error)` | Error event | `EventEmitter<STError>` | - | - |

Expand Down
1 change: 1 addition & 0 deletions packages/abc/st/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module: import { STModule } from '@delon/abc/st';
| `[responsiveHideHeaderFooter]` | 是否在小屏幕下才显示顶部与底部 | `boolean` | `false` ||
| `[resizable]` | 当前表格所有列的调整表头配置项,**不支持多表头** | `STResizable, boolean` | - | - |
| `[trackBy]` | `@for` 列表循环的 `TrackByFunction` 函数 | `TrackByFunction<T>` | - | - |
| `[drag]` | 拖拽排序 | `STDragOptions, boolean` | - | - |
| `(change)` | 变化时回调,包括:`pi``ps``checkbox``radio``sort``filter``click``dblClick``expand` 变动 | `EventEmitter<STChange>` | - | - |
| `(error)` | 异常时回调 | `EventEmitter<STError>` | - | - |

Expand Down
11 changes: 10 additions & 1 deletion packages/abc/st/st.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@
}
</thead>
}
<tbody class="st__body">
<tbody
class="st__body"
cdkDropList
[cdkDropListDisabled]="drag() === null"
(cdkDropListDropped)="drag()?.dropped?.($event)"
(cdkDropListEntered)="drag()?.entered?.($event)"
(cdkDropListExited)="drag()?.exited?.($event)"
(cdkDropListSorted)="drag()?.sorted?.($event)"
>
@if (!_loading) {
<ng-template [ngTemplateOutlet]="bodyHeader!" [ngTemplateOutletContext]="{ $implicit: _statistical }" />
}
Expand All @@ -155,6 +163,7 @@
(click)="_rowClick($event, i, index, false)"
(dblclick)="_rowClick($event, i, index, true)"
[class]="i._rowClassName"
cdkDrag
>
@if (expand) {
<td
Expand Down
20 changes: 19 additions & 1 deletion packages/abc/st/st.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { moveItemInArray, DragDropModule } from '@angular/cdk/drag-drop';
import type { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
import { DecimalPipe, DOCUMENT, NgTemplateOutlet } from '@angular/common';
import {
Expand All @@ -10,6 +11,7 @@ import {
ElementRef,
EventEmitter,
inject,
input,
Input,
numberAttribute,
OnChanges,
Expand Down Expand Up @@ -72,6 +74,7 @@ import type {
STContextmenuItem,
STCustomRequestOptions,
STData,
STDragOptions,
STError,
STExportOptions,
STLoadOptions,
Expand Down Expand Up @@ -251,7 +254,8 @@ export class STTdComponent {
NzIconDirective,
NzMenuModule,
STFilterComponent,
STTdComponent
STTdComponent,
DragDropModule
]
})
export class STComponent implements AfterViewInit, OnChanges {
Expand Down Expand Up @@ -330,6 +334,20 @@ export class STComponent implements AfterViewInit, OnChanges {
@Input({ transform: booleanAttribute }) bordered = false;
@Input() size!: 'small' | 'middle' | 'default';
@Input() scroll: { x?: string | null; y?: string | null } = { x: null, y: null };
drag = input<STDragOptions | null, STDragOptions | boolean | string | null | undefined>(null, {
transform: v => {

Check warning on line 338 in packages/abc/st/st.component.ts

View check run for this annotation

Codecov / codecov/patch

packages/abc/st/st.component.ts#L338

Added line #L338 was not covered by tests
const obj: STDragOptions | null = typeof v === 'object' ? v : booleanAttribute(v) ? {} : null;
if (obj == null) return null;

if (typeof obj.dropped !== 'function') {
obj.dropped = e => {
moveItemInArray(this._data, e.previousIndex, e.currentIndex);
this.cd();

Check warning on line 345 in packages/abc/st/st.component.ts

View check run for this annotation

Codecov / codecov/patch

packages/abc/st/st.component.ts#L343-L345

Added lines #L343 - L345 were not covered by tests
};
}
return obj;

Check warning on line 348 in packages/abc/st/st.component.ts

View check run for this annotation

Codecov / codecov/patch

packages/abc/st/st.component.ts#L348

Added line #L348 was not covered by tests
}
});
@Input() singleSort?: STSingleSort | null;
private _multiSort?: STMultiSort;
@Input()
Expand Down
29 changes: 26 additions & 3 deletions packages/abc/st/st.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { ElementRef, TemplateRef } from '@angular/core';
import { Observable } from 'rxjs';
import type { CdkDragDrop, CdkDragEnter, CdkDragExit, CdkDragSortEvent } from '@angular/cdk/drag-drop';
import type { HttpHeaders, HttpParams } from '@angular/common/http';
import type { ElementRef, TemplateRef } from '@angular/core';
import type { Observable } from 'rxjs';

import type { ThemeType } from '@ant-design/icons-angular';

Expand Down Expand Up @@ -1316,3 +1317,25 @@ export interface STOnCellResult {
rowSpan?: number | null;
colSpan?: number | null;
}

export interface STDragOptions {
/**
* Emits when the user drops an item inside the container, default: `moveItemInArray()`
*/
dropped?: (e: CdkDragDrop<any, any, any>) => void;

/**
* Emits when the user has moved a new drag item into this container.
*/
entered?: (e: CdkDragEnter<any>) => void;

/**
* Emits when the user removes an item from the container by dragging it into another container.
*/
exited?: (e: CdkDragExit<any>) => void;

/**
* Emits as the user is swapping items while actively dragging.
*/
sorted?: (e: CdkDragSortEvent<any>) => void;
}
9 changes: 9 additions & 0 deletions packages/theme/system/ng/_drag.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.cdk-drag {
&-preview {
display: table;
}

&-placeholder {
opacity: 0;
}
}
1 change: 1 addition & 0 deletions packages/theme/system/ng/index.less
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import './_preserve-white-spaces.less';
@import './_form.less';
@import './_drag.less';

0 comments on commit f32ee3d

Please sign in to comment.