Commit 9fea8e7de0d990528e339e6d8859c63b02e0e5b8
1 parent
8e171de8
fixed ivu-table-fixed-right scroll bar
Showing
4 changed files
with
146 additions
and
27 deletions
Show diff stats
examples/routers/table.vue
| ... | ... | @@ -5,7 +5,12 @@ |
| 5 | 5 | <Button @click="handleClearData">Clear Data</Button> |
| 6 | 6 | <Button @click="handleSelectAll(true)">Set all selected</Button> |
| 7 | 7 | <Button @click="handleSelectAll(false)">Cancel all selected</Button> |
| 8 | - | |
| 8 | + <div style='margin:20px 0px'> | |
| 9 | + <Table border :columns="columns2" :data="data3"></Table> | |
| 10 | + </div> | |
| 11 | + <div style='margin:20px 0px'> | |
| 12 | + <Table :height='200' border :columns="columns2" :data="data3"></Table> | |
| 13 | + </div> | |
| 9 | 14 | <div style='margin:20px 0px;'> |
| 10 | 15 | <Table :data="tableData1" :columns="tableColumns1" :height='500' stripe size='small'></Table> |
| 11 | 16 | <div style="margin: 10px;overflow: hidden"> |
| ... | ... | @@ -14,6 +19,7 @@ |
| 14 | 19 | </div> |
| 15 | 20 | </div> |
| 16 | 21 | </div> |
| 22 | + | |
| 17 | 23 | </div> |
| 18 | 24 | </template> |
| 19 | 25 | <script> |
| ... | ... | @@ -49,6 +55,95 @@ |
| 49 | 55 | data1: [ |
| 50 | 56 | |
| 51 | 57 | ], |
| 58 | + columns2: [ | |
| 59 | + { | |
| 60 | + title: 'Name', | |
| 61 | + key: 'name', | |
| 62 | + width: 100, | |
| 63 | + fixed: 'left' | |
| 64 | + }, | |
| 65 | + { | |
| 66 | + title: 'Age', | |
| 67 | + key: 'age', | |
| 68 | + width: 100 | |
| 69 | + }, | |
| 70 | + { | |
| 71 | + title: 'Province', | |
| 72 | + key: 'province', | |
| 73 | + width: 100 | |
| 74 | + }, | |
| 75 | + { | |
| 76 | + title: 'City', | |
| 77 | + key: 'city', | |
| 78 | + width: 100 | |
| 79 | + }, | |
| 80 | + { | |
| 81 | + title: 'Address', | |
| 82 | + key: 'address', | |
| 83 | + width: 200 | |
| 84 | + }, | |
| 85 | + { | |
| 86 | + title: 'Postcode', | |
| 87 | + key: 'zip', | |
| 88 | + width: 100 | |
| 89 | + }, | |
| 90 | + { | |
| 91 | + title: 'Action', | |
| 92 | + key: 'action', | |
| 93 | + fixed: 'right', | |
| 94 | + width: 120, | |
| 95 | + render: (h, params) => { | |
| 96 | + return h('div', [ | |
| 97 | + h('Button', { | |
| 98 | + props: { | |
| 99 | + type: 'text', | |
| 100 | + size: 'small' | |
| 101 | + } | |
| 102 | + }, 'View'), | |
| 103 | + h('Button', { | |
| 104 | + props: { | |
| 105 | + type: 'text', | |
| 106 | + size: 'small' | |
| 107 | + } | |
| 108 | + }, 'Edit') | |
| 109 | + ]); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + ], | |
| 113 | + data3: [ | |
| 114 | + { | |
| 115 | + name: 'John Brown', | |
| 116 | + age: 18, | |
| 117 | + address: 'New York No. 1 Lake Park', | |
| 118 | + province: 'America', | |
| 119 | + city: 'New York', | |
| 120 | + zip: 100000 | |
| 121 | + }, | |
| 122 | + { | |
| 123 | + name: 'Jim Green', | |
| 124 | + age: 24, | |
| 125 | + address: 'Washington, D.C. No. 1 Lake Park', | |
| 126 | + province: 'America', | |
| 127 | + city: 'Washington, D.C.', | |
| 128 | + zip: 100000 | |
| 129 | + }, | |
| 130 | + { | |
| 131 | + name: 'Joe Black', | |
| 132 | + age: 30, | |
| 133 | + address: 'Sydney No. 1 Lake Park', | |
| 134 | + province: 'Australian', | |
| 135 | + city: 'Sydney', | |
| 136 | + zip: 100000 | |
| 137 | + }, | |
| 138 | + { | |
| 139 | + name: 'Jon Snow', | |
| 140 | + age: 26, | |
| 141 | + address: 'Ottawa No. 2 Lake Park', | |
| 142 | + province: 'Canada', | |
| 143 | + city: 'Ottawa', | |
| 144 | + zip: 100000 | |
| 145 | + } | |
| 146 | + ], | |
| 52 | 147 | |
| 53 | 148 | tableData1: [], |
| 54 | 149 | tableColumns1: [ | ... | ... |
src/components/table/mixin.js
| ... | ... | @@ -31,7 +31,7 @@ export default { |
| 31 | 31 | width += scrollBarWidth; |
| 32 | 32 | } |
| 33 | 33 | // when fixed type,reset first right fixed column's width |
| 34 | - if (this.fixed === 'right') { | |
| 34 | + if (this.fixed === 'right' && top ) { | |
| 35 | 35 | const firstFixedIndex = this.columns.findIndex((col) => col.fixed === 'right'); |
| 36 | 36 | if (firstFixedIndex === index) { |
| 37 | 37 | let scrollBarWidth = this.$parent.scrollBarWidth; | ... | ... |
src/components/table/table.vue
| ... | ... | @@ -188,6 +188,7 @@ |
| 188 | 188 | currentContext: this.context, |
| 189 | 189 | cloneData: deepCopy(this.data), // when Cell has a button to delete row data, clickCurrentRow will throw an error, so clone a data |
| 190 | 190 | showVerticalScrollBar:false, |
| 191 | + showHorizontalScrollBar:false, | |
| 191 | 192 | headerWidth:0 |
| 192 | 193 | }; |
| 193 | 194 | }, |
| ... | ... | @@ -277,8 +278,9 @@ |
| 277 | 278 | this.rightFixedColumns.forEach((col) => { |
| 278 | 279 | if (col.fixed && col.fixed === 'right') width += col._width; |
| 279 | 280 | }); |
| 280 | - width += this.scrollBarWidth; | |
| 281 | + //width += this.scrollBarWidth; | |
| 281 | 282 | style.width = `${width}px`; |
| 283 | + style.right = `${this.showVerticalScrollBar?this.scrollBarWidth:0}px`; | |
| 282 | 284 | return style; |
| 283 | 285 | }, |
| 284 | 286 | bodyStyle () { |
| ... | ... | @@ -293,11 +295,11 @@ |
| 293 | 295 | fixedBodyStyle () { |
| 294 | 296 | let style = {}; |
| 295 | 297 | if (this.bodyHeight !== 0) { |
| 296 | - let height = this.bodyHeight + this.scrollBarWidth - 1; | |
| 298 | + let height = this.bodyHeight + (!this.showHorizontalScrollBar?this.scrollBarWidth:0) - 1; | |
| 297 | 299 | |
| 298 | 300 | // #2102 里,如果 Table 没有设置 width,而是集成父级的 width,固定列也应该不包含滚动条高度,所以这里直接计算表格宽度 |
| 299 | 301 | const tableWidth = parseInt(getStyle(this.$el, 'width')) - 1; |
| 300 | - if ((this.width && this.width < this.tableWidth) || tableWidth < this.tableWidth){ | |
| 302 | + if ((this.width && this.width < this.tableWidth) || tableWidth < this.tableWidth+(this.showVerticalScrollBar?this.scrollBarWidth:0)){ | |
| 301 | 303 | height = this.bodyHeight; |
| 302 | 304 | } |
| 303 | 305 | // style.height = this.scrollBarWidth > 0 ? `${this.bodyHeight}px` : `${this.bodyHeight - 1}px`; |
| ... | ... | @@ -349,12 +351,13 @@ |
| 349 | 351 | this.tableWidth = parseInt(getStyle(this.$el, 'width')) - 1; |
| 350 | 352 | } |
| 351 | 353 | this.columnsWidth = {}; |
| 352 | - this.fixedHeader(); | |
| 353 | - this.headerWidth = this.$refs.header.childNodes[0].offsetWidth; | |
| 354 | - if (!this.$refs.tbody) { | |
| 355 | - this.showVerticalScrollBar = false; | |
| 356 | - return; | |
| 357 | - } | |
| 354 | + this.$nextTick(()=>{ | |
| 355 | + this.headerWidth = this.$refs.header.childNodes[0].offsetWidth; | |
| 356 | + if (!this.$refs.tbody) { | |
| 357 | + this.showVerticalScrollBar = false; | |
| 358 | + return; | |
| 359 | + } | |
| 360 | + }); | |
| 358 | 361 | this.$nextTick(() => { |
| 359 | 362 | let columnsWidth = {}; |
| 360 | 363 | let autoWidthIndex = -1; |
| ... | ... | @@ -380,22 +383,36 @@ |
| 380 | 383 | }; |
| 381 | 384 | } |
| 382 | 385 | this.columnsWidth = columnsWidth; |
| 386 | + this.$nextTick(()=>{ | |
| 387 | + this.fixedHeader(); | |
| 388 | + if (this.$refs.tbody) { | |
| 389 | + let bodyContentEl = this.$refs.tbody.$el; | |
| 390 | + let bodyEl = bodyContentEl.parentElement; | |
| 391 | + let bodyContentHeight = bodyContentEl.offsetHeight; | |
| 392 | + let bodyContentWidth = bodyContentEl.offsetWidth; | |
| 393 | + let bodyWidth = bodyEl.offsetWidth; | |
| 394 | + let bodyHeight = bodyEl.offsetHeight; | |
| 395 | + let scrollBarWidth = 0; | |
| 396 | + if (bodyWidth < bodyContentWidth + (bodyHeight<bodyContentHeight?this.scrollBarWidth : 0)) { | |
| 397 | + scrollBarWidth = this.scrollBarWidth; | |
| 398 | + } | |
| 399 | + | |
| 400 | + this.showVerticalScrollBar = this.bodyHeight? bodyHeight - scrollBarWidth < bodyContentHeight : false; | |
| 401 | + this.showHorizontalScrollBar = bodyWidth < bodyContentWidth + (this.showVerticalScrollBar?this.scrollBarWidth:0); | |
| 402 | + | |
| 403 | + if(this.showVerticalScrollBar){ | |
| 404 | + bodyEl.classList.add(this.prefixCls +'-overflowY') | |
| 405 | + }else{ | |
| 406 | + bodyEl.classList.remove(this.prefixCls +'-overflowY') | |
| 407 | + } | |
| 408 | + if(this.showHorizontalScrollBar){ | |
| 409 | + bodyEl.classList.add(this.prefixCls +'-overflowX') | |
| 410 | + }else{ | |
| 411 | + bodyEl.classList.remove(this.prefixCls +'-overflowX') | |
| 412 | + } | |
| 383 | 413 | |
| 384 | - if (this.$refs.tbody) { | |
| 385 | - let bodyContentEl = this.$refs.tbody.$el; | |
| 386 | - let bodyEl = bodyContentEl.parentElement; | |
| 387 | - bodyEl.className = ''; | |
| 388 | - this.$nextTick(() => { bodyEl.className = this.prefixCls + '-body'; }); | |
| 389 | - let bodyContentHeight = bodyContentEl.offsetHeight; | |
| 390 | - let bodyContentWidth = bodyContentEl.offsetWidth; | |
| 391 | - let bodyWidth = bodyEl.offsetWidth; | |
| 392 | - let bodyHeight = bodyEl.offsetHeight; | |
| 393 | - let scrollBarWidth = 0; | |
| 394 | - if (bodyWidth < bodyContentWidth) { | |
| 395 | - scrollBarWidth = this.scrollBarWidth; | |
| 396 | 414 | } |
| 397 | - this.showVerticalScrollBar = this.bodyHeight? bodyHeight - scrollBarWidth < bodyContentHeight : false; | |
| 398 | - } | |
| 415 | + }); | |
| 399 | 416 | } |
| 400 | 417 | }); |
| 401 | 418 | // get table real height,for fixed when set height prop,but height < table's height,show scrollBarWidth | ... | ... |
src/styles/components/table.less
| ... | ... | @@ -71,8 +71,15 @@ |
| 71 | 71 | overflow: hidden; |
| 72 | 72 | } |
| 73 | 73 | &-body{ |
| 74 | - overflow: auto; | |
| 74 | + //overflow: auto; | |
| 75 | 75 | //position: relative; |
| 76 | + | |
| 77 | + } | |
| 78 | + &-overflowX{ | |
| 79 | + overflow-x: scroll; | |
| 80 | + } | |
| 81 | + &-overflowY{ | |
| 82 | + overflow-y: scroll; | |
| 76 | 83 | } |
| 77 | 84 | &-tip{ |
| 78 | 85 | overflow-x: auto; |
| ... | ... | @@ -278,7 +285,7 @@ |
| 278 | 285 | box-shadow: -2px 0 6px -2px rgba(0, 0, 0, 0.2); |
| 279 | 286 | } |
| 280 | 287 | &-fixed-header{ |
| 281 | - overflow: hidden; | |
| 288 | + overflow: visible; | |
| 282 | 289 | &-with-empty{ |
| 283 | 290 | .@{table-prefix-cls}-hidden{ |
| 284 | 291 | .@{table-prefix-cls}-sort{ | ... | ... |