当前位置:   article > 正文

vue3 实现关于 el-table 表格组件的封装以及调用_vue3 el-table

vue3 el-table

一、示例图:

二、组件

  1. <template>
  2. <div class="sn-table" :class="props.colorType === 1 ? '' : 'bg-scroll'">
  3. <el-table :data="tableData" :row-class-name="tableRowClassName" height="500" style="width: 100%;"
  4. :default-sort="[{ prop: '正确率', order: 'descending' },{ prop: '未答题数', order: 'descending' }]"
  5. :class="props.colorType === 1 ? '' : 'bg-scroll'">
  6. <el-table-column align="center" :prop="item.keyName"
  7. :sortable="item.keyName==='正确率'&&props.isExistSelect||item.keyName==='未答题数'&&props.isExistSelect?true:false"
  8. :label="item.keyName" v-for="item in columns"
  9. :width="item.width ? item.width + 'px' : ''">
  10. <template #default="scope">
  11. <div v-if="item.keyName==='正确率'&&props.isExistSelect" class="tag-list">
  12. <el-progress :percentage="scope.row[item.keyName]" color="#00B386" :stroke-width="10" :text-inside="false"/>
  13. </div>
  14. </template>
  15. </el-table-column>
  16. </el-table>
  17. </div>
  18. </template>
  19. <script lang='ts' setup>
  20. type TProps = {
  21. tableData: any[]
  22. columns: any[],
  23. colorType: number, // 颜色类型
  24. isExistSelect: boolean // 是否存在筛选项
  25. }
  26. const props = withDefaults(defineProps<TProps>(), {})
  27. const tableRowClassName = ({ rowIndex }: { rowIndex: number }) => {
  28. if (rowIndex % 2 === 1) {
  29. return props.colorType === 1 ? 'odd-row' : 'class-odd-row'
  30. } else {
  31. return props.colorType === 1 ? 'even-row' : 'class-even-row'
  32. }
  33. }
  34. </script>
  35. <style lang='scss' scoped>
  36. .bg-scroll {
  37. border-radius: 10px;
  38. height: 96%;
  39. overflow-y: scroll;
  40. &::-webkit-scrollbar {
  41. width: 5px;
  42. height: 0 !important;
  43. }
  44. &::-webkit-scrollbar-thumb {
  45. border-radius: 10px;
  46. background: #eeeeee;
  47. }
  48. }
  49. .sn-table {
  50. padding: 0 10px 0 20px;
  51. :deep(.el-table) {
  52. color: #ffffff !important;
  53. tr {
  54. td {
  55. border: none;
  56. padding: 16px 0;
  57. font-size: 15px;
  58. }
  59. }
  60. th.el-table__cell {
  61. background: #141414 !important;
  62. border: none;
  63. color: #00B386;
  64. font-size: 14px;
  65. font-weight: 400;
  66. }
  67. .even-row {
  68. background-color: #333 !important;
  69. }
  70. .odd-row {
  71. background-color: #141414 !important;
  72. }
  73. .class-even-row {
  74. background-color: #333 !important;
  75. }
  76. .class-odd-row {
  77. background-color: #00B386 !important;
  78. }
  79. }
  80. :deep(.el-scrollbar__wrap--hidden-default) {
  81. background: #141414 !important;
  82. }
  83. :deep(.el-table--enable-row-hover) {
  84. .el-table__body {
  85. tr:hover>td.el-table__cell {
  86. color: #8C8C8C;
  87. background: #333 !important;
  88. }
  89. }
  90. }
  91. :deep(.el-table__inner-wrapper) {
  92. &::before {
  93. background-color: transparent;
  94. }
  95. }
  96. :deep(.el-table .ascending .sort-caret.ascending){
  97. border-bottom-color:#00B386 !important;
  98. }
  99. :deep(.el-table .descending .sort-caret.descending){
  100. border-top-color:#00B386 !important;
  101. }
  102. .ok-text{
  103. font-size: 35px;
  104. font-weight: 300;
  105. }
  106. .tag-list{
  107. width: 100%;
  108. padding: 2px 0;
  109. .tag-btn{
  110. border-radius: 5px;
  111. border: 1px solid #EF8714;
  112. color: #EF8714;
  113. padding: 1px 10px;
  114. margin-right: 15px;
  115. &:last-child{
  116. margin-right: 0;
  117. }
  118. }
  119. }
  120. }
  121. :deep(.el-progress){
  122. width: 185px;
  123. margin: 0 auto;
  124. }
  125. :deep(.el-progress__text){
  126. span{
  127. font-size: 16px;
  128. }
  129. }
  130. :deep(.el-progress-bar__outer){
  131. background: #D9D9D9;
  132. }
  133. </style>

三、页面调用

  1. <details-table :tableData="knowInfo" :columns="knowColumns" :isExistSelect="false" :colorType="1"/>
  2. <script setup lang="ts">
  3. import { onMounted, ref } from 'vue'
  4. import CanvasVideo from "@/components/CanvasVideo.vue"
  5. const knowInfo = ref<any[]>([])
  6. const knowColumns = ref<any[]>([])
  7. onMounted(()=>{
  8. init()
  9. })
  10. //数据处理
  11. const init = () => {
  12. const datas = ref([
  13. {studentName:'陈佳颖',correctRate:0,noAnswerCount:13},
  14. {studentName:'丁靖芸',correctRate:0,noAnswerCount:13},
  15. {studentName:'谷雨恒',correctRate:0,noAnswerCount:13},
  16. {studentName:'欧阳江源',correctRate:0,noAnswerCount:13},
  17. {studentName:'任行宽',correctRate:0,noAnswerCount:13},
  18. {studentName:'任彦宇',correctRate:0,noAnswerCount:13},
  19. {studentName:'王骁南',correctRate:0,noAnswerCount:13},
  20. {studentName:'吴骏扬',correctRate:0,noAnswerCount:13}
  21. ])
  22. if (datas && datas.length > 0) {
  23. datas.forEach((it: any, index:number) => {
  24. knowInfo.value.push({
  25. '行号': index+1,
  26. '姓名': it.studentName,
  27. '正确率': it.correctRate,
  28. '未答题数': it.noAnswerCount
  29. })
  30. })
  31. for (const key in knowInfo.value[0]) {
  32. knowColumns.value.push({
  33. keyName: key,
  34. width: key === '行号' ? 140 : null
  35. })
  36. }
  37. }
  38. }
  39. </script>

四、其他

(1)自定义标题

  1. <el-table :data="datas" style="width: 100%;">
  2. <el-table-column label="" prop="name" align="center">
  3. <template #header>
  4. 姓名
  5. </template>
  6. </el-table-column>
  7. </el-table>

(2)自定义下标

  1. <el-table :data="datas" style="width: 100%;">
  2. <el-table-column label="行号" align="center">
  3. <template #default="{$index}">
  4. {{$index+1}}
  5. </template>
  6. </el-table-column>
  7. </el-table>

(3)自定义内容

  1. <el-table :data="datas" style="width: 100%;">
  2. <el-table-column label="姓名" prop="name" align="center">
  3. <template #default="scope">
  4. <div>{{scope.row.name}}s</div>
  5. </template>
  6. </el-table-column>
  7. </el-table>

(4)添加排序(升序、降序)

  1. <el-table :data="datas" style="width: 100%;"
  2. :default-sort="[{ prop: 'rank', order: 'descending' },{ prop: 'time', order: 'descending' }]">
  3. <el-table-column label="排名" prop="rank" sortable align="center"/>
  4. <el-table-column label="时长" prop="time" sortable align="center"/>
  5. </el-table>

(5)多选与单选

1. 单选

  1. <el-table :data="datas" style="width: 100%;"
  2. row-key="id" ref="multipleTable" highlight-current-row @row-click="rowselect" @selection-change="selectGroupChange">
  3. <el-table-column type="selection" width="55" />
  4. <el-table-column label="排名" prop="rank" align="center"/>
  5. <el-table-column label="时长" prop="time" align="center"/>
  6. </el-table>
  7. <script setup lang="ts">
  8. import { ref } from "vue"
  9. const multipleTable = ref()
  10. const handleList = ref([])
  11. // 设置单选||显示高亮
  12. const rowselect = (row:any) => {
  13. multipleTable.value.toggleRowSelection(row)
  14. }
  15. // 选择多个单选框,只取最后选中的那一个
  16. const selectGroupChange = (row:any) => {
  17. if (row.length > 1) {
  18. multipleTable.value.clearSelection()
  19. multipleTable.value.toggleRowSelection(row.pop())
  20. return
  21. }
  22. if (row.length == 1) {
  23. handleList.value = row
  24. } else {
  25. handleList.value = []
  26. }
  27. }
  28. </script>
  29. <style lang='scss' scoped>
  30. // 隐藏表头选择框
  31. :deep(.el-table__header){
  32. .el-checkbox{
  33. display: none;
  34. }
  35. }
  36. </style>

2. 多选

  1. <el-table :data="datas" style="width: 100%;"
  2. row-key="id" @select="handleSelectionChange" @select-all="handleAllChange">
  3. <el-table-column type="selection" width="55" />
  4. <el-table-column label="排名" prop="rank" align="center"/>
  5. <el-table-column label="时长" prop="time" align="center"/>
  6. </el-table>
  7. <script setup lang="ts">
  8. // 选择多个选择框
  9. const handleSelectionChange = (selecteds: any, row: any) => {}
  10. // 全选
  11. const handleAllChange= (row:any) => {}
  12. </script>

       希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/101016
推荐阅读
相关标签
  

闽ICP备14008679号