update vc-table

pull/9/head
tjz 2018-03-26 23:05:29 +08:00
parent d508fb1fbd
commit 29eceaf8f1
10 changed files with 142 additions and 63 deletions

View File

@ -29,21 +29,15 @@ export default {
}, },
onExpandedRowsChange (rows) { onExpandedRowsChange (rows) {
this.setState({ this.expandedRowKeys = rows
expandedRowKeys: rows,
})
}, },
onExpandIconAsCellChange (e) { onExpandIconAsCellChange (e) {
this.setState({ this.expandIconAsCell = e.target.checked
expandIconAsCell: e.target.checked,
})
}, },
onExpandRowByClickChange (e) { onExpandRowByClickChange (e) {
this.setState({ this.expandRowByClick = e.target.checked
expandRowByClick: e.target.checked,
})
}, },
toggleButton () { toggleButton () {
@ -58,7 +52,7 @@ export default {
remove (index) { remove (index) {
const data = this.data const data = this.data
data.splice(index, 1) data.splice(index, 1)
this.setState({ data }) this.data = data
}, },
expandedRowRender (record) { expandedRowRender (record) {

View File

@ -0,0 +1,53 @@
/* eslint-disable no-console,func-names,react/no-multi-comp */
import Table from '../index'
import '../assets/index.less'
const columns = [
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100, fixed: 'left' },
{ title: 'title3', dataIndex: 'c', key: 'c' },
{ title: 'title4', dataIndex: 'b', key: 'd' },
{ title: 'title5', dataIndex: 'b', key: 'e' },
{ title: 'title6', dataIndex: 'b', key: 'f',
render: (h) => <div style={{ height: '40px', lineHeight: '40px' }}>我很高</div> },
{ title: 'title7', dataIndex: 'b', key: 'g' },
{ title: 'title8', dataIndex: 'b', key: 'h' },
{ title: 'title9', dataIndex: 'b', key: 'i' },
{ title: 'title10', dataIndex: 'b', key: 'j' },
{ title: 'title11', dataIndex: 'b', key: 'k' },
{ title: 'title12', dataIndex: 'b', key: 'l', width: 100, fixed: 'right' },
]
const data = [
{ a: '123', b: 'xxxxxxxx', d: 3, key: '1', title: 'hello' },
{ a: 'cdd', b: 'edd12221', d: 3, key: '2', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '3', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '4', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '5', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '6', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '7', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '8', title: 'hello' },
{ a: '133', c: 'edd12221', d: 2, key: '9', title: 'hello' },
]
export default {
data () {
return {
show: false,
}
},
render () {
return (
<div style={{ width: '800px' }}>
<h2>Fixed columns</h2>
<button onClick={() => { this.show = true }}>show Table</button>
{ this.show ? <Table
columns={columns}
expandedRowRender={record => record.title}
expandIconAsCell
scroll={{ x: 1200 }}
data={data}
/> : null}
</div>
)
},
}

View File

@ -28,7 +28,7 @@ const BaseTable = {
}, },
methods: { methods: {
handleRowHover (isHover, key) { handleRowHover (isHover, key) {
this.props.store.setState({ this.store.setState({
currentHoverKey: isHover ? key : null, currentHoverKey: isHover ? key : null,
}) })
}, },
@ -159,7 +159,7 @@ const BaseTable = {
if (scroll.x === true) { if (scroll.x === true) {
tableStyle.tableLayout = 'fixed' tableStyle.tableLayout = 'fixed'
} else { } else {
tableStyle.width = scroll.x tableStyle.width = `${scroll.x}px`
} }
} }

View File

@ -19,6 +19,21 @@ export default {
inject: { inject: {
table: { default: {}}, table: { default: {}},
}, },
mounted () {
this.updateTableRef()
},
updated () {
this.updateTableRef()
},
methods: {
updateTableRef () {
this.$nextTick(() => {
this.$refs.fixedColumnsBodyLeft && this.table.saveChildrenRef('fixedColumnsBodyLeft', this.$refs.fixedColumnsBodyLeft)
this.$refs.fixedColumnsBodyRight && this.table.saveChildrenRef('fixedColumnsBodyRight', this.$refs.fixedColumnsBodyLeft)
this.$refs.bodyTable && this.table.saveChildrenRef('bodyTable', this.$refs.bodyTable)
})
},
},
render () { render () {
const { prefixCls, scroll } = this.table const { prefixCls, scroll } = this.table
const { const {

View File

@ -36,9 +36,9 @@ const ExpandableRow = {
methods: { methods: {
hasExpandIcon (columnIndex) { hasExpandIcon (columnIndex) {
const { expandRowByClick } = this const { expandRowByClick } = this
return !this.expandIconAsCell && return !this.tempExpandIconAsCell &&
!expandRowByClick && !expandRowByClick &&
columnIndex === this.expandIconColumnIndex columnIndex === this.tempExpandIconColumnIndex
}, },
handleExpandChange (record, event) { handleExpandChange (record, event) {
@ -75,7 +75,7 @@ const ExpandableRow = {
}, },
renderExpandIconCell (cells) { renderExpandIconCell (cells) {
if (!this.expandIconAsCell) { if (!this.tempExpandIconAsCell) {
return return
} }
const { prefixCls } = this const { prefixCls } = this
@ -101,8 +101,8 @@ const ExpandableRow = {
$scopedSlots, $scopedSlots,
} = this } = this
this.expandIconAsCell = fixed !== 'right' ? this.expandIconAsCell : false this.tempExpandIconAsCell = fixed !== 'right' ? this.expandIconAsCell : false
this.expandIconColumnIndex = fixed !== 'right' ? this.expandIconColumnIndex : -1 this.tempExpandIconColumnIndex = fixed !== 'right' ? this.expandIconColumnIndex : -1
const childrenData = record[childrenColumnName] const childrenData = record[childrenColumnName]
this.expandable = !!(childrenData || expandedRowRender) this.expandable = !!(childrenData || expandedRowRender)
const expandableRowProps = { const expandableRowProps = {

View File

@ -7,6 +7,7 @@ import { initDefaultProps, getOptionProps } from '../../_util/props-util'
export const ExpandableTableProps = () => ({ export const ExpandableTableProps = () => ({
expandIconAsCell: PropTypes.bool, expandIconAsCell: PropTypes.bool,
expandRowByClick: PropTypes.bool,
expandedRowKeys: PropTypes.array, expandedRowKeys: PropTypes.array,
expandedRowClassName: PropTypes.func, expandedRowClassName: PropTypes.func,
defaultExpandAllRows: PropTypes.bool, defaultExpandAllRows: PropTypes.bool,

View File

@ -17,6 +17,19 @@ export default {
inject: { inject: {
table: { default: {}}, table: { default: {}},
}, },
mounted () {
this.updateTableRef()
},
updated () {
this.updateTableRef()
},
methods: {
updateTableRef () {
this.$nextTick(() => {
this.$refs.headTable && this.table.saveChildrenRef('headTable', this.$refs.headTable)
})
},
},
render () { render () {
const { columns, fixed, tableClassName, handleBodyScrollLeft, expander, table } = this const { columns, fixed, tableClassName, handleBodyScrollLeft, expander, table } = this
const { prefixCls, scroll, showHeader } = table const { prefixCls, scroll, showHeader } = table

View File

@ -61,6 +61,7 @@ export default {
expandedRowRender: PropTypes.func, expandedRowRender: PropTypes.func,
childrenColumnName: PropTypes.string, childrenColumnName: PropTypes.string,
indentSize: PropTypes.number, indentSize: PropTypes.number,
expandRowByClick: PropTypes.bool,
}, { }, {
data: [], data: [],
useFixedHeader: false, useFixedHeader: false,
@ -217,15 +218,15 @@ export default {
setScrollPosition (position) { setScrollPosition (position) {
this.scrollPosition = position this.scrollPosition = position
if (this.tableNode) { if (this.$refs.tableNode) {
const { prefixCls } = this const { prefixCls } = this
if (position === 'both') { if (position === 'both') {
classes(this.tableNode) classes(this.$refs.tableNode)
.remove(new RegExp(`^${prefixCls}-scroll-position-.+$`)) .remove(new RegExp(`^${prefixCls}-scroll-position-.+$`))
.add(`${prefixCls}-scroll-position-left`) .add(`${prefixCls}-scroll-position-left`)
.add(`${prefixCls}-scroll-position-right`) .add(`${prefixCls}-scroll-position-right`)
} else { } else {
classes(this.tableNode) classes(this.$refs.tableNode)
.remove(new RegExp(`^${prefixCls}-scroll-position-.+$`)) .remove(new RegExp(`^${prefixCls}-scroll-position-.+$`))
.add(`${prefixCls}-scroll-position-${position}`) .add(`${prefixCls}-scroll-position-${position}`)
} }
@ -233,7 +234,7 @@ export default {
}, },
setScrollPositionClassName () { setScrollPositionClassName () {
const node = this.bodyTable const node = this.ref_bodyTable
const scrollToLeft = node.scrollLeft === 0 const scrollToLeft = node.scrollLeft === 0
const scrollToRight = node.scrollLeft + 1 >= const scrollToRight = node.scrollLeft + 1 >=
node.children[0].getBoundingClientRect().width - node.children[0].getBoundingClientRect().width -
@ -255,17 +256,17 @@ export default {
}, },
syncFixedTableRowHeight () { syncFixedTableRowHeight () {
const tableRect = this.tableNode.getBoundingClientRect() const tableRect = this.$refs.tableNode.getBoundingClientRect()
// If tableNode's height less than 0, suppose it is hidden and don't recalculate rowHeight. // If tableNode's height less than 0, suppose it is hidden and don't recalculate rowHeight.
// see: https://github.com/ant-design/ant-design/issues/4836 // see: https://github.com/ant-design/ant-design/issues/4836
if (tableRect.height !== undefined && tableRect.height <= 0) { if (tableRect.height !== undefined && tableRect.height <= 0) {
return return
} }
const { prefixCls } = this.props const { prefixCls } = this
const headRows = this.headTable const headRows = this.ref_headTable
? this.headTable.querySelectorAll('thead') ? this.ref_headTable.querySelectorAll('thead')
: this.bodyTable.querySelectorAll('thead') : this.ref_bodyTable.querySelectorAll('thead')
const bodyRows = this.bodyTable.querySelectorAll(`.${prefixCls}-row`) || [] const bodyRows = this.ref_bodyTable.querySelectorAll(`.${prefixCls}-row`) || []
const fixedColumnsHeadRowsHeight = [].map.call( const fixedColumnsHeadRowsHeight = [].map.call(
headRows, row => row.getBoundingClientRect().height || 'auto' headRows, row => row.getBoundingClientRect().height || 'auto'
) )
@ -277,7 +278,7 @@ export default {
shallowequal(state.fixedColumnsBodyRowsHeight, fixedColumnsBodyRowsHeight)) { shallowequal(state.fixedColumnsBodyRowsHeight, fixedColumnsBodyRowsHeight)) {
return return
} }
console.log(fixedColumnsHeadRowsHeight, fixedColumnsBodyRowsHeight)
this.store.setState({ this.store.setState({
fixedColumnsHeadRowsHeight, fixedColumnsHeadRowsHeight,
fixedColumnsBodyRowsHeight, fixedColumnsBodyRowsHeight,
@ -285,11 +286,11 @@ export default {
}, },
resetScrollX () { resetScrollX () {
if (this.headTable) { if (this.ref_headTable) {
this.headTable.scrollLeft = 0 this.ref_headTable.scrollLeft = 0
} }
if (this.bodyTable) { if (this.ref_bodyTable) {
this.bodyTable.scrollLeft = 0 this.ref_bodyTable.scrollLeft = 0
} }
}, },
@ -305,12 +306,12 @@ export default {
} }
const target = e.target const target = e.target
const { scroll = {}} = this const { scroll = {}} = this
const { headTable, bodyTable } = this const { ref_headTable, ref_bodyTable } = this
if (target.scrollLeft !== this.lastScrollLeft && scroll.x) { if (target.scrollLeft !== this.lastScrollLeft && scroll.x) {
if (target === bodyTable && headTable) { if (target === ref_bodyTable && ref_headTable) {
headTable.scrollLeft = target.scrollLeft ref_headTable.scrollLeft = target.scrollLeft
} else if (target === headTable && bodyTable) { } else if (target === ref_headTable && ref_bodyTable) {
bodyTable.scrollLeft = target.scrollLeft ref_bodyTable.scrollLeft = target.scrollLeft
} }
this.setScrollPositionClassName() this.setScrollPositionClassName()
} }
@ -321,8 +322,8 @@ export default {
handleBodyScrollTop (e) { handleBodyScrollTop (e) {
const target = e.target const target = e.target
const { scroll = {}} = this const { scroll = {}} = this
const { headTable, bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this const { ref_headTable, ref_bodyTable, fixedColumnsBodyLeft, fixedColumnsBodyRight } = this
if (target.scrollTop !== this.lastScrollTop && scroll.y && target !== headTable) { if (target.scrollTop !== this.lastScrollTop && scroll.y && target !== ref_headTable) {
const scrollTop = target.scrollTop const scrollTop = target.scrollTop
if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) { if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
fixedColumnsBodyLeft.scrollTop = scrollTop fixedColumnsBodyLeft.scrollTop = scrollTop
@ -330,8 +331,8 @@ export default {
if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) { if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) {
fixedColumnsBodyRight.scrollTop = scrollTop fixedColumnsBodyRight.scrollTop = scrollTop
} }
if (bodyTable && target !== bodyTable) { if (ref_bodyTable && target !== ref_bodyTable) {
bodyTable.scrollTop = scrollTop ref_bodyTable.scrollTop = scrollTop
} }
} }
// Remember last scrollTop for scroll direction detecting. // Remember last scrollTop for scroll direction detecting.
@ -342,7 +343,9 @@ export default {
this.handleBodyScrollLeft(e) this.handleBodyScrollLeft(e)
this.handleBodyScrollTop(e) this.handleBodyScrollTop(e)
}, },
saveChildrenRef (name, node) {
this[`ref_${name}`] = node
},
renderMainTable () { renderMainTable () {
const { scroll, prefixCls } = this const { scroll, prefixCls } = this
const isAnyColumnsFixed = this.columnManager.isAnyColumnsFixed() const isAnyColumnsFixed = this.columnManager.isAnyColumnsFixed()
@ -420,19 +423,19 @@ export default {
}, },
renderTitle () { renderTitle () {
const { title, prefixCls } = this const { title, prefixCls, data } = this
return title ? ( return title ? (
<div class={`${prefixCls}-title`} key='title'> <div class={`${prefixCls}-title`} key='title'>
{title(this.props.data)} {title(data)}
</div> </div>
) : null ) : null
}, },
renderFooter () { renderFooter () {
const { footer, prefixCls } = this const { footer, prefixCls, data } = this
return footer ? ( return footer ? (
<div class={`${prefixCls}-footer`} key='footer'> <div class={`${prefixCls}-footer`} key='footer'>
{footer(this.props.data)} {footer(data)}
</div> </div>
) : null ) : null
}, },

View File

@ -2,7 +2,7 @@ import PropTypes from '../../_util/vue-types'
import { connect } from '../../_util/store' import { connect } from '../../_util/store'
import TableCell from './TableCell' import TableCell from './TableCell'
import { warningOnce } from './utils' import { warningOnce } from './utils'
import { initDefaultProps, mergeProps } from '../../_util/props-util' import { initDefaultProps, mergeProps, getStyle } from '../../_util/props-util'
import BaseMixin from '../../_util/BaseMixin' import BaseMixin from '../../_util/BaseMixin'
function noop () {} function noop () {}
const TableRow = { const TableRow = {
@ -78,15 +78,15 @@ const TableRow = {
}, },
}, },
componentWillReceiveProps (nextProps) { // componentWillReceiveProps (nextProps) {
if (this.props.visible || (!this.props.visible && nextProps.visible)) { // if (this.props.visible || (!this.props.visible && nextProps.visible)) {
this.shouldRender = true // this.shouldRender = true
} // }
}, // },
shouldComponentUpdate (nextProps) { // shouldComponentUpdate (nextProps) {
return !!(this.props.visible || nextProps.visible) // return !!(this.props.visible || nextProps.visible)
}, // },
updated () { updated () {
if (this.shouldRender && !this.rowRef) { if (this.shouldRender && !this.rowRef) {
@ -144,16 +144,16 @@ const TableRow = {
getStyle () { getStyle () {
const { height, visible } = this const { height, visible } = this
let style = getStyle(this)
if (height && height !== this.style.height) { if (height) {
this.style = { ...this.style, height } style = { ...style, height }
} }
if (!visible && !this.style.display) { if (!visible && !style.display) {
this.style = { ...this.style, display: 'none' } style = { ...style, display: 'none' }
} }
return this.style return style
}, },
saveRowRef () { saveRowRef () {
@ -239,7 +239,7 @@ const TableRow = {
const rowProps = onRow(record, index) const rowProps = onRow(record, index)
const customStyle = rowProps ? rowProps.style : {} const customStyle = rowProps ? rowProps.style : {}
let style = { height } let style = { height: typeof height === 'number' ? `${height}px` : height }
if (!visible) { if (!visible) {
style.display = 'none' style.display = 'none'

View File

@ -3,7 +3,7 @@ const AsyncComp = () => {
const hashs = window.location.hash.split('/') const hashs = window.location.hash.split('/')
const d = hashs[hashs.length - 1] const d = hashs[hashs.length - 1]
return { return {
component: import(`../components/vc-slider/demo/${d}`), component: import(`../components/vc-table/demo/${d}`),
} }
} }
export default [ export default [