|
| 1 | +<template> |
| 2 | + <div class="app"> |
| 3 | + <GithubCorner path="/item-mode" /> |
| 4 | + <div class="container"> |
| 5 | + <Header |
| 6 | + title="table-mode" |
| 7 | + :desciption="'Build ' + itemCount.toLocaleString() + ' items.'" |
| 8 | + :start-index="start" |
| 9 | + :on-data-change="onHeaderDataChange" |
| 10 | + /> |
| 11 | + <div class="main my-table"> |
| 12 | + <div> |
| 13 | + <table |
| 14 | + cellspacing="0" |
| 15 | + cellpadding="0" |
| 16 | + > |
| 17 | + <colgroup> |
| 18 | + <col :span="columnsLength" /> |
| 19 | + </colgroup> |
| 20 | + <thead> |
| 21 | + <tr> |
| 22 | + <th |
| 23 | + v-for="key in keys" |
| 24 | + :key="key" |
| 25 | + :title="key" |
| 26 | + >{{key}}</th> |
| 27 | + </tr> |
| 28 | + </thead> |
| 29 | + </table> |
| 30 | + </div> |
| 31 | + <!-- old item mode usage --> |
| 32 | + <!-- :item="item" |
| 33 | + :itemprops="getItemProps" --> |
| 34 | + <virtual-list |
| 35 | + :size="size" |
| 36 | + :remain="remain" |
| 37 | + :bench="30" |
| 38 | + :start="start" |
| 39 | + :isTable="true" |
| 40 | + :itemcount="itemCount" |
| 41 | + > |
| 42 | + <template v-slot:item="targetIndex"> |
| 43 | + <item |
| 44 | + :key="targetIndex" |
| 45 | + :height="getItemProps(targetIndex).props.height" |
| 46 | + :index="getItemProps(targetIndex).props.index" |
| 47 | + :info="getItemProps(targetIndex).props.info" |
| 48 | + ></item> |
| 49 | + </template> |
| 50 | + </virtual-list> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | +</template> |
| 55 | + |
| 56 | +<script> |
| 57 | +import Vue from 'vue'; |
| 58 | +import VirtualList from 'vue-virtual-scroll-list' |
| 59 | +import { countStorage, getRandomUser } from '../common/util' |
| 60 | +
|
| 61 | +
|
| 62 | +const Item = Vue.extend({ |
| 63 | + props: { |
| 64 | + info: { |
| 65 | + type: Object, |
| 66 | + required: true |
| 67 | + }, |
| 68 | + index: { |
| 69 | + type: Number, |
| 70 | + default: 0 |
| 71 | + }, |
| 72 | + height: { |
| 73 | + type: Number, |
| 74 | + default: 0 |
| 75 | + }, |
| 76 | + }, |
| 77 | + computed: { |
| 78 | + itemStyle() { |
| 79 | + return { |
| 80 | + 'height': `${this.height}px`, |
| 81 | + 'line-height': `${this.height}px` |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + render(h) { |
| 86 | + const { info, index, itemStyle } = this; |
| 87 | + const tds = Object.values(info).map(v => h('td', `(${index})--${v}`)) |
| 88 | + return h('tr', { style: itemStyle }, tds) |
| 89 | + } |
| 90 | +}) |
| 91 | +
|
| 92 | +const remain = 20 |
| 93 | +const itemSize = 20 |
| 94 | +const itemCount = countStorage.get() |
| 95 | +
|
| 96 | +let userInfoList = [] |
| 97 | +for (let i = 0; i < itemCount; i++) { |
| 98 | + userInfoList.push(getRandomUser()) |
| 99 | +} |
| 100 | +const keys = Object.keys(userInfoList[0]) |
| 101 | +const columnsLength = keys.length |
| 102 | +export default { |
| 103 | + name: 'App', |
| 104 | + components: { |
| 105 | + 'virtual-list': VirtualList, |
| 106 | + 'item': Item, |
| 107 | + }, |
| 108 | +
|
| 109 | + data() { |
| 110 | + return { |
| 111 | + remain, |
| 112 | + start: 0, |
| 113 | + size: itemSize, |
| 114 | + item: Item, |
| 115 | + itemCount: itemCount, |
| 116 | + keys: keys, |
| 117 | + columnsLength: columnsLength |
| 118 | + } |
| 119 | + }, |
| 120 | +
|
| 121 | + methods: { |
| 122 | + getItemProps(itemIndex) { |
| 123 | + return { |
| 124 | + key: itemIndex, |
| 125 | + props: { |
| 126 | + height: itemSize, |
| 127 | + index: itemIndex, |
| 128 | + info: userInfoList[itemIndex] || {} |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | +
|
| 133 | + onHeaderDataChange(type, value) { |
| 134 | + if (type === 'start') { |
| 135 | + this.start = value |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +</script> |
| 141 | + |
| 142 | +<style lang="less"> |
| 143 | +@import "../common/app.less"; |
| 144 | +.my-table { |
| 145 | + table { |
| 146 | + display: table !important; |
| 147 | + border-collapse: collapse; |
| 148 | + width: 100%; |
| 149 | + color: #fff; |
| 150 | + font-size: 13px; |
| 151 | + table-layout: fixed; |
| 152 | +
|
| 153 | + // table, |
| 154 | + // tr, |
| 155 | + // th, |
| 156 | + // td { |
| 157 | + // // border: 1px solid #d6d6d6; |
| 158 | + // } |
| 159 | + tbody { |
| 160 | + display: table-row-group !important; |
| 161 | + } |
| 162 | +
|
| 163 | + tr { |
| 164 | + td:not(:first-child) { |
| 165 | + font-family: myFirstFont; |
| 166 | + } |
| 167 | + } |
| 168 | +
|
| 169 | + th { |
| 170 | + background-color: #164893; |
| 171 | + font-size: 14px; |
| 172 | + } |
| 173 | +
|
| 174 | + th, |
| 175 | + td { |
| 176 | + padding: 8px 0; |
| 177 | + text-align: center; |
| 178 | + white-space: nowrap; |
| 179 | + text-overflow: ellipsis; |
| 180 | + overflow: hidden; |
| 181 | + } |
| 182 | +
|
| 183 | + tr:nth-child(2n) { |
| 184 | + background: #0f2e5d; |
| 185 | + } |
| 186 | +
|
| 187 | + tr { |
| 188 | + transition: background-color 1s; |
| 189 | + } |
| 190 | +
|
| 191 | + tr:hover { |
| 192 | + background: #2170b9; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | +</style> |
0 commit comments