Skip to content

Commit

Permalink
feat(module:table): support display and sorting of custom table colum…
Browse files Browse the repository at this point in the history
…ns (#7966)
  • Loading branch information
OriginRing committed Jul 16, 2023
1 parent 4143473 commit d26870f
Show file tree
Hide file tree
Showing 13 changed files with 607 additions and 4 deletions.
14 changes: 14 additions & 0 deletions components/table/demo/custom-column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 31
title:
en-US: Custom Column
zh-CN: 自定义列
---

## zh-CN

控制表格中列的展示与排序

## en-US

Control the display and ordering of columns in a table.
280 changes: 280 additions & 0 deletions components/table/demo/custom-column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';

import { NzCustomColumn } from 'ng-zorro-antd/table';

interface Person {
key: string;
name: string;
gender: 'male' | 'female';
age: number;
address: string;
}

interface CustomColumn extends NzCustomColumn {
name: string;
required?: boolean;
position?: 'left' | 'right';
}

@Component({
selector: 'nz-demo-table-custom-column',
template: `
<button nz-button nzType="primary" nzSize="small" (click)="showModal()" style="margin-bottom: 8px;">
<span nz-icon nzType="setting" nzTheme="outline"></span>
</button>
<nz-table #basicTable [nzData]="listOfData" [nzCustomColumn]="customColumn">
<thead>
<tr>
<th nzCellControl="name">Name</th>
<th nzCellControl="gender">Gender</th>
<th nzCellControl="age">Age</th>
<th nzCellControl="address">Address</th>
<th nzCellControl="action">Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of basicTable.data">
<td nzCellControl="name">{{ data.name }}</td>
<td nzCellControl="gender">{{ data.gender }}</td>
<td nzCellControl="age">{{ data.age }}</td>
<td nzCellControl="address">{{ data.address }}</td>
<td nzCellControl="action">
<a>Action</a>
<nz-divider nzType="vertical"></nz-divider>
<a>Delete</a>
</td>
</tr>
</tbody>
</nz-table>
<nz-modal [(nzVisible)]="isVisible" nzTitle="Custom Column" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()">
<ng-container *nzModalContent>
<div nz-row [nzGutter]="24">
<div nz-col class="gutter-row" [nzSpan]="12">
<div class="example-container">
<p>Displayed (drag and drop to sort)</p>
<div class="example-box" *ngFor="let item of title">
{{ item.name }}
</div>
<div
cdkDropList
#todoList="cdkDropList"
[cdkDropListData]="fix"
[cdkDropListConnectedTo]="[doneList]"
class="example-list"
(cdkDropListDropped)="drop($event)"
>
<div class="example-box" *ngFor="let item of fix; let i = index" cdkDrag>
{{ item.name }}
<span nz-icon nzType="minus-circle" nzTheme="outline" (click)="deleteCustom(item, i)"></span>
</div>
</div>
<div class="example-box" *ngFor="let item of footer">
{{ item.name }}
</div>
</div>
</div>
<div nz-col class="gutter-row" [nzSpan]="12">
<div class="example-container">
<p>Not Shown</p>
<div
cdkDropList
#doneList="cdkDropList"
[cdkDropListData]="notFix"
[cdkDropListConnectedTo]="[todoList]"
class="example-list"
(cdkDropListDropped)="drop($event)"
>
<div class="example-box" *ngFor="let item of notFix; let i = index" cdkDrag>
{{ item.name }}
<span nz-icon nzType="plus-circle" nzTheme="outline" (click)="addCustom(item, i)"></span>
</div>
</div>
</div>
</div>
</div>
</ng-container>
</nz-modal>
`,
styles: [
`
.example-container {
height: 350px;
display: flex;
flex-direction: column;
}
.example-list {
min-height: 60px;
border-radius: 4px;
overflow-x: hidden;
overflow-y: auto;
display: block;
border: 1px dashed #ccc;
flex: 1 1 auto;
}
.example-list > .example-box {
cursor: move;
}
.cdk-drag-preview {
box-sizing: border-box;
border-radius: 4px;
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2), 0 8px 10px 1px rgba(0, 0, 0, 0.14),
0 3px 14px 2px rgba(0, 0, 0, 0.12);
}
.cdk-drag-placeholder {
opacity: 0;
}
.cdk-drag-animating {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-box {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
box-sizing: border-box;
margin: 4px;
padding: 4px 8px;
background-color: rgb(0 112 204 / 15%);
}
.example-box span {
cursor: pointer;
}
`
]
})
export class NzDemoTableCustomColumnComponent implements OnInit {
listOfData: Person[] = [
{
key: '1',
name: 'John Brown',
gender: 'female',
age: 32,
address: 'New York No. 1 Lake Park'
},
{
key: '2',
name: 'Jim Green',
gender: 'female',
age: 42,
address: 'London No. 1 Lake Park'
},
{
key: '3',
name: 'Joe Black',
gender: 'male',
age: 32,
address: 'Sidney No. 1 Lake Park'
}
];

customColumn: CustomColumn[] = [
{
name: 'Name',
value: 'name',
default: true,
required: true,
position: 'left',
width: 200,
fixWidth: true
},
{
name: 'Gender',
value: 'gender',
default: true,
width: 200
},
{
name: 'Address',
value: 'address',
default: true,
width: 200
},
{
name: 'Age',
value: 'age',
default: true,
width: 200
},
{
name: 'Action',
value: 'action',
default: true,
required: true,
position: 'right',
width: 200
}
];

isVisible: boolean = false;
title: CustomColumn[] = [];
footer: CustomColumn[] = [];
fix: CustomColumn[] = [];
notFix: CustomColumn[] = [];

constructor(private cdr: ChangeDetectorRef) {}

ngOnInit(): void {
this.title = this.customColumn.filter(item => item.position === 'left' && item.required);
this.footer = this.customColumn.filter(item => item.position === 'right' && item.required);
this.fix = this.customColumn.filter(item => item.default && !item.required);
this.notFix = this.customColumn.filter(item => !item.default && !item.required);
}

drop(event: CdkDragDrop<CustomColumn[]>): void {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex);
}
this.fix = this.fix.map(item => {
item.default = true;
return item;
});
this.notFix = this.notFix.map(item => {
item.default = false;
return item;
});
this.cdr.markForCheck();
}

deleteCustom(value: CustomColumn, index: number): void {
value.default = false;
this.notFix = [...this.notFix, value];
this.fix.splice(index, 1);
this.cdr.markForCheck();
}

addCustom(value: CustomColumn, index: number): void {
value.default = true;
this.fix = [...this.fix, value];
this.notFix.splice(index, 1);
this.cdr.markForCheck();
}

showModal(): void {
this.isVisible = true;
}

handleOk(): void {
this.customColumn = [...this.title, ...this.fix, ...this.notFix, ...this.footer];
this.isVisible = false;
this.cdr.markForCheck();
}

handleCancel(): void {
this.isVisible = false;
}
}
4 changes: 4 additions & 0 deletions components/table/demo/module
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import { NzBadgeModule } from 'ng-zorro-antd/badge';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzGridModule } from 'ng-zorro-antd/grid';
import { NzPopconfirmModule } from 'ng-zorro-antd/popconfirm';

export const moduleList = [
NzTableModule,
NzGridModule,
NzModalModule,
NzDividerModule,
NzDropDownModule,
NzSwitchModule,
Expand Down
4 changes: 4 additions & 0 deletions components/table/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The data passed to `[nzData]` is exported with [Template Context](https://angula
| `[nzData]` | Data record array to be rendered | `T[]` | - |
| `[nzFrontPagination]` | Whether to paginate data on client. Should be set to `false` if data is to be paginated on server side or if all the data is to be displayed at once in the table without any pagination | `boolean` | `true` |
| `[nzTotal]` | Total data count. Should set when `nzFrontPagination` is `false` | `number` | - |
| `[nzCustomColumn]` | Control the display and sorting of table columns, (after enabling `nzWidthConfig` and `[nzWidth]` of `th` will not take effect) | `NzCustomColumn[]` | - |
| `[nzPageIndex]` | pageIndex , double binding | `number` | - |
| `[nzPageSize]` | pageSize, double binding | `number` | - |
| `[nzShowPagination]` | Whether to show pagination component at bottom of the table | `boolean` | `true` |
Expand Down Expand Up @@ -98,6 +99,7 @@ The data passed to `[nzData]` is exported with [Template Context](https://angula
| `(nzPageIndexChange)` | Callback when `pageIndex` changes | `EventEmitter<number>` | - |
| `(nzPageSizeChange)` | Callback when `pageSize` changes | `EventEmitter<number>` | - |
| `(nzCurrentPageDataChange)` | Callback when current pageData changes | `EventEmitter<T[]>` | - |
| `(nzCustomColumnChange)` | Callback when the table is reordered | `EventEmitter<NzCustomColumn[]>` | - |
| `(nzQueryParams)` | Callback with params when working with server side pagination, sorting and filtering | `EventEmitter<NzTableQueryParams>` | - |

### th
Expand Down Expand Up @@ -150,6 +152,7 @@ Style property
| `[nzLeft]` | Left pixels, used to fixed column to left, auto calc when set to `true` and disable fixed when `false` | `string \| boolean` | - |
| `[nzRight]` | Right pixels, used to fixed column to right, auto calc when set to `true` and disable fixed when `false` | `string \| boolean` | - |
| `[nzAlign]` | Specify how content is aligned | `'left' \| 'right' \| 'center'` | - |
| `[nzCellControl]` | Set the position of the column, which is the value of the `value` field in the `NzCustomColumn` type | `string` | - |
| `[nzBreakWord]` | Whether insert line breaks within words | `boolean` | `false` |
| `[nzEllipsis]` | ellipsis cell content, not working with sorter and filters for now. Only work when nzTableLayout was `fixed` | `boolean` | `false` |

Expand Down Expand Up @@ -190,6 +193,7 @@ Style property
| `[nzLeft]` | Left pixels, used to fixed column to left, auto calc when set to `true` and disable fixed when `false` | `string \| boolean` | - |
| `[nzRight]` | Right pixels, used to fixed column to right, auto calc when set to `true` and disable fixed when `false` | `string \| boolean` | - |
| `[nzAlign]` | Specify how content is aligned | `'left' \| 'right' \| 'center'` | - |
| `[nzCellControl]` | Set the position of the column, which is the value of the `value` field in the `NzCustomColumn` type | `string` | - |
| `[nzBreakWord]` | Whether insert line breaks within words | `boolean` | `false` |
| `[nzEllipsis]` | ellipsis cell content, not working with sorter and filters for now. Only work when nzTableLayout was `fixed` | `boolean` | `false` |

Expand Down
4 changes: 4 additions & 0 deletions components/table/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Table 组件同时具备了易用性和高度可定制性
| `[nzData]` | 数据数组 | `T[]` | - |
| `[nzFrontPagination]` | 是否在前端对数据进行分页,如果在服务器分页数据或者需要在前端显示全部数据时传入 false | `boolean` | `true` |
| `[nzTotal]` | 当前总数据,在服务器渲染时需要传入 | `number` | - |
| `[nzCustomColumn]` | 控制表格列的展示与排序,(开启后 `nzWidthConfig``th``[nzWidth]` 将不生效) | `NzCustomColumn[]` | - |
| `[nzPageIndex]` | 当前页码,可双向绑定 | `number` | - |
| `[nzPageSize]` | 每页展示多少数据,可双向绑定 | `number` | - |
| `[nzShowPagination]` | 是否显示分页器 | `boolean` | `true` |
Expand Down Expand Up @@ -99,6 +100,7 @@ Table 组件同时具备了易用性和高度可定制性
| `(nzPageIndexChange)` | 当前页码改变时的回调函数 | `EventEmitter<number>` | - |
| `(nzPageSizeChange)` | 页数改变时的回调函数 | `EventEmitter<number>` | - |
| `(nzCurrentPageDataChange)` | 当前页面展示数据改变的回调函数 | `EventEmitter<T[]>` | - |
| `(nzCustomColumnChange)` | 当表格重新排序后的回调 | `EventEmitter<NzCustomColumn[]>` | - |
| `(nzQueryParams)` | 当服务端分页、筛选、排序时,用于获得参数,具体见示例 | `EventEmitter<NzTableQueryParams>` | - |

### th
Expand Down Expand Up @@ -148,6 +150,7 @@ Table 组件同时具备了易用性和高度可定制性
| `[nzLeft]` | 左侧距离,用于固定左侧列,当为 `true` 时自动计算,为 `false` 时停止固定 | `string \| boolean` | `false` |
| `[nzRight]` | 右侧距离,用于固定右侧列,当为 `true` 时自动计算,为 `false` 时停止固定 | `string \| boolean` | `false` |
| `[nzAlign]` | 设置列内容的对齐方式 | `'left' \| 'right' \| 'center'` | - |
| `[nzCellControl]` | 设置列的位置,该值为 `NzCustomColumn` 类型中 `value` 字段的值 | `string` | - |
| `[nzBreakWord]` | 是否折行显示 | `boolean` | `false` |
| `[nzEllipsis]` | 超过宽度将自动省略,暂不支持和排序筛选一起使用。仅当表格布局将为 `nzTableLayout="fixed"`时可用 | `boolean` | `false` |
| `[colSpan]` | 每单元格中扩展列的数量 | `number` | `null` |
Expand Down Expand Up @@ -190,6 +193,7 @@ Table 组件同时具备了易用性和高度可定制性
| `[nzLeft]` | 左侧距离,用于固定左侧列,当为 `true` 时自动计算,为 `false` 时停止固定 | `string \| boolean` | `false` |
| `[nzRight]` | 右侧距离,用于固定右侧列,当为 `true` 时自动计算,为 `false` 时停止固定 | `string \| boolean` | `false` |
| `[nzAlign]` | 设置列内容的对齐方式 | `'left' \| 'right' \| 'center'` | - |
| `[nzCellControl]` | 设置列的位置,该值为 `NzCustomColumn` 类型中 `value` 字段的值 | `string` | - |
| `[nzBreakWord]` | 是否折行显示 | `boolean` | `false` |
| `[nzEllipsis]` | 超过宽度将自动省略,暂不支持和排序筛选一起使用。仅当表格布局将为 `nzTableLayout="fixed"`时可用 | `boolean` | `false` |

Expand Down
1 change: 1 addition & 0 deletions components/table/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './src/table/title-footer.component';
export * from './src/table/tr-measure.component';
export * from './src/cell/cell-fixed.directive';
export * from './src/cell/cell.directive';
export * from './src/cell/custom-column.directive';
export * from './src/cell/th-measure.directive';
export * from './src/cell/td-addon.component';
export * from './src/cell/th-selection.component';
Expand Down
Loading

0 comments on commit d26870f

Please sign in to comment.