不想努力的程序员 2024-09-14 12:02 采纳率: 48.5%
浏览 7
已结题

element-plus中el-table合并多行如何实现

后端提供的数据如下


{
            "topicType": "D",
            "topicId": 1608,
            "fullMarks": 4,
            "topicNumber": "8-8",
            "classDifficulty": 0.92,
            "gradeDifficulty": 0.69,
            "classDistinction": 0.24,
            "gradeDistinction": 0.01,
            "classAvgScore": 3.67,
            "gradeAvgScore": 2.77,
            "classStandardDeviation": 1.12,
            "gradeStandardDeviation": 1.85,
            "correctCount": 33,
            "correctOptions": "D",
            "optionCount": "D:33",
            "wrongAnswerVOList": [
                {
                    "options": "B",
                    "count": 2,
                    "optionCount": "B:2",
                    "gradeCount": 48,
                    "gradeWrongAnswerRate": 0.21,
                    "classWrongAnswerRate": 0.06
                },
                {
                    "options": "C",
                    "count": 1,
                    "optionCount": "C:1",
                    "gradeCount": 22,
                    "gradeWrongAnswerRate": 0.1,
                    "classWrongAnswerRate": 0.03
                }
            ]
        },

前端是使用vue3+ts+element-plus.如何通过el-table将这些数据展示出来,效果如下图

img

目前我做出来的效果如下图

img

代码如下



<template>
<div>
  <div style="margin: 10px 0">
    <el-select v-model="pageParam.projectId"
               @change="getAllSubject"
               clearable filterable placeholder="请选择项目"
               style="margin-left: 10px;width: 200px;margin-right: 10px">
      <el-option
          v-for="item in allData.projectList"
          :key="item.id"
          :label="item.projectName"
          :value="item.id">
      </el-option>
    </el-select>
    <el-select v-model="pageParam.classTypeId" clearable filterable placeholder="请选择"
               @change="getAllClass"
               style="margin-left: 10px;width: 100px;margin-right: 10px" v-if="userInfo.haveTeachingClass">
      <el-option
          v-for="item in allData.classType"
          :key="item.id"
          :label="item.value"
          :value="item.id">
      </el-option>
    </el-select>
    <el-select v-model="pageParam.departmentId"
               clearable filterable placeholder="请选择班级"
               style="margin-left: 10px;width: 180px;margin-right: 10px">
      <el-option
          v-for="item in allData.classList"
          :key="item.id"
          :label="item.departmentName"
          :value="item.id">
      </el-option>
    </el-select>
    <el-select v-model="pageParam.subjectId" clearable filterable placeholder="请选择科目"
               style="margin-left: 10px;width: 180px;margin-right: 10px">
      <el-option
          v-for="item in allData.subjectList"
          :key="item.id"
          :label="item.subjectName"
          :value="item.id">
      </el-option>
    </el-select>

    <el-button class="ml-5" type="primary" @click="toSearch">搜索</el-button>


  </div>
  <div class="top-back">
    <div style="color: #b1b1b1;font-size: 15px">本试卷共{{allData.firstInfo.topicNumber}}道题,
      <span v-if="allData.firstInfo.subjectiveQuestionsNumber>0">主观题{{allData.firstInfo.subjectiveQuestionsNumber}}道,分值占比{{allData.firstInfo.subjectiveQuestionsScoreRate}}%,</span>
      <span v-if="allData.firstInfo.objectiveQuestionsNumber>0">客观题{{allData.firstInfo.objectiveQuestionsNumber}}道,分值占比{{allData.firstInfo.objectiveQuestionsScoreRate}}%</span>
      学科总分{{ allData.firstInfo.totalScore }}</div>
    <div class="top-item">
      <div class="item-number">{{ allData.midInfo.difficulty}}</div>
      <div>难度</div>
    </div>
    <div class="top-item">
      <div  class="item-number">{{ allData.midInfo.difficultyRate}}</div>
      <div>难度比例(难:中:易)</div>
    </div>
    <div class="top-item">
      <div class="item-number">{{ allData.midInfo.distinction }}</div>
      <div>区分度</div>
    </div>

  </div>
  <div>
    <div class="table-title-info">客观题分析</div>
    <div class="table-container">
      <el-table :data="allData.objectiveQuestionsList"
                :span-method="objectSpanMethod"
                border stripe :header-cell-class-name="'headerBg'"
                row-key="id" default-expand-all >
        <el-table-column prop="topicNumber" label="题号" align="center"></el-table-column>
        <el-table-column prop="fullMarks" label="满分" align="center"></el-table-column>
        <el-table-column prop="fullMarks" label="难度" align="center">
          <template v-slot="scope">
            <el-table-column prop="classDifficulty" label="班级" align="center"></el-table-column>
            <el-table-column prop="gradeDifficulty" label="年级" align="center"></el-table-column>
          </template>
        </el-table-column>
        <el-table-column prop="fullMarks" label="区分度" align="center">
          <template v-slot="scope">
            <el-table-column prop="classDistinction" label="班级" align="center"></el-table-column>
            <el-table-column prop="gradeDistinction" label="年级" align="center"></el-table-column>
          </template>
        </el-table-column>
        <el-table-column prop="fullMarks" label="标准差" align="center">
          <template v-slot="scope">
            <el-table-column prop="classStandardDeviation" label="班级" align="center"></el-table-column>
            <el-table-column prop="gradeStandardDeviation" label="年级" align="center"></el-table-column>
          </template>
        </el-table-column>
        <el-table-column prop="fullMarks" label="平均分" align="center">
          <template v-slot="scope">
            <el-table-column prop="classAvgScore" label="班级" align="center"></el-table-column>
            <el-table-column prop="gradeAvgScore" label="年级" align="center"></el-table-column>
          </template>
        </el-table-column>
        <el-table-column prop="optionCount" label="答对人数" align="center">

        </el-table-column>

        <el-table-column prop="wrongAnswerVOList.optionCount" label="答错人数" align="center">

        </el-table-column>
        <el-table-column prop="fullMarks" label="错选率" align="center">
          <template v-slot="scope">
            <el-table-column prop="wrongAnswerVOList.classAvgScore" label="班级" align="center">

            </el-table-column>
            <el-table-column prop="wrongAnswerVOList.gradeAvgScore" label="年级" align="center">

            </el-table-column>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div>
    <div class="table-title-info">主观题分析</div>
  </div>


</div>
</template>
<script setup lang="ts">
import {reactive, ref, watch} from "vue";
import {userLoginInfoStore} from "@/store/user.ts";
import {roleItemInfo} from "@/store/roleState.ts";
import {selectAllProject} from "@/api/project.ts";
import {selectSubjectByProductId} from "@/api/subject.ts";
import {selectTeachingClass} from "@/api/grade.ts";
import {selectMiddleInfo, selectTopicInfo, selectTopInfo} from "@/api/topicAnalysis.ts";
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";
import type { TableColumnCtx } from 'element-plus'
const pageParam = reactive({
  projectId: "",
  classTypeId:1,
  subjectId: "",
  departmentId: "",
  page:1,
  pageSize:10
})
const allData = reactive({
  classList: [],
  projectList:[],
  subjectList:[],
  classType: [
    {
      id: 1,
      value: "行政班"
    }, {
      id: 2,
      value: "教学班"
    }
  ],
  roleId:'',
  firstInfo:[],
  midInfo:[],
  objectiveQuestionsList:[],
  subjectiveQuestions:[]
})
const userLoginFormInfo = userLoginInfoStore()
const userInfo = userLoginFormInfo.userInfo
//查询所有科目
const getAllSubject = () => {
  let list = {
    productId: pageParam.projectId
  }
  allData.subjectList = []
  selectSubjectByProductId(list).then(function (res: any) {
    if (res.code === 200) {
      let dataList = res.data
      for (let i = 0; i < dataList.length; i++) {
        allData.subjectList.push(dataList[i])
      }

    }
  })
}
//查询所有班级
const getAllClass = () => {
  let list = {
    roleId:allData.roleId,
    classType:pageParam.classTypeId,
    projectId:pageParam.projectId
  }
  selectTeachingClass(list).then(function (res: any) {
    if (res.code === 200) {
      let newData = res.data
      for (let i = 0; i < newData.length; i++) {
        if(newData[i].departmentName=='所有班级'){
          continue;
        }
        allData.classList.push(newData[i])
      }


      pageParam.departmentId = allData.classList[0].id

    }
  })
}
const toSearch = () => {
  getTopInfo()
  getValue()
  getTableInfo()
}
interface Topic {
  topicType: string
  topicId: number
  fullMarks: number
  topicNumber: string
  classDifficulty: number
  gradeDifficulty:number
  classDistinction:number
  gradeDistinction:number
  classAvgScore:number
  gradeAvgScore:number
  classStandardDeviation:number
  gradeStandardDeviation:number
  correctCount:number
  correctOptions:string
  wrongAnswerVOList:WrongAnswerVOList[]
}
interface WrongAnswerVOList {
  options:string
  count:number
  gradeCount:number
  gradeWrongAnswerRate:number
  classWrongAnswerRate:number
  optionCount:string

}
interface SpanMethodProps {
  row: Topic
  column: TableColumnCtx<Topic>
  rowIndex: number
  columnIndex: number
}
const objectSpanMethod = ({
                            row,
                            column,
                            rowIndex,
                            columnIndex,
                          }: SpanMethodProps) => {

  if (columnIndex>10){
    return { rowspan: 1, colspan: 1 };
  }




}
const getTableInfo = () => {
  selectTopicInfo(pageParam).then(function (res:any) {
    allData.objectiveQuestionsList = []
    allData.subjectiveQuestions = []
    let newData = res.data
    for (let i = 0; i < newData.length; i++) {

      if(newData[i].topicType=='1'){
        allData.subjectiveQuestions.push(newData[i])
      }
      if (newData[i].topicType!='1'){
        allData.objectiveQuestionsList.push(newData[i])
      }
    }
    console.log(allData.objectiveQuestionsList)
  })
}
const getValue = () => {
  selectMiddleInfo(pageParam).then(function (res:any) {
    allData.midInfo = res.data
  })
}
const getTopInfo = () => {
  selectTopInfo(pageParam).then(function (res: any) {
    if (res.code === 200) {

      allData.firstInfo = res.data
    }
  })
}
//获取所有项目
const getAllProjectItem = () => {
  let list = {
    roleId: allData.roleId
  }
  selectAllProject(list).then(function (res: any) {
    if (res.code === 200) {
      allData.projectList = res.data

      pageParam.projectId = allData.projectList[0].id
      getAllSubject()
      getAllClass()
    }
  })
}
const roleInfo = roleItemInfo()
watch(() =>
        roleInfo.currentRoleId,
    (currentRoleId) => {
      if (currentRoleId === null || currentRoleId === undefined || currentRoleId === "") {
        return
      }
      allData.roleId = currentRoleId
      //要执行的方法
      getAllProjectItem()
    }, {immediate: true, deep: true})
</script>



这个如何修改?

  • 写回答

1条回答 默认 最新

  • 一轮明月照丘壑 2024-09-14 12:04
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    要将后端数据在 Element Plus 的 el-table 中展示,你需要确保你的数据结构符合表格的需求,并使用合适的 v-for 和 v-bind 指令将数据绑定到表格上。根据你给出的数据结构和需求,我可以为你提供一个修改后的示例。

    首先,确保你的数据结构是正确的,并且已经包含了所有需要展示的数据。然后,你可以使用 v-for 指令在表格中循环遍历数据。对于每个字段,使用 v-bind 指令绑定到表格的列上。对于嵌套数据,你可能需要使用嵌套的 v-for 指令或计算属性来处理。

    以下是一个修改后的示例代码:

    <template>
      <!-- 其他代码省略 -->
      <div>
        <!-- 客观题分析表格 -->
        <div class="table-container">
          <el-table :data="allData.objectiveQuestionsList" border stripe default-expand-all>
            <!-- 题号列 -->
            <el-table-column prop="topicNumber" label="题号" align="center"></el-table-column>
            <!-- 满分列 -->
            <el-table-column prop="fullMarks" label="满分" align="center"></el-table-column>
            <!-- 难度列,包含班级和年级 -->
            <el-table-column label="难度">
              <template v-slot="scope">
                <!-- 班级难度 -->
                <el-table-column prop="classDifficulty" label="班级难度" align="center"></el-table-column>
                <!-- 年级难度 -->
                <el-table-column prop="gradeDifficulty" label="年级难度" align="center"></el-table-column>
              </template>
            </el-table-column>
            <!-- 区分度列,包含班级和年级 -->
            <el-table-column label="区分度">
              <template v-slot="scope">
                <!-- 班级区分度 -->
                <el-table-column prop="classDistinction" label="班级区分度" align="center"></el-table-column>
                <!-- 年级区分度 -->
                <el-table-column prop="gradeDistinction" label="年级区分度" align="center"></el-table-column>
              </template>
            </el-table-column>
            <!-- 标准差列,包含班级和年级 -->
            <el-table-column label="标准差">
              <template v-slot="scope">
                <!-- 班级标准差 -->
                <el-table-column prop="classStandardDeviation" label="班级标准差" align="center"></el-table-column>
                <!-- 年级标准差 -->
                <el-table-column prop="gradeStandardDeviation" label="年级标准差" align="center"></el-table-column>
              </template>
            </el-table-column>
            <!-- 平均分列,包含班级和年级 -->
            <el-table-column label="平均分">
              <template v-slot="scope">
                <!-- 班级平均分 -->
                <el-table-column prop="classAvgScore" label="班级平均分" :formatter="formatScore" align="center"></el-table-column>
                <!-- 年级平均分 -->
                <el-table-column prop="gradeAvgScore" label="年级平均分" :formatter="formatScore" align="center"></el-table-column>
              </template>
            </el-table-column>
            <!-- 答对人数列 -->
            <el-table-column prop="optionCount" label="答对人数" align="center"></el-table-column>
            <!-- 答错人数列 -->
            <el-table-column prop="wrongAnswerCount" label="答错人数" align="center"></el-table-column>
            <!-- 错选率列,包含班级和年级 -->
            <el-table-column label="错选率">
              <template v-slot="scope">
                <!-- 班级错选率 -->
                <div>{{ formatWrongRate(scope.row.wrongAnswerVOList[0].classAvgScore) }}</div> <!-- 这里假设每个班级的错选率只有一个数据 -->
                <!-- 年级错选率 -->
                <div>{{ formatWrongRate(scope.row.wrongAnswerVOList[0].gradeAvgScore) }}</div> <!-- 同上 -->
              </template>
            </el-table-column> <!-- 其他列可以根据需要继续添加 -->
          </el-table> <!-- 结束表格标签 --> <!-- 其他代码省略 --> </div> <!-- 结束容器标签 --> </template> <!-- 结束模板标签 --> <script setup lang="ts"> // 其他代码省略 // 添加格式化函数 formatScore 和 formatWrongRate functions formatScore(score) { return score ? score.toFixed(2) : '未知'; } formatWrongRate(score) { return score ? `${score}%` : '未知'; } </script> ``` 在这个示例中,我添加了两个格式化函数 `formatScore` 和 `formatWrongRate` 来处理分数的显示格式。你需要根据你的实际需求调整这些函数和数据结构。同时,注意我在嵌套数据列中使用了嵌套的 `v-for` 指令或计算属性来处理嵌套的数据结构。希望这个示例能帮助你实现你的需求。如果有其他问题,请随时提问。
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月24日
  • 创建了问题 9月14日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见