ant-design-vue/components/vc-table/src/TableHeader.jsx

80 lines
1.9 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../../_util/vue-types';
import TableHeaderRow from './TableHeaderRow';
2018-03-24 14:02:24 +00:00
2019-01-12 03:33:27 +00:00
function getHeaderRows(columns, currentRow = 0, rows) {
rows = rows || [];
rows[currentRow] = rows[currentRow] || [];
2018-03-24 14:02:24 +00:00
columns.forEach(column => {
if (column.rowSpan && rows.length < column.rowSpan) {
while (rows.length < column.rowSpan) {
2019-01-12 03:33:27 +00:00
rows.push([]);
2018-03-24 14:02:24 +00:00
}
}
const cell = {
key: column.key,
2018-03-31 09:46:35 +00:00
className: column.className || column.class || '',
2018-03-24 14:02:24 +00:00
children: column.title,
column,
2019-01-12 03:33:27 +00:00
};
2018-03-24 14:02:24 +00:00
if (column.children) {
2019-01-12 03:33:27 +00:00
getHeaderRows(column.children, currentRow + 1, rows);
2018-03-24 14:02:24 +00:00
}
if ('colSpan' in column) {
2019-01-12 03:33:27 +00:00
cell.colSpan = column.colSpan;
2018-03-24 14:02:24 +00:00
}
if ('rowSpan' in column) {
2019-01-12 03:33:27 +00:00
cell.rowSpan = column.rowSpan;
2018-03-24 14:02:24 +00:00
}
if (cell.colSpan !== 0) {
2019-01-12 03:33:27 +00:00
rows[currentRow].push(cell);
2018-03-24 14:02:24 +00:00
}
2019-01-12 03:33:27 +00:00
});
return rows.filter(row => row.length > 0);
2018-03-24 14:02:24 +00:00
}
export default {
name: 'TableHeader',
props: {
fixed: PropTypes.string,
columns: PropTypes.array.isRequired,
expander: PropTypes.object.isRequired,
},
2018-03-25 10:07:04 +00:00
inject: {
2019-01-28 13:09:13 +00:00
table: { default: () => ({}) },
2018-03-25 10:07:04 +00:00
},
2018-03-24 14:02:24 +00:00
2019-01-12 03:33:27 +00:00
render() {
const { sComponents: components, prefixCls, showHeader, customHeaderRow } = this.table;
const { expander, columns, fixed } = this;
2018-03-24 14:02:24 +00:00
if (!showHeader) {
2019-01-12 03:33:27 +00:00
return null;
2018-03-24 14:02:24 +00:00
}
2019-01-12 03:33:27 +00:00
const rows = getHeaderRows(columns);
2018-03-24 14:02:24 +00:00
2019-01-12 03:33:27 +00:00
expander.renderExpandIndentCell(rows, fixed);
2018-03-24 14:02:24 +00:00
2019-01-12 03:33:27 +00:00
const HeaderWrapper = components.header.wrapper;
2018-03-24 14:02:24 +00:00
return (
<HeaderWrapper class={`${prefixCls}-thead`}>
2019-01-12 03:33:27 +00:00
{rows.map((row, index) => (
<TableHeaderRow
prefixCls={prefixCls}
key={index}
index={index}
fixed={fixed}
columns={columns}
rows={rows}
row={row}
components={components}
customHeaderRow={customHeaderRow}
/>
))}
2018-03-24 14:02:24 +00:00
</HeaderWrapper>
2019-01-12 03:33:27 +00:00
);
2018-03-24 14:02:24 +00:00
},
2019-01-12 03:33:27 +00:00
};