el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼(行列互換)
el-table實(shí)現(xiàn)轉(zhuǎn)置表格
vue版本:vue2.6.10
elementui版本:2.15.14
實(shí)現(xiàn)效果:el-table實(shí)現(xiàn)行列互換
代碼:
<template>
<div class="app-container">
<span>原始數(shù)據(jù)</span>
<el-table
:data="datas"
border
>
<template v-for="(item, index) in columns">
<el-table-column
:key="index"
:prop="item.prop"
align="center"
:label="item.label"
/>
</template>
</el-table>
<span>行轉(zhuǎn)列的數(shù)據(jù)</span>
<el-table
:data="tableData"
border
>
<el-table-column v-for="item in columnsData" :key="item.prop" :label="item.label" :prop="item.prop">
<template slot-scope="scope">
{{scope.row[item.prop]}}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: 'TestTable',
data() {
return {
datas: [
{
"user_name": "小紅",
"user_sex": "女",
"user_age": 18,
"grade": 100
},
{
"user_name": "小明",
"user_sex": "男",
"user_age": 20,
"grade": 97
},
{
"user_name": "小紫",
"user_sex": "女",
"user_age": 21,
"grade": 99
},
{
"user_name": "小李",
"user_sex": "男",
"user_age": 19,
"grade": 98
}
],
columns: [
{ "label": "名稱", "prop": "user_name" },
{ "label": "性別", "prop": "user_sex" },
{ "label": "年齡", "prop": "user_age" },
{ "label": "成績", "prop": "grade" },
],
tableData: [],
columnsData: []
}
},
created() {
this.init()
},
methods: {
init() {
console.log('test')
const _this = this
const columnObj = {} //創(chuàng)建標(biāo)題數(shù)組中第一個(gè)對象
columnObj.label = '名稱' //第一個(gè)標(biāo)題名稱
columnObj.prop = 'title' //第一個(gè)標(biāo)題名稱對應(yīng)的字段
_this.columnsData.push(columnObj) //第一個(gè)標(biāo)題 放入標(biāo)題數(shù)組中
_this.tableData.push({ 'title': '性別' }) //表格數(shù)據(jù)中第一個(gè)對象數(shù)據(jù) 屬性名叫 title 與上面的第一個(gè)prop設(shè)置一樣
_this.tableData.push({ 'title': '年齡' }) //表格數(shù)據(jù)中第二個(gè)對象數(shù)據(jù) 屬性名叫 title 與上面的第一個(gè)prop設(shè)置一樣
_this.tableData.push({ 'title': '成績' }) //表格數(shù)據(jù)中第三個(gè)對象數(shù)據(jù) 屬性名叫 title 與上面的第一個(gè)prop設(shè)置一樣
var props = 'prop' //自定義字段名稱
_this.datas.forEach(function(item, index) {
const columnObj = {}
columnObj.label = item.user_name // 每一列的標(biāo)題的名稱
columnObj.prop = props + index //自定義每一列標(biāo)題字段名稱
_this.columnsData.push(columnObj)
_this.$set(_this.tableData[0], columnObj.prop, item.user_sex) //表格數(shù)據(jù)中第一個(gè)數(shù)組對象 往里面添加自定義的屬性
_this.$set(_this.tableData[1], columnObj.prop, item['user_age']) //表格數(shù)據(jù)中第二個(gè)數(shù)組對象 往里面添加自定義的屬性
_this.$set(_this.tableData[2], columnObj.prop, item.grade) //表格數(shù)據(jù)中第三個(gè)數(shù)組對象 往里面添加自定義的屬性
})
console.log(_this.columnsData)
console.log(_this.tableData)
}
}
}
</script>界面展示效果:

Vue 縱向Table轉(zhuǎn)橫向Table (轉(zhuǎn)置)

數(shù)組參照矩陣思想, 對數(shù)組進(jìn)行轉(zhuǎn)置。
缺點(diǎn): 轉(zhuǎn)置后的數(shù)組僅是單純的存每一行數(shù)據(jù)的數(shù)組用于展示, 失去了原數(shù)組的數(shù)組結(jié)構(gòu). 建議該方法用于僅展示的需求.
<template>
<div class="m50">
<el-table border style="margin-top: 50px;" :data="originData">
<el-table-column label="題型" property="type" align="center">
</el-table-column>
<el-table-column label="數(shù)量" property="num" align="center">
</el-table-column>
<el-table-column label="均分" property="average" align="center">
</el-table-column>
</el-table>
<!-- 轉(zhuǎn)化后 -->
<el-table border style="margin-top: 50px;" :data="transData">
<el-table-column v-for="(item, index) in transTitle" :label="item" :key="index" align="center">
<template slot-scope="scope">
{{scope.row[index]}}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
// originData 為后端原始正常的數(shù)據(jù), 此數(shù)據(jù)按正常表格展示 一行一行的數(shù)據(jù)
// 保證數(shù)組里每一個(gè)對象中的字段順序, 從上到下 一次對應(yīng)顯示表格中的從左到右
originData: [{
type: '選擇題',
num: '5題',
average: '3分/題',
},
{
type: '填空題',
num: '5題',
average: '3分/題',
},
{
type: '選擇題',
num: '2題',
average: '10分/題',
}
],
originTitle: ['題型', '數(shù)量', '均分'], // originTitle 該標(biāo)題為 正常顯示的標(biāo)題, 數(shù)組中的順序就是上面數(shù)據(jù)源對象中的字段標(biāo)題對應(yīng)的順序
transTitle: ['', '學(xué)生1', '學(xué)生2', '學(xué)生3'], // transTitle 該標(biāo)題為轉(zhuǎn)化后的標(biāo)題, 注意多一列, 因?yàn)樵瓉淼臉?biāo)題變成了豎著顯示了, 所以多一列標(biāo)題, 第一個(gè)為空即可
transData: [],
}
},
created() {
// 數(shù)組按矩陣思路, 變成轉(zhuǎn)置矩陣
let matrixData = this.originData.map((row, i) => {
let arr = []
for (let key in row) {
arr.push(row[key])
}
return arr
})
// 加入標(biāo)題拼接最終的數(shù)據(jù)
this.transData = matrixData[0].map((col, i) => {
return [this.originTitle[i], ...matrixData.map((row) => {
return row[i]
})]
})
}
}
</script>
<style lang="scss" scoped>
.m50 {
margin: 50px;
}
</style>到此這篇關(guān)于el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼的文章就介紹到這了,更多相關(guān)el-table轉(zhuǎn)置表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue封裝DateRangePicker組件流程詳細(xì)介紹
在后端管理項(xiàng)目中使用vue來進(jìn)行前端項(xiàng)目的開發(fā),但我們都知道Vue實(shí)際上無法監(jiān)聽由第三方插件所引起的數(shù)據(jù)變化。也無法獲得JQuery這樣的js框架對元素值的修改的。而日期控件daterangepicker又基于JQuery來實(shí)現(xiàn)的2022-11-11
vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇
項(xiàng)目中需要選擇時(shí)間范圍,并且只能選擇當(dāng)前日期之后的七天,本文就來介紹了vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯(cuò)誤解
這篇文章主要為大家介紹了SyntaxError:?/xx.vue:?Unexpected?token,?expected?“,“錯(cuò)誤解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
VUE對Storage的過期時(shí)間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對Storage的過期時(shí)間設(shè)置,及增刪改查方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue使用Echarts實(shí)現(xiàn)立體柱狀圖
這篇文章主要為大家詳細(xì)介紹了Vue使用Echarts實(shí)現(xiàn)立體柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
使用vue中的混入mixin優(yōu)化表單驗(yàn)證插件問題
這篇文章主要介紹了使用vue中的混入mixin優(yōu)化表單驗(yàn)證插件,本文是小編給大家總結(jié)的一些表單插件的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07

