ant-design-vue/components/table/index.jsx

110 lines
3.2 KiB
Vue
Raw Normal View History

2018-04-01 04:52:27 +00:00
import T from './Table'
2018-03-29 14:08:04 +00:00
2018-04-01 04:52:27 +00:00
import { getOptionProps, getKey, getClass,
getStyle, getEvents, getSlotOptions, camelize, getSlots,
} from '../_util/props-util'
2018-04-01 08:50:31 +00:00
2018-04-01 04:52:27 +00:00
const Table = {
2018-04-08 13:17:20 +00:00
name: 'ATable',
2018-04-01 04:52:27 +00:00
Column: T.Column,
ColumnGroup: T.ColumnGroup,
props: T.props,
methods: {
normalize (elements = []) {
const columns = []
elements.forEach(element => {
if (!element.tag) {
return
}
const key = getKey(element)
const style = getStyle(element)
const cls = getClass(element)
const props = getOptionProps(element)
const events = getEvents(element)
const listeners = {}
Object.keys(events).forEach(e => {
2018-04-01 14:33:01 +00:00
const k = `on-${e}`
2018-04-01 04:52:27 +00:00
listeners[camelize(k)] = events[e]
})
2018-04-01 14:33:01 +00:00
const { default: children, ...restSlots } = getSlots(element)
const column = { ...restSlots, ...props, style, class: cls, ...listeners }
2018-04-01 04:52:27 +00:00
if (key) {
column.key = key
}
if (getSlotOptions(element).__ANT_TABLE_COLUMN_GROUP) {
column.children = this.normalize(children)
} else {
const customRender = element.data && element.data.scopedSlots && element.data.scopedSlots.default
column.customRender = column.customRender || customRender
}
columns.push(column)
})
return columns
},
updateColumns (cols = []) {
const columns = []
const { $slots, $scopedSlots } = this
cols.forEach(col => {
2018-04-02 01:48:03 +00:00
const { slots = {}, scopedSlots = {}, ...restProps } = col
2018-04-01 04:52:27 +00:00
const column = {
...restProps,
}
2018-04-02 01:48:03 +00:00
Object.keys(slots).forEach(key => {
const name = slots[key]
if (column[key] === undefined && $slots[name]) {
column[key] = $slots[name]
}
})
Object.keys(scopedSlots).forEach(key => {
const name = scopedSlots[key]
if (column[key] === undefined && $scopedSlots[name]) {
column[key] = $scopedSlots[name]
}
})
// if (slotScopeName && $scopedSlots[slotScopeName]) {
// column.customRender = column.customRender || $scopedSlots[slotScopeName]
// }
2018-04-01 04:52:27 +00:00
if (col.children) {
column.children = this.updateColumns(column.children)
}
columns.push(column)
})
return columns
},
},
render () {
2018-04-01 08:50:31 +00:00
const { $listeners, $slots, normalize, $scopedSlots } = this
2018-04-01 04:52:27 +00:00
const props = getOptionProps(this)
const columns = props.columns ? this.updateColumns(props.columns) : normalize($slots.default)
2018-04-01 08:50:31 +00:00
let { title, footer } = props
2018-04-02 14:13:11 +00:00
const {
title: slotTitle,
footer: slotFooter,
expandedRowRender = props.expandedRowRender,
} = $scopedSlots
2018-04-01 08:50:31 +00:00
title = title || slotTitle
footer = footer || slotFooter
2018-04-01 04:52:27 +00:00
const tProps = {
props: {
...props,
columns,
2018-04-01 08:50:31 +00:00
title,
footer,
2018-04-02 14:13:11 +00:00
expandedRowRender,
2018-04-01 04:52:27 +00:00
},
on: $listeners,
}
return (
<T {...tProps}/>
)
},
}
/* istanbul ignore next */
Table.install = function (Vue) {
Vue.component(Table.name, Table)
Vue.component(Table.Column.name, Table.Column)
Vue.component(Table.ColumnGroup.name, Table.ColumnGroup)
}
2018-03-29 14:08:04 +00:00
export default Table