-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): !#zh: 增加表格组件 !#: add table component
- Loading branch information
Showing
4 changed files
with
293 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// https://github.com/luban-h5-components/plugin-common-props | ||
import PropTypes from '@luban-h5/plugin-common-props' | ||
import { addListener as addResizeListener, removeListener } from 'resize-detector' | ||
import './styles/table.scss' | ||
|
||
function sum (arr = [], key) { | ||
return arr.map(item => item[key]).reduce((a, b) => a + b, 0) | ||
} | ||
|
||
export default { | ||
name: 'lbp-table', | ||
extra: { | ||
defaultStyle: { | ||
width: 320, | ||
height: 150 | ||
} | ||
}, | ||
data: () => ({ | ||
mainTableWrapperEle: null, | ||
mainTableEle: null, | ||
fixedTableWrapperEle: null, | ||
fixedTableEle: null | ||
}), | ||
props: { | ||
theme: PropTypes.string({ defaultValue: '', label: '主题', visible: false }), | ||
columnWidth: PropTypes.number({ label: '每列宽度(px)', defaultValue: 100 }), | ||
freezeCount: PropTypes.number({ label: '冻结列数(px)', defaultValue: 0 }), | ||
dataset: PropTypes.excel({ | ||
defaultValue: () => [ | ||
[ '列A', '列B', '列C'], | ||
[ '————', '————', '————'], | ||
[ '————', '————', '————'], | ||
[ '————', '————', '————'] | ||
] | ||
}) | ||
}, | ||
watch: { | ||
freezeCount () { | ||
setTimeout(() => { | ||
this.setFixedTableStyle() | ||
}, 100) | ||
} | ||
}, | ||
render () { | ||
const renderCell = cell => { | ||
return <td><div class="cell" >{cell}</div></td> | ||
} | ||
|
||
const renderTable = (tableData = [], tableClass = '', tableStyle = {}) => { | ||
const headers = tableData.length ? tableData[0] : [] | ||
const columnsCount = headers.length | ||
return ( | ||
<table class={tableClass} style={tableStyle}> | ||
<colgroup> | ||
{ | ||
[...Array(columnsCount)].map((item, i) => <col style={{ width: this.columnWidth + 'px' }} />) | ||
} | ||
</colgroup> | ||
<tbody> | ||
{ tableData.map(row => <tr>{ row.map(renderCell) }</tr>) } | ||
</tbody> | ||
</table> | ||
) | ||
} | ||
|
||
return ( | ||
<div class={['lbp-table', this.theme]} ref="lbpTable"> | ||
<div class="main-table-wrapper"> | ||
{renderTable(this.dataset)} | ||
</div> | ||
<div class="fixed-table-wrapper" v-show="freezeCount"> | ||
{renderTable(this.dataset, 'left-table')} | ||
</div> | ||
</div> | ||
) | ||
}, | ||
methods: { | ||
getFixedColsWidth () { | ||
const tableHeaders = [].slice.apply(this.mainTableEle.querySelectorAll('tr:first-child > td')) | ||
const freezeColsWidth = sum(tableHeaders.slice(0, +this.freezeCount), 'offsetWidth') | ||
return freezeColsWidth | ||
}, | ||
setFixedTableStyle () { | ||
this.fixedTableWrapperEle.style.width = `${this.getFixedColsWidth()}px` | ||
this.fixedTableWrapperEle.style.height = `calc(100% - ${this.mainTableWrapperEle.offsetHeight - this.mainTableWrapperEle.scrollHeight}px)` | ||
}, | ||
setTableWidth () { | ||
const parentWidth = this.$el.parentNode.style.width | ||
this.fixedTableEle.style.width = this.mainTableEle.style.width = parentWidth | ||
}, | ||
initElements () { | ||
const root = this.$el | ||
this.mainTableWrapperEle = root.querySelector('.main-table-wrapper') | ||
this.mainTableEle = root.querySelector('.main-table-wrapper > table') | ||
this.fixedTableWrapperEle = root.querySelector('.fixed-table-wrapper') | ||
this.fixedTableEle = root.querySelector('.left-table') | ||
} | ||
}, | ||
|
||
mounted () { | ||
this.initElements() | ||
this.setTableWidth() | ||
this.setFixedTableStyle() | ||
addResizeListener(this.$refs.lbpTable, () => { | ||
this.setTableWidth() | ||
if (this.freezeCount) { | ||
this.setFixedTableStyle() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
$border-color: #ebeef5; | ||
.lbp-table { | ||
position: relative; | ||
overflow: hidden; | ||
|
||
table { | ||
position: absolute; | ||
table-layout: fixed; | ||
height: 100%; | ||
background: white; | ||
|
||
// 边框 | ||
td { | ||
border-top: 2px solid $border-color; | ||
border-left: 2px solid $border-color; | ||
} | ||
tr:last-child { | ||
border-bottom: 2px solid $border-color; | ||
border-right: 2px solid $border-color; | ||
} | ||
|
||
// 表头加粗 | ||
tr:first-child { | ||
background-color: #f6f6f6; | ||
font-size: 13px; | ||
font-weight: bold; | ||
} | ||
|
||
// 居中对齐 | ||
td { | ||
text-align: center; | ||
} | ||
|
||
} | ||
|
||
.main-table-wrapper { | ||
position: absolute; | ||
left: 0px; | ||
top: 0px; | ||
overflow: auto; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
.fixed-table-wrapper { | ||
position: absolute; | ||
left: 0px; | ||
top: 0px; | ||
overflow: hidden; | ||
} | ||
} | ||
|
||
|
||
.lbp-table-theme-stripe { | ||
table { | ||
tbody tr { | ||
// 奇 | ||
&:nth-child(odd):not(:first-child) { | ||
background-color: #f6f6f6; | ||
} | ||
|
||
&:nth-child(even) { | ||
background-color: white; | ||
} | ||
} | ||
} | ||
} | ||
|
||
.lbp-table-theme-light-blue { | ||
table { | ||
tbody tr { | ||
&:first-child { | ||
background-color: #edf8ff; | ||
} | ||
&:nth-child(odd):not(:first-child) { | ||
background-color: #edf8ff; | ||
} | ||
|
||
&:nth-child(even) { | ||
background-color: white; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.