Skip to content

Commit

Permalink
feat(module:transfer): add nzShowSelectAll & nzRenderList properties (#…
Browse files Browse the repository at this point in the history
…3588)

- close #3567, close #2870
  • Loading branch information
cipchk authored and vthinkxie committed Jun 21, 2019
1 parent d0ad5e8 commit 1619f30
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 64 deletions.
14 changes: 14 additions & 0 deletions components/transfer/demo/table-transfer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 5
title:
zh-CN: 表格穿梭框
en-US: Table Transfer
---

## zh-CN

使用 Table 组件作为自定义渲染列表。

## en-US

Customize render list with Table component.
95 changes: 95 additions & 0 deletions components/transfer/demo/table-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Component, OnInit } from '@angular/core';
import { TransferItem } from 'ng-zorro-antd/transfer';

@Component({
selector: 'nz-demo-transfer-table-transfer',
template: `
<nz-transfer
[nzDataSource]="list"
[nzDisabled]="disabled"
[nzShowSearch]="showSearch"
[nzShowSelectAll]="false"
[nzRenderList]="[renderList, renderList]"
(nzSelectChange)="select($event)"
(nzChange)="change($event)"
>
<ng-template
#renderList
let-items
let-direction="direction"
let-stat="stat"
let-disabled="disabled"
let-onItemSelectAll="onItemSelectAll"
let-onItemSelect="onItemSelect"
>
<nz-table #t [nzData]="convertItems(items)" nzSize="small">
<thead>
<tr>
<th
nzShowCheckbox
[nzDisabled]="disabled"
[nzChecked]="stat.checkAll"
[nzIndeterminate]="stat.checkHalf"
(nzCheckedChange)="onItemSelectAll($event)"
></th>
<th>Name</th>
<th *ngIf="direction === 'left'">Tag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of t.data" (click)="onItemSelect(data)">
<td
nzShowCheckbox
[nzChecked]="data.checked"
[nzDisabled]="disabled || data.disabled"
(nzCheckedChange)="onItemSelect(data)"
></td>
<td>{{ data.title }}</td>
<td *ngIf="direction === 'left'">
<nz-tag>{{ data.tag }}</nz-tag>
</td>
<td>{{ data.description }}</td>
</tr>
</tbody>
</nz-table>
</ng-template>
</nz-transfer>
<div style="margin-top: 8px;">
<nz-switch [(ngModel)]="disabled" nzCheckedChildren="disabled" nzUnCheckedChildren="disabled"></nz-switch>
<nz-switch [(ngModel)]="showSearch" nzCheckedChildren="showSearch" nzUnCheckedChildren="showSearch"></nz-switch>
</div>
`
})
export class NzDemoTransferTableTransferComponent implements OnInit {
// tslint:disable-next-line:no-any
list: any[] = [];
disabled = false;
showSearch = false;

ngOnInit(): void {
for (let i = 0; i < 20; i++) {
this.list.push({
key: i.toString(),
title: `content${i + 1}`,
description: `description of content${i + 1}`,
disabled: i % 4 === 0,
tag: ['cat', 'dog', 'bird'][i % 3]
});
}

[2, 3].forEach(idx => (this.list[idx].direction = 'right'));
}

convertItems(items: TransferItem[]): TransferItem[] {
return items.filter(i => !i.hide);
}

select(ret: {}): void {
console.log('nzSelectChange', ret);
}

change(ret: {}): void {
console.log('nzChange', ret);
}
}
14 changes: 14 additions & 0 deletions components/transfer/demo/tree-transfer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 6
title:
zh-CN: 树穿梭框
en-US: Tree Transfer
---

## zh-CN

使用 Tree 组件作为自定义渲染列表。

## en-US

Customize render list with Tree component.
98 changes: 98 additions & 0 deletions components/transfer/demo/tree-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// tslint:disable: no-any
import { ChangeDetectionStrategy, Component, ViewChild } from '@angular/core';
import { NzTreeNode } from 'ng-zorro-antd/core';
import { TransferChange, TransferItem } from 'ng-zorro-antd/transfer';
import { NzTreeComponent } from 'ng-zorro-antd/tree';

@Component({
selector: 'nz-demo-transfer-tree-transfer',
template: `
<nz-transfer
[nzDataSource]="list"
[nzShowSelectAll]="false"
[nzRenderList]="[leftRenderList, null]"
(nzChange)="change($event)"
>
<ng-template #leftRenderList let-items let-onItemSelectAll="onItemSelectAll" let-onItemSelect="onItemSelect">
<nz-tree #tree [nzData]="treeData" nzExpandAll nzBlockNode>
<ng-template #nzTreeTemplate let-node>
<span
class="ant-tree-checkbox"
[class.ant-tree-checkbox-disabled]="node.isDisabled"
[class.ant-tree-checkbox-checked]="node.isChecked"
(click)="checkBoxChange(node, onItemSelect)"
>
<span class="ant-tree-checkbox-inner"></span>
</span>
<span
(click)="checkBoxChange(node, onItemSelect)"
class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-open"
>{{ node.title }}</span
>
</ng-template>
</nz-tree>
</ng-template>
</nz-transfer>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NzDemoTransferTreeTransferComponent {
@ViewChild('tree', { static: true }) tree: NzTreeComponent;
list: any[] = [
{ id: 1, parentid: 0, title: 'parent 1' },
{ id: 2, parentid: 1, title: 'leaf 1-1', disabled: true, isLeaf: true },
{ id: 3, parentid: 1, title: 'leaf 1-2', isLeaf: true }
];
treeData = this.generateTree(this.list);
checkedNodeList: NzTreeNode[] = [];

private generateTree(arr: TransferItem[]): TransferItem[] {
const tree: TransferItem[] = [];
const mappedArr: any = {};
let arrElem: TransferItem;
let mappedElem: TransferItem;

for (let i = 0, len = arr.length; i < len; i++) {
arrElem = arr[i];
mappedArr[arrElem.id] = { ...arrElem };
mappedArr[arrElem.id].children = [];
}

for (const id in mappedArr) {
if (mappedArr.hasOwnProperty(id)) {
mappedElem = mappedArr[id];
if (mappedElem.parentid) {
mappedArr[mappedElem.parentid].children.push(mappedElem);
} else {
tree.push(mappedElem);
}
}
}
return tree;
}

checkBoxChange(node: NzTreeNode, onItemSelect: (item: TransferItem) => void): void {
if (node.isDisabled) {
return;
}
node.isChecked = !node.isChecked;
if (node.isChecked) {
this.checkedNodeList.push(node);
} else {
const idx = this.checkedNodeList.indexOf(node);
if (idx !== -1) {
this.checkedNodeList.splice(idx, 1);
}
}
const item = this.list.find(w => w.id === node.origin.id);
onItemSelect(item);
}

change(ret: TransferChange): void {
const isDisabled = ret.to === 'right';
this.checkedNodeList.forEach(node => {
node.isDisabled = isDisabled;
node.isChecked = isDisabled;
});
}
}
14 changes: 13 additions & 1 deletion components/transfer/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ import { NzTransferModule } from 'ng-zorro-antd';

| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `[nzDataSource]` | Used for setting the source data. Except the elements whose keys are `direction: 'right'` prop. | `TransferItem[]` | `[]` |
| `[nzDataSource]` | Used for setting the source data. Except the elements whose keys are `direction: 'right'` prop , or using `nzTargetKeys` prop. | `TransferItem[]` | `[]` |
| `[nzDisabled]` | Whether disabled transfer | `boolean` | `false` |
| `[nzTitles]` | A set of titles that are sorted from left to right. | `string[]` | `['', '']` |
| `[nzOperations]` | A set of operations that are sorted from bottom to top. | `string[]` | `['', '']` |
| `[nzListStyle]` | A custom CSS style used for rendering the transfer columns. equal `ngStyle` | `object` | - |
| `[nzItemUnit]` | single unit | `string` | `'item'` |
| `[nzItemsUnit]` | multiple unit | `string` | `'items'` |
| `[nzRenderList]` | Customize render list, please refer to the case. | `Array<TemplateRef<void>|null>` | `[null, null]` |
| `[nzRender]` | The function to generate the item shown on a column. please refer to the case. | `TemplateRef<void>` | - |
| `[nzFooter]` | A function used for rendering the footer. please refer to the case. | `TemplateRef<void>` | - |
| `[nzShowSearch]` | If included, a search box is shown on each column. | `boolean` | `false` |
| `[nzFilterOption]` | A function to determine whether an item should show in search result list | `(inputValue: string, item: TransferItem) => boolean` | - |
| `[nzSearchPlaceholder]` | The hint text of the search box. | `string` | `'Search here'` |
| `[nzNotFoundContent]` | Text to display when a column is empty. | `string` | `'The list is empty'` |
| `[nzCanMove]` | Two verification when transfer choice box. please refer to the case. | `(arg: TransferCanMove) => Observable<TransferItem[]>` | - |
| `[nzTargetKeys]` | A set of keys of elements that are listed on the right column. | `string[]` | - |
| `(nzChange)` | A callback function that is executed when the transfer between columns is complete. | `EventEmitter<TransferChange>` | - |
| `(nzSearchChange)` | A callback function which is executed when search field are changed | `EventEmitter<TransferSearchChange>` | - |
| `(nzSelectChange)` | A callback function which is executed when selected items are changed. | `EventEmitter<TransferSearchChange>` | - |
Expand Down Expand Up @@ -75,3 +77,13 @@ import { NzTransferModule } from 'ng-zorro-antd';
| -------- | ----------- | ---- | ------- |
| direction | data direction | `'left'|'right'` | - |
| value | Search keyword | `string` | - |

#### nzRenderList

| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| `direction` | List render direction | `'left'|'right'` | - |
| `disabled` | Disable list or not | `boolean` | - |
| `items` | Filtered items | `TransferItem[]` | - |
| `onItemSelect` | Select item | `(item: TransferItem) => void` | - |
| `onItemSelectAll` | Select a group of items | `(selected: boolean) => void` | - |
14 changes: 13 additions & 1 deletion components/transfer/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ import { NzTransferModule } from 'ng-zorro-antd';

| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[nzDataSource]` | 数据源,其中若数据属性 `direction: 'right'` 将会被渲染到右边一栏中 | `TransferItem[]` | `[]` |
| `[nzDataSource]` | 数据源,其中若数据属性 `direction: 'right'` 将会被渲染到右边一栏中或使用 `nzTargetKeys` | `TransferItem[]` | `[]` |
| `[nzDisabled]` | 是否禁用 | `boolean` | `false` |
| `[nzTitles]` | 标题集合,顺序从左至右 | `string[]` | `['', '']` |
| `[nzOperations]` | 操作文案集合,顺序从下至上 | `string[]` | `['', '']` |
| `[nzListStyle]` | 两个穿梭框的自定义样式,等同 `ngStyle` | `object` | - |
| `[nzItemUnit]` | 单数单位 | `string` | `'项目'` |
| `[nzItemsUnit]` | 复数单位 | `string` | `'项目'` |
| `[nzRenderList]` | 自定义渲染列表,见示例 | `Array<TemplateRef<void>|null>` | `[null, null]` |
| `[nzRender]` | 每行数据渲染模板,见示例 | `TemplateRef<void>` | - |
| `[nzFooter]` | 底部渲染模板,见示例 | `TemplateRef<void>` | - |
| `[nzShowSearch]` | 是否显示搜索框 | `boolean` | `false` |
| `[nzFilterOption]` | 接收 `inputValue` `option` 两个参数,当 `option` 符合筛选条件时,应返回 `true`,反之则返回 `false`| `(inputValue: string, item: TransferItem) => boolean` | - |
| `[nzSearchPlaceholder]` | 搜索框的默认值 | `string` | `'请输入搜索内容'` |
| `[nzNotFoundContent]` | 当列表为空时显示的内容 | `string` | `'列表为空'` |
| `[nzCanMove]` | 穿梭时二次校验。**注意:** 穿梭组件内部始终只保留一份数据,二次校验过程中需取消穿梭项则直接删除该项;具体用法见示例。 | `(arg: TransferCanMove) => Observable<TransferItem[]>` | - |
| `[nzTargetKeys]` | 显示在右侧框数据的 key 集合 | `string[]` | - |
| `(nzChange)` | 选项在两栏之间转移时的回调函数 | `EventEmitter<TransferChange>` | - |
| `(nzSearchChange)` | 搜索框内容时改变时的回调函数 | `EventEmitter<TransferSearchChange>` | - |
| `(nzSelectChange)` | 选中项发生改变时的回调函数 | `EventEmitter<TransferSearchChange>` | - |
Expand Down Expand Up @@ -77,3 +79,13 @@ import { NzTransferModule } from 'ng-zorro-antd';
| --- | --- | --- | --- |
| direction | 数据方向 | `'left'|'right'` | - |
| value | 搜索关键词 | `string` | - |

#### nzRenderList

| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `direction` | 渲染列表的方向 | `'left'|'right'` | - |
| `disabled` | 是否禁用列表 | `boolean` | - |
| `items` | 过滤后的数据 | `TransferItem[]` | - |
| `onItemSelect` | 勾选条目 | `(item: TransferItem) => void` | - |
| `onItemSelectAll` | 勾选一组条目 | `(selected: boolean) => void` | - |
16 changes: 9 additions & 7 deletions components/transfer/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

export type TransferDirection = 'left' | 'right';

export interface TransferItem {
title: string;
direction?: 'left' | 'right';
direction?: TransferDirection;
disabled?: boolean;
checked?: boolean;
_hiden?: boolean;
hide?: boolean;
// tslint:disable-next-line:no-any
[key: string]: any;
}

export interface TransferCanMove {
direction: string;
direction: TransferDirection;
list: TransferItem[];
}

export interface TransferChange {
from: string;
to: string;
from: TransferDirection;
to: TransferDirection;
list: TransferItem[];
}

export interface TransferSearchChange {
direction: string;
direction: TransferDirection;
value: string;
}

export interface TransferSelectChange {
direction: string;
direction: TransferDirection;
checked: boolean;
list: TransferItem[];
item?: TransferItem;
Expand Down
Loading

0 comments on commit 1619f30

Please sign in to comment.