From 1619f30c7e6b927e13ad211473364bde260cd75e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Fri, 21 Jun 2019 17:48:57 +0800 Subject: [PATCH] feat(module:transfer): add nzShowSelectAll & nzRenderList properties (#3588) - close #3567, close #2870 --- components/transfer/demo/table-transfer.md | 14 +++ components/transfer/demo/table-transfer.ts | 95 ++++++++++++++++++ components/transfer/demo/tree-transfer.md | 14 +++ components/transfer/demo/tree-transfer.ts | 98 +++++++++++++++++++ components/transfer/doc/index.en-US.md | 14 ++- components/transfer/doc/index.zh-CN.md | 14 ++- components/transfer/interface.ts | 16 +-- .../transfer/nz-transfer-list.component.html | 44 ++++++--- .../transfer/nz-transfer-list.component.ts | 32 +++--- .../transfer/nz-transfer.component.html | 4 + components/transfer/nz-transfer.component.ts | 46 ++++++--- components/transfer/transfer.spec.ts | 51 +++++++--- 12 files changed, 378 insertions(+), 64 deletions(-) create mode 100644 components/transfer/demo/table-transfer.md create mode 100644 components/transfer/demo/table-transfer.ts create mode 100644 components/transfer/demo/tree-transfer.md create mode 100644 components/transfer/demo/tree-transfer.ts diff --git a/components/transfer/demo/table-transfer.md b/components/transfer/demo/table-transfer.md new file mode 100644 index 00000000000..d6583e48ee7 --- /dev/null +++ b/components/transfer/demo/table-transfer.md @@ -0,0 +1,14 @@ +--- +order: 5 +title: + zh-CN: 表格穿梭框 + en-US: Table Transfer +--- + +## zh-CN + +使用 Table 组件作为自定义渲染列表。 + +## en-US + +Customize render list with Table component. diff --git a/components/transfer/demo/table-transfer.ts b/components/transfer/demo/table-transfer.ts new file mode 100644 index 00000000000..4bfded04ea7 --- /dev/null +++ b/components/transfer/demo/table-transfer.ts @@ -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: ` + + + + + + + Name + Tag + Description + + + + + + {{ data.title }} + + {{ data.tag }} + + {{ data.description }} + + + + + +
+ + +
+ ` +}) +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); + } +} diff --git a/components/transfer/demo/tree-transfer.md b/components/transfer/demo/tree-transfer.md new file mode 100644 index 00000000000..8593e5fd442 --- /dev/null +++ b/components/transfer/demo/tree-transfer.md @@ -0,0 +1,14 @@ +--- +order: 6 +title: + zh-CN: 树穿梭框 + en-US: Tree Transfer +--- + +## zh-CN + +使用 Tree 组件作为自定义渲染列表。 + +## en-US + +Customize render list with Tree component. diff --git a/components/transfer/demo/tree-transfer.ts b/components/transfer/demo/tree-transfer.ts new file mode 100644 index 00000000000..5787c61e70c --- /dev/null +++ b/components/transfer/demo/tree-transfer.ts @@ -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: ` + + + + + + + + {{ node.title }} + + + + + `, + 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; + }); + } +} diff --git a/components/transfer/doc/index.en-US.md b/components/transfer/doc/index.en-US.md index 6b428dd9423..c9dd2b8f534 100644 --- a/components/transfer/doc/index.en-US.md +++ b/components/transfer/doc/index.en-US.md @@ -27,13 +27,14 @@ 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|null>` | `[null, null]` | | `[nzRender]` | The function to generate the item shown on a column. please refer to the case. | `TemplateRef` | - | | `[nzFooter]` | A function used for rendering the footer. please refer to the case. | `TemplateRef` | - | | `[nzShowSearch]` | If included, a search box is shown on each column. | `boolean` | `false` | @@ -41,6 +42,7 @@ import { NzTransferModule } from 'ng-zorro-antd'; | `[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` | - | +| `[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` | - | | `(nzSearchChange)` | A callback function which is executed when search field are changed | `EventEmitter` | - | | `(nzSelectChange)` | A callback function which is executed when selected items are changed. | `EventEmitter` | - | @@ -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` | - | diff --git a/components/transfer/doc/index.zh-CN.md b/components/transfer/doc/index.zh-CN.md index fae0c2252f5..ad95ecdd0fd 100644 --- a/components/transfer/doc/index.zh-CN.md +++ b/components/transfer/doc/index.zh-CN.md @@ -29,13 +29,14 @@ 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|null>` | `[null, null]` | | `[nzRender]` | 每行数据渲染模板,见示例 | `TemplateRef` | - | | `[nzFooter]` | 底部渲染模板,见示例 | `TemplateRef` | - | | `[nzShowSearch]` | 是否显示搜索框 | `boolean` | `false` | @@ -43,6 +44,7 @@ import { NzTransferModule } from 'ng-zorro-antd'; | `[nzSearchPlaceholder]` | 搜索框的默认值 | `string` | `'请输入搜索内容'` | | `[nzNotFoundContent]` | 当列表为空时显示的内容 | `string` | `'列表为空'` | | `[nzCanMove]` | 穿梭时二次校验。**注意:** 穿梭组件内部始终只保留一份数据,二次校验过程中需取消穿梭项则直接删除该项;具体用法见示例。 | `(arg: TransferCanMove) => Observable` | - | +| `[nzTargetKeys]` | 显示在右侧框数据的 key 集合 | `string[]` | - | | `(nzChange)` | 选项在两栏之间转移时的回调函数 | `EventEmitter` | - | | `(nzSearchChange)` | 搜索框内容时改变时的回调函数 | `EventEmitter` | - | | `(nzSelectChange)` | 选中项发生改变时的回调函数 | `EventEmitter` | - | @@ -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` | - | diff --git a/components/transfer/interface.ts b/components/transfer/interface.ts index 841bed37380..0467b9b08d4 100644 --- a/components/transfer/interface.ts +++ b/components/transfer/interface.ts @@ -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; diff --git a/components/transfer/nz-transfer-list.component.html b/components/transfer/nz-transfer-list.component.html index 3bc8566945b..629bb848f0f 100644 --- a/components/transfer/nz-transfer-list.component.html +++ b/components/transfer/nz-transfer-list.component.html @@ -1,5 +1,22 @@ + +
    +
    +
  • + +
  • +
    +
+
+ +
+
- @@ -17,21 +34,18 @@ [disabled]="disabled" [value]="filter">
-
    -
    -
  • - -
  • + +
    +
    -
-
- -
+