diff --git a/README.md b/README.md index be12421..e111a12 100755 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Page({ 配置项 | 说明 | 类型 | 可选值 | 默认值 | 必填 ---|---|---|---|---|--- -headers | 表格头部标题、列宽度、列属性 | Array | `{prop: 'datetime', width: 150, label: '日期'}` | [] | yes +headers | 表格头部标题、列宽度、列属性 | Array | `{prop: 'datetime', width: 150, label: '日期',align:'center',fixed:'left'}` | [] | yes data | 表格列表数据 | Array | | [] | no stripe | 是否为斑马纹 | boolean | true/false | false | no border | 是否有间隔线 | boolean | true/false | false | no diff --git a/src/index.js b/src/index.js index 4c89b36..a505c49 100755 --- a/src/index.js +++ b/src/index.js @@ -63,7 +63,8 @@ Component({ * 组件的初始数据 */ data: { - scrolWidth: '100%' + scrolWidth: '100%', + columns: [] }, /** @@ -72,13 +73,42 @@ Component({ observers: { // 在 numberA 或者 numberB 被设置时,执行这个函数 'headers': function (headers) { + if (!headers) return; + const reducer = (accumulator, currentValue) => { return accumulator + Number(currentValue.width) }; const scrolWidth = headers.reduce(reducer, 0) + // Calculate sticky offsets + let leftOffset = 0; + let rightOffset = 0; + + const columns = headers.map(h => ({...h})); + + // Calculate Left Offsets + for (let i = 0; i < columns.length; i++) { + if (columns[i].fixed === 'left') { + columns[i].stickyLeft = leftOffset; + leftOffset += Number(columns[i].width); + columns[i].isFixed = true; + columns[i].fixDirection = 'left'; + } + } + + // Calculate Right Offsets + for (let i = columns.length - 1; i >= 0; i--) { + if (columns[i].fixed === 'right') { + columns[i].stickyRight = rightOffset; + rightOffset += Number(columns[i].width); + columns[i].isFixed = true; + columns[i].fixDirection = 'right'; + } + } + this.setData({ - scrolWidth: scrolWidth + scrolWidth: scrolWidth, + columns: columns }) } }, @@ -88,7 +118,7 @@ Component({ */ methods: { onRowClick(e) { - this.triggerEvent('rowClick', e, e.currentTarget.dataset.it) + this.triggerEvent('rowClick', e.currentTarget.dataset) }, } }) \ No newline at end of file diff --git a/src/index.wxml b/src/index.wxml index 24816d8..e4e0502 100755 --- a/src/index.wxml +++ b/src/index.wxml @@ -1,14 +1,14 @@ - + {{ item.label }} @@ -16,30 +16,29 @@ - - {{it[head["prop"]]}} + {{it[head["prop"]]|| '-'}} @@ -47,7 +46,6 @@ {{ msg }} - + - - + \ No newline at end of file diff --git a/src/index.wxss b/src/index.wxss index f38f79a..af1883a 100755 --- a/src/index.wxss +++ b/src/index.wxss @@ -4,7 +4,6 @@ background: #fff; border-right:none; border-radius: 8rpx; - overflow: hidden; } .thead{ border-bottom: none;