From f1e62240e0d804ac03d8ca28616621f3c312f31f Mon Sep 17 00:00:00 2001
From: tjz <415800467@qq.com>
Date: Sat, 31 Mar 2018 21:11:02 +0800
Subject: [PATCH] add table demo
---
components/index.js | 5 +
components/style.js | 1 +
components/table/Table.jsx | 1500 ++++++++++++++-------------
components/table/createBodyRow.jsx | 2 +-
components/table/demo/basic.md | 59 ++
components/table/filterDropdown.jsx | 3 +-
components/table/interface.js | 5 +-
examples/routes.js | 2 +-
8 files changed, 824 insertions(+), 753 deletions(-)
create mode 100644 components/table/demo/basic.md
diff --git a/components/index.js b/components/index.js
index 5b19a3170..654b206c8 100644
--- a/components/index.js
+++ b/components/index.js
@@ -132,4 +132,9 @@ import DatePicker from './date-picker'
const { MonthPicker, RangePicker, WeekPicker } = DatePicker
export { DatePicker, MonthPicker, RangePicker, WeekPicker }
+import Table from './table'
+const { Column: TableColumn, ColumnGroup: TableColumnGroup } = Table
+
+export { Table, TableColumn, TableColumnGroup }
+
export { default as version } from './version'
diff --git a/components/style.js b/components/style.js
index 94578a116..032e7069f 100644
--- a/components/style.js
+++ b/components/style.js
@@ -34,3 +34,4 @@ import './steps/style'
import './breadcrumb/style'
import './calendar/style'
import './date-picker/style'
+import './table/style'
diff --git a/components/table/Table.jsx b/components/table/Table.jsx
index 0832a6245..d53f5dffc 100755
--- a/components/table/Table.jsx
+++ b/components/table/Table.jsx
@@ -1,4 +1,5 @@
+import cloneDeep from 'lodash/cloneDeep'
import VcTable from '../vc-table'
import classNames from 'classnames'
import Pagination from '../pagination'
@@ -16,6 +17,7 @@ import ColumnGroup from './ColumnGroup'
import createBodyRow from './createBodyRow'
import { flatArray, treeMap, flatFilter } from './util'
import { initDefaultProps, mergeProps, getOptionProps } from '../_util/props-util'
+import BaseMixin from '../_util/BaseMixin'
import {
TableProps,
} from './interface'
@@ -47,12 +49,12 @@ export default {
name: 'Table',
Column,
ColumnGroup,
-
+ mixins: [BaseMixin],
props: initDefaultProps(TableProps, {
dataSource: [],
prefixCls: 'ant-table',
useFixedHeader: false,
- rowSelection: null,
+ // rowSelection: null,
size: 'large',
loading: false,
bordered: false,
@@ -148,802 +150,804 @@ export default {
this.createComponents(val, preVal)
},
},
-
- getCheckboxPropsByItem (item, index) {
- const { rowSelection = {}} = this
- if (!rowSelection.getCheckboxProps) {
- return {}
- }
- const key = this.getRecordKey(item, index)
- // Cache checkboxProps
- if (!this.CheckboxPropsCache[key]) {
- this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item)
- }
- return this.CheckboxPropsCache[key]
- },
-
- getDefaultSelection () {
- const { rowSelection = {}} = this
- if (!rowSelection.getCheckboxProps) {
- return []
- }
- return this.getFlatData()
- .filter((item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
- .map((record, rowIndex) => this.getRecordKey(record, rowIndex))
- },
-
- getDefaultPagination (props) {
- const pagination = props.pagination || {}
- pagination.props = pagination.props || {}
- return this.hasPagination(props)
- ? mergeProps(
- defaultPagination,
- pagination,
- {
- props: {
- current: pagination.props.defaultCurrent || pagination.props.current || 1,
- pageSize: pagination.props.defaultPageSize || pagination.props.pageSize || 10,
- },
- }) : {}
- },
-
- onRow (record, index) {
- const { prefixCls, customRow } = this
- const custom = customRow ? customRow(record, index) : {}
- return mergeProps(custom, {
- props: {
- prefixCls,
- store: this.store,
- rowKey: this.getRecordKey(record, index),
- },
- })
- },
-
- setSelectedRowKeys (selectedRowKeys, { selectWay, record, checked, changeRowKeys }) {
- const { rowSelection = {}, $listeners: {
- rowSelectionChange,
- rowSelectionSelect,
- rowSelectionSelectAll,
- rowSelectionSelectInvert,
- }} = this
- if (rowSelection && !('selectedRowKeys' in rowSelection)) {
- this.store.setState({ selectedRowKeys })
- }
- const data = this.getFlatData()
- if (!rowSelectionChange && !rowSelection[selectWay]) {
- return
- }
- const selectedRows = data.filter(
- (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
- )
- this.$emit('rowSelectionChange', selectedRowKeys, selectedRows)
-
- if (selectWay === 'onSelect' && rowSelectionSelect) {
- this.$emit('rowSelectionSelect', record, checked, selectedRows)
- } else if (selectWay === 'onSelectAll' && rowSelectionSelectAll) {
- const changeRows = data.filter(
- (row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
- )
- this.$emit('rowSelectionSelectAll', checked, selectedRows, changeRows)
- } else if (selectWay === 'onSelectInvert' && rowSelectionSelectInvert) {
- this.$emit('rowSelectionSelectInvert', selectedRowKeys)
- }
- },
-
- hasPagination () {
- return this.pagination !== false
- },
-
- isFiltersChanged (filters) {
- let filtersChanged = false
- if (Object.keys(filters).length !== Object.keys(this.sFilters).length) {
- filtersChanged = true
- } else {
- Object.keys(filters).forEach(columnKey => {
- if (filters[columnKey] !== this.sFilters[columnKey]) {
- filtersChanged = true
- }
- })
- }
- return filtersChanged
- },
-
- getSortOrderColumns (columns) {
- return flatFilter(
- columns || this.columns || [],
- (column) => 'sortOrder' in column,
- )
- },
-
- getFilteredValueColumns (columns) {
- return flatFilter(
- columns || this.columns || [],
- (column) => typeof column.filteredValue !== 'undefined',
- )
- },
-
- getFiltersFromColumns (columns) {
- const filters = {}
- this.getFilteredValueColumns(columns).forEach((col) => {
- const colKey = this.getColumnKey(col)
- filters[colKey] = col.filteredValue
- })
- return filters
- },
-
- getDefaultSortOrder (columns) {
- const definedSortState = this.getSortStateFromColumns(columns)
-
- const defaultSortedColumn = flatFilter(columns || [], (column) => column.defaultSortOrder != null)[0]
-
- if (defaultSortedColumn && !definedSortState.sortColumn) {
- return {
- sSortColumn: defaultSortedColumn,
- sSortOrder: defaultSortedColumn.defaultSortOrder,
+ methods: {
+ getCheckboxPropsByItem (item, index) {
+ const { rowSelection = {}} = this
+ if (!rowSelection.getCheckboxProps) {
+ return {}
}
- }
-
- return definedSortState
- },
-
- getSortStateFromColumns (columns) {
- // return first column which sortOrder is not falsy
- const sortedColumn = this.getSortOrderColumns(columns).filter((col) => col.sortOrder)[0]
-
- if (sortedColumn) {
- return {
- sSortColumn: sortedColumn,
- sSortOrder: sortedColumn.sortOrder,
+ const key = this.getRecordKey(item, index)
+ // Cache checkboxProps
+ if (!this.CheckboxPropsCache[key]) {
+ this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item)
}
- }
+ return this.CheckboxPropsCache[key]
+ },
- return {
- sSortColumn: null,
- sSortOrder: null,
- }
- },
-
- getSorterFn () {
- const { sSortOrder: sortOrder, sSortColumn: sortColumn } = this
- if (!sortOrder || !sortColumn ||
- typeof sortColumn.sorter !== 'function') {
- return
- }
-
- return (a, b) => {
- const result = sortColumn.sorter(a, b)
- if (result !== 0) {
- return (sortOrder === 'descend') ? -result : result
+ getDefaultSelection () {
+ const { rowSelection = {}} = this
+ if (!rowSelection.getCheckboxProps) {
+ return []
}
- return 0
- }
- },
+ return this.getFlatData()
+ .filter((item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
+ .map((record, rowIndex) => this.getRecordKey(record, rowIndex))
+ },
- toggleSortOrder (order, column) {
- let { sSortOrder: sortOrder, sSortColumn: sortColumn } = this
- // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
- const isSortColumn = this.isSortColumn(column)
- if (!isSortColumn) { // 当前列未排序
- sortOrder = order
- sortColumn = column
- } else { // 当前列已排序
- if (sortOrder === order) { // 切换为未排序状态
- sortOrder = ''
- sortColumn = null
- } else { // 切换为排序状态
- sortOrder = order
- }
- }
- const newState = {
- sSortOrder: sortOrder,
- sSortColumn: sortColumn,
- }
-
- // Controlled
- if (this.getSortOrderColumns().length === 0) {
- this.setState(newState)
- }
- this.$emit('change', this.prepareParamsArguments({
- ...this.$data,
- ...newState,
- }))
- },
-
- handleFilter (column, nextFilters) {
- const props = this.$props
- const pagination = { ...this.sPagination }
- const filters = {
- ...this.sFilters,
- [this.getColumnKey(column)]: nextFilters,
- }
- // Remove filters not in current columns
- const currentColumnKeys = []
- treeMap(this.columns, c => {
- if (!c.children) {
- currentColumnKeys.push(this.getColumnKey(c))
- }
- })
- Object.keys(filters).forEach((columnKey) => {
- if (currentColumnKeys.indexOf(columnKey) < 0) {
- delete filters[columnKey]
- }
- })
-
- if (props.pagination) {
- // Reset current prop
+ getDefaultPagination (props) {
+ const pagination = props.pagination || {}
pagination.props = pagination.props || {}
- pagination.props.current = 1
- pagination.on.change(pagination.current)
- }
+ return this.hasPagination(props)
+ ? mergeProps(
+ defaultPagination,
+ pagination,
+ {
+ props: {
+ current: pagination.props.defaultCurrent || pagination.props.current || 1,
+ pageSize: pagination.props.defaultPageSize || pagination.props.pageSize || 10,
+ },
+ }) : {}
+ },
- const newState = {
- sPagination: pagination,
- sFilters: {},
- }
- const filtersToSetState = { ...filters }
- // Remove filters which is controlled
- this.getFilteredValueColumns().forEach((col) => {
- const columnKey = this.getColumnKey(col)
- if (columnKey) {
- delete filtersToSetState[columnKey]
- }
- })
- if (Object.keys(filtersToSetState).length > 0) {
- newState.sFilters = filtersToSetState
- }
-
- // Controlled current prop will not respond user interaction
- if (typeof props.pagination === 'object' && props.pagination.props && 'current' in (props.pagination.props)) {
- newState.sPagination = mergeProps(pagination, {
+ onRow (record, index) {
+ const { prefixCls, customRow } = this
+ const custom = customRow ? customRow(record, index) : {}
+ return mergeProps(custom, {
props: {
- current: this.sPagination.props.current,
+ prefixCls,
+ store: this.store,
+ rowKey: this.getRecordKey(record, index),
},
})
- }
+ },
+
+ setSelectedRowKeys (selectedRowKeys, { selectWay, record, checked, changeRowKeys }) {
+ const { rowSelection = {}, $listeners: {
+ rowSelectionChange,
+ rowSelectionSelect,
+ rowSelectionSelectAll,
+ rowSelectionSelectInvert,
+ }} = this
+ if (rowSelection && !('selectedRowKeys' in rowSelection)) {
+ this.store.setState({ selectedRowKeys })
+ }
+ const data = this.getFlatData()
+ if (!rowSelectionChange && !rowSelection[selectWay]) {
+ return
+ }
+ const selectedRows = data.filter(
+ (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
+ )
+ this.$emit('rowSelectionChange', selectedRowKeys, selectedRows)
+
+ if (selectWay === 'onSelect' && rowSelectionSelect) {
+ this.$emit('rowSelectionSelect', record, checked, selectedRows)
+ } else if (selectWay === 'onSelectAll' && rowSelectionSelectAll) {
+ const changeRows = data.filter(
+ (row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
+ )
+ this.$emit('rowSelectionSelectAll', checked, selectedRows, changeRows)
+ } else if (selectWay === 'onSelectInvert' && rowSelectionSelectInvert) {
+ this.$emit('rowSelectionSelectInvert', selectedRowKeys)
+ }
+ },
+
+ hasPagination () {
+ return this.pagination !== false
+ },
+
+ isFiltersChanged (filters) {
+ let filtersChanged = false
+ if (Object.keys(filters).length !== Object.keys(this.sFilters).length) {
+ filtersChanged = true
+ } else {
+ Object.keys(filters).forEach(columnKey => {
+ if (filters[columnKey] !== this.sFilters[columnKey]) {
+ filtersChanged = true
+ }
+ })
+ }
+ return filtersChanged
+ },
+
+ getSortOrderColumns (columns) {
+ return flatFilter(
+ columns || this.columns || [],
+ (column) => 'sortOrder' in column,
+ )
+ },
+
+ getFilteredValueColumns (columns) {
+ return flatFilter(
+ columns || this.columns || [],
+ (column) => typeof column.filteredValue !== 'undefined',
+ )
+ },
+
+ getFiltersFromColumns (columns) {
+ const filters = {}
+ this.getFilteredValueColumns(columns).forEach((col) => {
+ const colKey = this.getColumnKey(col)
+ filters[colKey] = col.filteredValue
+ })
+ return filters
+ },
+
+ getDefaultSortOrder (columns) {
+ const definedSortState = this.getSortStateFromColumns(columns)
+
+ const defaultSortedColumn = flatFilter(columns || [], (column) => column.defaultSortOrder != null)[0]
+
+ if (defaultSortedColumn && !definedSortState.sortColumn) {
+ return {
+ sSortColumn: defaultSortedColumn,
+ sSortOrder: defaultSortedColumn.defaultSortOrder,
+ }
+ }
+
+ return definedSortState
+ },
+
+ getSortStateFromColumns (columns) {
+ // return first column which sortOrder is not falsy
+ const sortedColumn = this.getSortOrderColumns(columns).filter((col) => col.sortOrder)[0]
+
+ if (sortedColumn) {
+ return {
+ sSortColumn: sortedColumn,
+ sSortOrder: sortedColumn.sortOrder,
+ }
+ }
+
+ return {
+ sSortColumn: null,
+ sSortOrder: null,
+ }
+ },
+
+ getSorterFn () {
+ const { sSortOrder: sortOrder, sSortColumn: sortColumn } = this
+ if (!sortOrder || !sortColumn ||
+ typeof sortColumn.sorter !== 'function') {
+ return
+ }
+
+ return (a, b) => {
+ const result = sortColumn.sorter(a, b)
+ if (result !== 0) {
+ return (sortOrder === 'descend') ? -result : result
+ }
+ return 0
+ }
+ },
+
+ toggleSortOrder (order, column) {
+ let { sSortOrder: sortOrder, sSortColumn: sortColumn } = this
+ // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
+ const isSortColumn = this.isSortColumn(column)
+ if (!isSortColumn) { // 当前列未排序
+ sortOrder = order
+ sortColumn = column
+ } else { // 当前列已排序
+ if (sortOrder === order) { // 切换为未排序状态
+ sortOrder = ''
+ sortColumn = null
+ } else { // 切换为排序状态
+ sortOrder = order
+ }
+ }
+ const newState = {
+ sSortOrder: sortOrder,
+ sSortColumn: sortColumn,
+ }
+
+ // Controlled
+ if (this.getSortOrderColumns().length === 0) {
+ this.setState(newState)
+ }
+ this.$emit('change', this.prepareParamsArguments({
+ ...this.$data,
+ ...newState,
+ }))
+ },
+
+ handleFilter (column, nextFilters) {
+ const props = this.$props
+ const pagination = { ...this.sPagination }
+ const filters = {
+ ...this.sFilters,
+ [this.getColumnKey(column)]: nextFilters,
+ }
+ // Remove filters not in current columns
+ const currentColumnKeys = []
+ treeMap(this.columns, c => {
+ if (!c.children) {
+ currentColumnKeys.push(this.getColumnKey(c))
+ }
+ })
+ Object.keys(filters).forEach((columnKey) => {
+ if (currentColumnKeys.indexOf(columnKey) < 0) {
+ delete filters[columnKey]
+ }
+ })
+
+ if (props.pagination) {
+ // Reset current prop
+ pagination.props = pagination.props || {}
+ pagination.props.current = 1
+ pagination.on.change(pagination.current)
+ }
+
+ const newState = {
+ sPagination: pagination,
+ sFilters: {},
+ }
+ const filtersToSetState = { ...filters }
+ // Remove filters which is controlled
+ this.getFilteredValueColumns().forEach((col) => {
+ const columnKey = this.getColumnKey(col)
+ if (columnKey) {
+ delete filtersToSetState[columnKey]
+ }
+ })
+ if (Object.keys(filtersToSetState).length > 0) {
+ newState.sFilters = filtersToSetState
+ }
+
+ // Controlled current prop will not respond user interaction
+ if (typeof props.pagination === 'object' && props.pagination.props && 'current' in (props.pagination.props)) {
+ newState.sPagination = mergeProps(pagination, {
+ props: {
+ current: this.sPagination.props.current,
+ },
+ })
+ }
+
+ this.setState(newState, () => {
+ this.store.setState({
+ selectionDirty: false,
+ })
+ this.$emit('change', this.prepareParamsArguments({
+ ...this.$data,
+ selectionDirty: false,
+ filters,
+ pagination,
+ }))
+ })
+ },
+
+ handleSelect (record, rowIndex, e) {
+ const checked = e.target.checked
+ const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection()
+ let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection)
+ const key = this.getRecordKey(record, rowIndex)
+ if (checked) {
+ selectedRowKeys.push(this.getRecordKey(record, rowIndex))
+ } else {
+ selectedRowKeys = selectedRowKeys.filter((i) => key !== i)
+ }
+ this.store.setState({
+ selectionDirty: true,
+ })
+ this.setSelectedRowKeys(selectedRowKeys, {
+ selectWay: 'onSelect',
+ record,
+ checked,
+ })
+ },
+
+ handleRadioSelect (record, rowIndex, e) {
+ const checked = e.target.checked
+ const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection()
+ let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection)
+ const key = this.getRecordKey(record, rowIndex)
+ selectedRowKeys = [key]
+ this.store.setState({
+ selectionDirty: true,
+ })
+ this.setSelectedRowKeys(selectedRowKeys, {
+ selectWay: 'onSelect',
+ record,
+ checked,
+ })
+ },
+
+ handleSelectRow (selectionKey, index, onSelectFunc) {
+ const data = this.getFlatCurrentPageData()
+ const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection()
+ const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection)
+ const changeableRowKeys = data
+ .filter((item, i) => !this.getCheckboxPropsByItem(item, i).disabled)
+ .map((item, i) => this.getRecordKey(item, i))
+
+ const changeRowKeys = []
+ let selectWay = ''
+ let checked
+ // handle default selection
+ switch (selectionKey) {
+ case 'all':
+ changeableRowKeys.forEach(key => {
+ if (selectedRowKeys.indexOf(key) < 0) {
+ selectedRowKeys.push(key)
+ changeRowKeys.push(key)
+ }
+ })
+ selectWay = 'onSelectAll'
+ checked = true
+ break
+ case 'removeAll':
+ changeableRowKeys.forEach(key => {
+ if (selectedRowKeys.indexOf(key) >= 0) {
+ selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1)
+ changeRowKeys.push(key)
+ }
+ })
+ selectWay = 'onSelectAll'
+ checked = false
+ break
+ case 'invert':
+ changeableRowKeys.forEach(key => {
+ if (selectedRowKeys.indexOf(key) < 0) {
+ selectedRowKeys.push(key)
+ } else {
+ selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1)
+ }
+ changeRowKeys.push(key)
+ selectWay = 'onSelectInvert'
+ })
+ break
+ default:
+ break
+ }
+
+ this.store.setState({
+ selectionDirty: true,
+ })
+ // when select custom selection, callback selections[n].onSelect
+ const { rowSelection } = this
+ let customSelectionStartIndex = 2
+ if (rowSelection && rowSelection.hideDefaultSelections) {
+ customSelectionStartIndex = 0
+ }
+ if (index >= customSelectionStartIndex && typeof onSelectFunc === 'function') {
+ return onSelectFunc(changeableRowKeys)
+ }
+ this.setSelectedRowKeys(selectedRowKeys, {
+ selectWay: selectWay,
+ checked,
+ changeRowKeys,
+ })
+ },
+
+ handlePageChange (current, ...otherArguments) {
+ const props = this.$props
+ const pagination = { ...this.sPagination }
+ if (current) {
+ pagination.props.current = current
+ } else {
+ pagination.props.current = pagination.props.current || 1
+ }
+ pagination.on.change(pagination.props.current, ...otherArguments)
+
+ const newState = {
+ sPagination: pagination,
+ }
+ // Controlled current prop will not respond user interaction
+ if (props.pagination &&
+ typeof props.pagination === 'object' &&
+ props.pagination.props &&
+ 'current' in (props.pagination.props)) {
+ newState.sPagination = mergeProps(pagination, {
+ props: {
+ current: this.sPagination.props.current,
+ },
+ })
+ }
+ this.setState(newState)
- this.setState(newState, () => {
this.store.setState({
selectionDirty: false,
})
this.$emit('change', this.prepareParamsArguments({
...this.$data,
selectionDirty: false,
- filters,
pagination,
}))
- })
- },
+ },
- handleSelect (record, rowIndex, e) {
- const checked = e.target.checked
- const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection()
- let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection)
- const key = this.getRecordKey(record, rowIndex)
- if (checked) {
- selectedRowKeys.push(this.getRecordKey(record, rowIndex))
- } else {
- selectedRowKeys = selectedRowKeys.filter((i) => key !== i)
- }
- this.store.setState({
- selectionDirty: true,
- })
- this.setSelectedRowKeys(selectedRowKeys, {
- selectWay: 'onSelect',
- record,
- checked,
- })
- },
+ renderSelectionBox (type) {
+ return (_, record, index) => {
+ const rowIndex = this.getRecordKey(record, index) // 从 1 开始
+ const props = this.getCheckboxPropsByItem(record, index)
+ const handleChange = (e) => {
+ type === 'radio' ? this.handleRadioSelect(record, rowIndex, e)
+ : this.handleSelect(record, rowIndex, e)
+ }
+ const selectionBoxProps = mergeProps({
+ props: {
+ type,
+ store: this.store,
+ rowIndex,
+ defaultSelection: this.getDefaultSelection(),
+ },
+ on: {
+ change: handleChange,
+ },
+ }, props)
- handleRadioSelect (record, rowIndex, e) {
- const checked = e.target.checked
- const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection()
- let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection)
- const key = this.getRecordKey(record, rowIndex)
- selectedRowKeys = [key]
- this.store.setState({
- selectionDirty: true,
- })
- this.setSelectedRowKeys(selectedRowKeys, {
- selectWay: 'onSelect',
- record,
- checked,
- })
- },
+ return (
+
+