今天遇到一個問題,實現這樣一個豎直的顯示表頭的表格,如下圖。默認顯示兩列。

vue實現代碼如下:
tableComponent.vue:
<template> <table class="mailTable" :style="styleObject" v-if="s_showByRow"> <tr v-for="index in rowCount"> <td class="column">{{tableData[index*2-2].key}}</td> <td>{{tableData[index*2-2].value}}</td> <td class="column">{{tableData[index*2-1] !== undefined ? tableData[index*2-1].key : ''}}</td> <td>{{tableData[index*2-1] !== undefined ? tableData[index*2-1].value : ''}}</td> </tr> </table> <table class="mailTable" :style="styleObject" v-else> <tr v-for="index in rowCount"> <td class="column">{{tableData[index-1].key}}</td> <td>{{tableData[index-1].value}}</td> <td class="column">{{tableData[rowCount+index-1] !== undefined ? tableData[rowCount+index-1].key : ''}}</td> <td>{{tableData[rowCount+index-1] !== undefined ? tableData[rowCount+index-1].value : ''}}</td> </tr> </table> </template> <script> export default { data() { return { styleObject: {}, s_showByRow: true, }; }, props: ['tableData', 'tableStyle', 'showByRow'], computed: { rowCount: function() { return Math.ceil(this.tableData.length/2); } }, created() { this.styleObject = this.tableStyle; if(this.showByRow !== undefined){ this.s_showByRow = this.showByRow; } }, } </script>
補充css:
<style> .mailTable, .mailTable tr, .mailTable tr td{ border:1px solid #E6EAEE; } .mailTable{ font-size: 12px; color: #71787E; } .mailTable tr td{ border:1px solid #E6EAEE; width: 150px; height: 35px; line-height: 35px; box-sizing: border-box; padding: 0 10px; } .mailTable tr td.column { background-color: #EFF3F6; color: #393C3E; } </style>
引用時如下:
<mailTable :tableData="tableData" :tableStyle="{ width:'600px' }"></mailTable>
數據tableData格式如下:
tableData: [ {key: '單號', value: '1001'}, {key: '商品名稱', value: '籃球'}, {key: '價格', value: '120.00'}, {key: '訂單日期', value: '2017-03-01'}, {key: '付款方式', value: '在線支付'}, {key: '收貨地址', value: '北京市海淀區西北旺鎮'}, ],
效果圖如下:
