Skip to content

Commit 3761edd

Browse files
authored
feat: add event update:filterDropdownVisible for Table.Column (#3893)
It's same as event filterDropdownVisibleChange, but can be used with prop as ':filterDropdownVisible.sync="filterDropdownVisible"' for short. It's the base of other props in Table.Column to add event update:prop to use ':prop.sync' shorthand. Next events for other props will be update:filteredValue and update:sortDirections.
1 parent 6fca13f commit 3761edd

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

antdv-demo/docs/table/demo/template.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<cn>
22
#### template 风格的 API
33
使用 template 风格的 API
4+
“Last Name” 列展示了列筛选在 template 风格 API 中的通常用法。
5+
“Tags” 列展示了自定义列筛选以及“双向绑定”的 filterDropdownVisible 属性,其内部使用了事件 update:filterDropdownVisible
46
> 这个只是一个描述 `columns` 的语法糖,所以你不能用其他组件去包裹 `Column``ColumnGroup`
57
</cn>
68
79
<us>
810
#### template style API
911
Using template style API
12+
The "Last Name" column shows the normal usage of column filter using template style API.
13+
The "Tags" column shows a customized column search, and also the "two-way binding" of prop filterDropdownVisible, which use event update:filterDropdownVisible.
1014
> Since this is just a syntax sugar for the prop `columns`, so that you can't compose `Column` and `ColumnGroup` with other Components.
1115
</us>
1216
@@ -18,11 +22,41 @@ Using template style API
1822
<a-table-column key="firstName" data-index="firstName">
1923
<span slot="title" style="color: #1890ff">First Name</span>
2024
</a-table-column>
21-
<a-table-column key="lastName" title="Last Name" data-index="lastName" />
25+
<a-table-column
26+
key="lastName"
27+
title="Last Name"
28+
data-index="lastName"
29+
:filters="[
30+
{ text: 'Brown', value: 'Brown' },
31+
{ text: 'Green', value: 'Green' },
32+
{ text: 'Black', value: 'Black' },
33+
]"
34+
:default-filtered-value="['Brown', 'Green']"
35+
@filter="(value, record) => record.lastName == value"
36+
/>
2237
</a-table-column-group>
2338
<a-table-column key="age" title="Age" data-index="age" />
2439
<a-table-column key="address" title="Address" data-index="address" />
25-
<a-table-column key="tags" title="Tags" data-index="tags">
40+
<a-table-column
41+
key="tags"
42+
:title="`Tags ${tagsFilterDropdownVisible ? 'filtering' : ''}`"
43+
data-index="tags"
44+
:filtered-value="filteredTags"
45+
@filter="(value, record) => record.tags.includes(value)"
46+
:filter-dropdown-visible.sync="tagsFilterDropdownVisible"
47+
>
48+
<template slot="filterDropdown" slot-scope="{ setSelectedKeys, selectedKeys }">
49+
<div style="background-color: white; border: solid;">
50+
<a-checkbox
51+
v-for="tag in ['nice', 'cool', 'loser']"
52+
:key="tag"
53+
:checked="filteredTags.includes(tag)"
54+
@change="filteredTags = toggleSelectedTags(filteredTags, tag, $event.target.checked)"
55+
>
56+
{{ tag }}
57+
</a-checkbox>
58+
</div>
59+
</template>
2660
<template slot-scope="tags">
2761
<span>
2862
<a-tag v-for="tag in tags" :key="tag" color="blue">{{ tag }}</a-tag>
@@ -72,8 +106,21 @@ export default {
72106
data() {
73107
return {
74108
data,
109+
filteredTags: [],
110+
tagsFilterDropdownVisible: false,
75111
};
76112
},
113+
methods: {
114+
toggleSelectedTags(oldTags, tag, checked) {
115+
let newTags = [...oldTags];
116+
if (checked) {
117+
newTags.push(tag);
118+
} else {
119+
newTags = newTags.filter(t => t != tag);
120+
}
121+
return newTags;
122+
},
123+
},
77124
};
78125
</script>
79126
```

antdv-demo/docs/table/index.en-US.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ One of the Table `columns` prop for describing the table's columns, Column has t
8585
| defaultFilteredValue | Default filtered values | string\[] | - | 1.5.0 |
8686
| defaultSortOrder | Default order of sorted values: `'ascend'` `'descend'` `null` | string | - | |
8787
| filterDropdown | Customized filter overlay | slot \| slot-scope | - | |
88-
| filterDropdownVisible | Whether `filterDropdown` is visible | boolean | - | |
88+
| filterDropdownVisible | Whether `filterDropdown` is visible. Can be used with `.sync` in template, see `update:filterDropdownVisible` | boolean | - | |
8989
| filtered | Whether the `dataSource` is filtered | boolean | `false` | |
9090
| filteredValue | Controlled filtered value, filter icon will highlight | string\[] | - | |
9191
| filterIcon | Customized filter icon | slot \| slot-scope \| (filtered: boolean, column: Column) | `false` | |
@@ -102,7 +102,7 @@ One of the Table `columns` prop for describing the table's columns, Column has t
102102
| customCell | Set props on per cell | Function(record, rowIndex) | - | |
103103
| customHeaderCell | Set props on per header cell | Function(column) | - | |
104104
| onFilter | Callback executed when the confirm filter button is clicked, Use as a `filter` event when using template or jsx | Function | - | |
105-
| onFilterDropdownVisibleChange | Callback executed when `filterDropdownVisible` is changed, Use as a `filterDropdownVisible` event when using template or jsx | function(visible) {} | - | |
105+
| onFilterDropdownVisibleChange <br> @filterDropdownVisibleChange <br> @update:filterDropdownVisible | Callback executed when `filterDropdownVisible` is changed, Use as a `filterDropdownVisibleChange` or `update:filterDropdownVisible` event when using template or jsx | function(visible) {} | - | |
106106
| slots | When using columns, you can use this property to configure the properties that support the slot, such as `slots: { filterIcon: 'XXX'}` | object | - | |
107107
| scopedSlots | When using columns, you can use this property to configure the properties that support the slot-scope, such as `scopedSlots: { customRender: 'XXX'}` | object | - | |
108108

antdv-demo/docs/table/index.zh-CN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
| dataIndex | 列数据在数据项中对应的 key,支持 `a.b.c` 的嵌套写法 | string | - | |
8686
| defaultFilteredValue | 默认筛选值 | string\[] | - | 1.5.0 |
8787
| filterDropdown | 可以自定义筛选菜单,此函数只负责渲染图层,需要自行编写各种交互 | VNode \| slot-scope | - | |
88-
| filterDropdownVisible | 用于控制自定义筛选菜单是否可见 | boolean | - | |
88+
| filterDropdownVisible | 用于控制自定义筛选菜单是否可见。在 template 中可用 `.sync` 后缀, 参见 `update:filterDropdownVisible` | boolean | - | |
8989
| filtered | 标识数据是否经过过滤,筛选图标会高亮 | boolean | false | |
9090
| filteredValue | 筛选的受控属性,外界可用此控制列的筛选状态,值为已筛选的 value 数组 | string\[] | - | |
9191
| filterIcon | 自定义 filter 图标。 | VNode \| (filtered: boolean, column: Column) => vNode \|slot \|slot-scope | false | |
@@ -102,7 +102,7 @@
102102
| customCell | 设置单元格属性 | Function(record, rowIndex) | - | |
103103
| customHeaderCell | 设置头部单元格属性 | Function(column) | - | |
104104
| onFilter | 本地模式下,确定筛选的运行函数, 使用 template 或 jsx 时作为`filter`事件使用 | Function | - | |
105-
| onFilterDropdownVisibleChange | 自定义筛选菜单可见变化时调用,使用 template 或 jsx 时作为`filterDropdownVisibleChange`事件使用 | function(visible) {} | - | |
105+
| onFilterDropdownVisibleChange <br> @filterDropdownVisibleChange <br> @update:filterDropdownVisible | 自定义筛选菜单可见`filterDropdownVisible`变化时调用,使用 template 或 jsx 时作为`filterDropdownVisibleChange``update:filterDropdownVisible`事件使用 | function(visible) {} | - | |
106106
| slots | 使用 columns 时,可以通过该属性配置支持 slot 的属性,如 `slots: { filterIcon: 'XXX'}` | object | - | |
107107
| scopedSlots | 使用 columns 时,可以通过该属性配置支持 slot-scope 的属性,如 `scopedSlots: { customRender: 'XXX'}` | object | - | |
108108

components/table/index.jsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,25 @@ const Table = {
3535
const events = getEvents(element);
3636
const listeners = {};
3737
Object.keys(events).forEach(e => {
38-
const k = `on-${e}`;
38+
/*
39+
Convert events on template Column to function props onPropAbcChange in Table.columns prop.
40+
If you write template code like below:
41+
<Column @prop-abc-change="f1" @update:prop-abc="f2" :prop-abc.sync="dataAbc" />
42+
You will get these events:
43+
{
44+
'prop-abc-change': this.f1,
45+
'update:prop-abc': [this.f2, e => this.dataAbc = e],
46+
'update:propAbc': e => this.dataAbc = e,
47+
}
48+
All of these events would be treat as column.onPropAbcChange,
49+
but only one of them will be valid, which can not be determined.
50+
*/
51+
let k;
52+
if (e.startsWith('update:')) {
53+
k = `on-${e.substr('update:'.length)}-change`;
54+
} else {
55+
k = `on-${e}`;
56+
}
3957
listeners[camelize(k)] = events[e];
4058
});
4159
const { default: children, ...restSlots } = getSlots(element);

0 commit comments

Comments
 (0)