Merge branch 'develop' of ***REMOVED*** into develop

This commit is contained in:
07akioni 2020-03-27 17:40:36 +08:00
commit 68424055d5
8 changed files with 47 additions and 67 deletions

View File

@ -1,4 +1,10 @@
# CHANGELOG # CHANGELOG
## 1.0.5 (2020-03-27)
### Features
- Change the data type of `n-data-table`'s filters from Array to Object.
### Fixes
- `n-data-table` cannot be filtered correctly when there are multiple filtered columns.
## 1.0.4 (2020-03-26) ## 1.0.4 (2020-03-26)
### Features ### Features
- Filter menu in `n-data-table` is scrollable when there are too many items. - Filter menu in `n-data-table` is scrollable when there are too many items.

View File

@ -1,4 +1,10 @@
# CHANGELOG # CHANGELOG
## 1.0.5 (2020-03-27)
### Features
- 改变 `n-data-table` 的 filters 的数据类型从数组改变成对象
### Fixes
- `n-data-table` 在有多列筛选的情况下数据不能被正确筛选
## 1.0.4 (2020-03-26) ## 1.0.4 (2020-03-26)
### Features ### Features
- 当选项过多时,`n-data-table` 过滤菜单的内容可以滚动 - 当选项过多时,`n-data-table` 过滤菜单的内容可以滚动

View File

@ -96,9 +96,8 @@ export default {
this.addressColumn.filterOptionValues = [] this.addressColumn.filterOptionValues = []
}, },
handleFiltersChange (filters, sourceColumn) { handleFiltersChange (filters, sourceColumn) {
this.addressColumn.filterOptionValues = filters.filter( console.log(filters, sourceColumn)
filter => filter.columnKey === sourceColumn.key this.addressColumn.filterOptionValues = filters[sourceColumn.key]
).map(filter => filter.filterOptionValue)
} }
} }
} }

View File

@ -55,8 +55,8 @@ These methods can help you control table in an uncontrolled manner. However, it'
|Name|Type|Description| |Name|Type|Description|
|-|-|-| |-|-|-|
|filters|`( columnKey: string \| number, filterOptionValue: string \| number } \| Array<{ columnKey: string \| number, filterOptionValue: string \| number }>)`|| |filters|`(filters: { [string \| number]: Array<string \| number> }) => void`|Key is the key of the column, value is the filterOptionValues.|
|sort|`(columnKey: string \| null, order: 'ascend' \| 'descend' \| false)`|If columnKey set to `null`, it is same as clearSorter| |sort|`(columnKey: string \| null, order: 'ascend' \| 'descend' \| false) => void`|If columnKey set to `null`, it is same as clearSorter|
|page|`(page: number) => void`|| |page|`(page: number) => void`||
|clearFilters|`() => void`|| |clearFilters|`() => void`||
|clearSorter|`() => void`|| |clearSorter|`() => void`||
@ -65,7 +65,7 @@ These methods can help you control table in an uncontrolled manner. However, it'
## Events ## Events
|Name|Parameters|Description| |Name|Parameters|Description|
|-|-|-| |-|-|-|
|filters-change|`(Array<{ columnKey: string \| number, filterOptionValue: string \| number }>, initiatorColumn: object)`|| |filters-change|`(filters: { [string \| number]: Array<string \| number> }, initiatorColumn: object)`||
|sorter-change|`({ columnKey: string \| number, sorter: 'default' \| function \| boolean, order: 'ascend' \| 'descend' \| false } \| null)`|If there won't be a active sorter after change, sorter-change will emit `null`| |sorter-change|`({ columnKey: string \| number, sorter: 'default' \| function \| boolean, order: 'ascend' \| 'descend' \| false } \| null)`|If there won't be a active sorter after change, sorter-change will emit `null`|
|page-change|`(page: number)`|| |page-change|`(page: number)`||
|page-size-change|`(pageSize: number)`|| |page-size-change|`(pageSize: number)`||

View File

@ -96,9 +96,8 @@ export default {
this.addressColumn.filterOptionValues = [] this.addressColumn.filterOptionValues = []
}, },
handleFiltersChange (filters, sourceColumn) { handleFiltersChange (filters, sourceColumn) {
this.addressColumn.filterOptionValues = filters.filter( console.log(filters, sourceColumn)
filter => filter.columnKey === sourceColumn.key this.addressColumn.filterOptionValues = filters[sourceColumn.key]
).map(filter => filter.filterOptionValue)
} }
} }
} }

View File

@ -55,8 +55,8 @@ ajaxUsage
|名称|参数|说明| |名称|参数|说明|
|-|-|-| |-|-|-|
|filters|`( columnKey: string \| number, filterOptionValue: string \| number } \| Array<{ columnKey: string \| number, filterOptionValue: string \| number }>)`|| |filters|`(filters: { [string \| number]: Array<string \| number> }) => void`| Key 是列的 key, value 是 filterOptionValues.|
|sort|`(columnKey: string \| null, order: 'ascend' \| 'descend' \| false)`|如果 columnKey 设为 `null`,那它和 clearSorter 效果一致| |sort|`(columnKey: string \| null, order: 'ascend' \| 'descend' \| false) => void`|如果 columnKey 设为 `null`,那它和 clearSorter 效果一致|
|page|`(page: number) => void`|| |page|`(page: number) => void`||
|clearFilters|`() => void`|| |clearFilters|`() => void`||
|clearSorter|`() => void`|| |clearSorter|`() => void`||
@ -65,7 +65,7 @@ ajaxUsage
## Events ## Events
|名称|参数|说明| |名称|参数|说明|
|-|-|-| |-|-|-|
|filters-change|`(Array<{ columnKey: string \| number, filterOptionValue: string \| number }>, initiatorColumn: object)`|| |filters-change|`(filters: { [string \| number]: Array<string \| number> }, initiatorColumn: object)`||
|sorter-change|`({ columnKey: string \| number, sorter: 'default' \| function \| boolean, order: 'ascend' \| 'descend' \| false } \| null)`|如果在变动后没有激活的排序,那么 sorter-change 将发出 `null`| |sorter-change|`({ columnKey: string \| number, sorter: 'default' \| function \| boolean, order: 'ascend' \| 'descend' \| false } \| null)`|如果在变动后没有激活的排序,那么 sorter-change 将发出 `null`|
|page-change|`(page: number)`|| |page-change|`(page: number)`||
|page-size-change|`(pageSize: number)`|| |page-size-change|`(pageSize: number)`||

View File

@ -68,11 +68,7 @@ import BaseTable from './BaseTable.vue'
import NEmpty from '../../Empty' import NEmpty from '../../Empty'
import NPagination from '../../Pagination' import NPagination from '../../Pagination'
import formatLength from '../../_utils/css/formatLength' import formatLength from '../../_utils/css/formatLength'
import isPlainObject from 'lodash-es/isPlainObject'
function createShallowClonedArray (array) {
if (Array.isArray(array)) return array.map(createShallowClonedObject)
return array
}
function createShallowClonedObject (object) { function createShallowClonedObject (object) {
if (!object) return object if (!object) return object
@ -213,7 +209,7 @@ export default {
/** internal checked rows */ /** internal checked rows */
internalCheckedRowKeys: [], internalCheckedRowKeys: [],
/** internal filters state */ /** internal filters state */
internalActiveFilters: [], internalActiveFilters: {},
/** internal sorter state */ /** internal sorter state */
internalActiveSorter: null, internalActiveSorter: null,
/** internal pagination state */ /** internal pagination state */
@ -271,9 +267,7 @@ export default {
} }
return this.data ? this.data.filter(row => { return this.data ? this.data.filter(row => {
for (const columnKey of Object.keys(row)) { for (const columnKey of Object.keys(row)) {
const activeFilterOptionValues = syntheticActiveFilters const activeFilterOptionValues = syntheticActiveFilters[columnKey] || []
.filter(filterInfo => filterInfo.columnKey === columnKey)
.map(filterInfo => filterInfo.filterOptionValue)
if (!activeFilterOptionValues.length) continue if (!activeFilterOptionValues.length) continue
const columnToFilter = normalizedColumns.find(column => column.key === columnKey) const columnToFilter = normalizedColumns.find(column => column.key === columnKey)
/** /**
@ -289,7 +283,7 @@ export default {
} }
} else { } else {
if (activeFilterOptionValues.some(filterOptionValue => filter(filterOptionValue, row))) { if (activeFilterOptionValues.some(filterOptionValue => filter(filterOptionValue, row))) {
return true continue
} else { } else {
return false return false
} }
@ -307,20 +301,13 @@ export default {
const columnsWithControlledFilter = this.normalizedColumns.filter(column => { const columnsWithControlledFilter = this.normalizedColumns.filter(column => {
return Array.isArray(column.filterOptionValues) return Array.isArray(column.filterOptionValues)
}) })
const keyOfColumnsToFilter = columnsWithControlledFilter.map(column => column.key) const controlledActiveFilters = {}
const controlledActiveFilters = []
columnsWithControlledFilter.forEach(column => { columnsWithControlledFilter.forEach(column => {
column.filterOptionValues.forEach(filterOptionValue => { controlledActiveFilters[column.key] = column.filterOptionValues
controlledActiveFilters.push({
columnKey: column.key,
filterOptionValue
})
})
}) })
const uncontrolledFilters = this.internalActiveFilters.filter(({ const activeFilters = Object.assign(
columnKey createShallowClonedObject(this.internalActiveFilters),
}) => !keyOfColumnsToFilter.includes(columnKey)) controlledActiveFilters)
const activeFilters = controlledActiveFilters.concat(uncontrolledFilters)
return activeFilters return activeFilters
}, },
syntheticActiveSorter () { syntheticActiveSorter () {
@ -380,7 +367,7 @@ export default {
this.$emit('change', { this.$emit('change', {
sorter: createShallowClonedObject(this.syntheticActiveSorter), sorter: createShallowClonedObject(this.syntheticActiveSorter),
pagination: createShallowClonedObject(this.syntheticPagination), pagination: createShallowClonedObject(this.syntheticPagination),
filters: createShallowClonedArray(this.syntheticActiveFilters) filters: createShallowClonedObject(this.syntheticActiveFilters)
}) })
this.$emit('page-size-change') this.$emit('page-size-change')
} }
@ -481,12 +468,7 @@ export default {
} }
} }
if (column.filter && Array.isArray(column.defaultFilterOptionValues)) { if (column.filter && Array.isArray(column.defaultFilterOptionValues)) {
column.defaultFilterOptionValues.forEach(filterOptionValue => { this.internalActiveFilters[column.key] = column.defaultFilterOptionValues
this.internalActiveFilters.push({
columnKey: column.key,
filterOptionValue
})
})
} }
}) })
this.internalCheckedRowKeys = this.defaultCheckedRowKeys this.internalCheckedRowKeys = this.defaultCheckedRowKeys
@ -502,16 +484,13 @@ export default {
}, },
changeFilters (filters, sourceColumn) { changeFilters (filters, sourceColumn) {
if (!filters) { if (!filters) {
this.internalActiveFilters = [] this.internalActiveFilters = {}
this.$emit('filters-change', [], createShallowClonedObject(sourceColumn)) this.$emit('filters-change', {}, createShallowClonedObject(sourceColumn))
} else if (isPlainObject(filters)) {
this.internalActiveFilters = filters
this.$emit('filters-change', createShallowClonedObject(filters), createShallowClonedObject(sourceColumn))
} else { } else {
if (Array.isArray(filters)) { console.error('[naive-ui/n-data-table]: filters is not an object')
this.internalActiveFilters = filters
this.$emit('filters-change', createShallowClonedArray(filters), createShallowClonedObject(sourceColumn))
} else {
this.internalActiveFilters = [ filters ]
this.$emit('filters-change', [ filters ], sourceColumn)
}
} }
}, },
scrollMainTableBodyToTop () { scrollMainTableBodyToTop () {
@ -599,7 +578,7 @@ export default {
this.clearFilters() this.clearFilters()
}, },
clearFilters () { clearFilters () {
this.filters([]) this.filters({})
}, },
filters (filters) { filters (filters) {
this.filter(filters) this.filter(filters)

View File

@ -40,22 +40,13 @@ import NDataTableFilterMenu from './FilterMenu'
import NPopover from '../../../Popover' import NPopover from '../../../Popover'
import funnel from '../../../_icons/funnel' import funnel from '../../../_icons/funnel'
function createFilterOptionValues (activeFilters, column) { function createActiveFilters (allFilters, columnKey, filterOptionValues) {
const activeFilterOptionValues = activeFilters if (!Array.isArray(filterOptionValues)) {
.filter(filter => filter.columnKey === column.key) filterOptionValues = [filterOptionValues]
.map(filter => filter.filterOptionValue)
return activeFilterOptionValues
}
function createActiveFilters (allFilters, columnKey, filters) {
allFilters = allFilters.filter(filter => filter.columnKey !== columnKey)
if (!Array.isArray(filters)) {
filters = [filters]
} }
return allFilters.concat(filters.map(filter => ({ const activeFilters = Object.assign({}, allFilters)
columnKey, activeFilters[columnKey] = filterOptionValues
filterOptionValue: filter return activeFilters
})))
} }
export default { export default {
@ -91,7 +82,7 @@ export default {
return this.NDataTable.syntheticActiveFilters return this.NDataTable.syntheticActiveFilters
}, },
activeFilterOptionValues () { activeFilterOptionValues () {
return createFilterOptionValues(this.activeFilters, this.column) return this.activeFilters[this.column.key]
}, },
active () { active () {
if (Array.isArray(this.activeFilterOptionValues)) { if (Array.isArray(this.activeFilterOptionValues)) {