Element-Ui实现评分表,评分项,一个例子掌握table合并行和列,computed计算总分,某级分类的最大评分值,超详细!逐行注释。

 效果图如下,这是最近公司的一个需求,由于网上例子较少,所以在完善代码后决定发布出来供大家参考。

首先我们的数据库中是以id,pid形式存储的数据,最多分为四级评分项,在这个例子中不涉及到后台拼接数据的内容,只是前端编写的假数据进行展示,需要实现后端的小伙伴可以按照我的数据格式去用sql查询出对应的格式即可复用。

起初接到这个需求的时候很苦恼,因为没有写过类似的内容,给了一个excel,让通过页面展示出来,最终还要填写分数,校验并存入到数据库中,刚开始想着是通过树状table展示,可是这样的话离图中效果差距很大,估计也是不能接收,下面就看看我是如何实现的吧。

页面效果实现:

<template>
  <div class="pd_1 hasTableContainer">
    <el-table
      :data="testData"
      :span-method="objectSpanMethodS"
      border style="width: 100%"
      :header-cell-style="headerStyle">
      <el-table-column prop="levelTwoId" label="序号" align="center" width="100">
      </el-table-column>
      <el-table-column prop="levelOne" label="评估项目(一级)" align="center">
      </el-table-column>
      <el-table-column prop="levelTwo" label="评估项目(二级)" align="center">
      </el-table-column>
      <el-table-column prop="maxScore" label="最大分值" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.levelTwoId === '总分'">
            {
  
  {sum}}
            {
  
  {totalScore}}
          </div>
          <div v-if="scope.row.levelTwoId !== '总分'">
            {
  
  {scope.row.maxScore}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="levelThree" label="评分标准" align="center" width="100">
      </el-table-column>
      <el-table-column prop="levelFour" align="center">
      </el-table-column>
      <el-table-column prop="score" label="分数" align="center">
        <template slot-scope="scope">
          <el-input
            placeholder="分数"
            v-model="scope.row.score"
            :disabled="scope.row.levelThree === '不具备'"
            type="number"
            step="1"
            min="0"
            @input="scope.row.score = inputVerification(scope.row)"
          ></el-input>
  
Element-UItable 组件中,可以通过设置 `height` `max-height` 来实现表格的固定高度。同时,通过设置 `fixed` 属性可以实现表头固定。要同时实现第一固定,可以使用 `append` slot,将第一作为 table 的子组件插入到 table 中,并设置其样式为固定位置。 具体实现可以参考以下代码: ```html <template> <el-table :data="tableData" :height="tableHeight" max-height="500px" :header-cell-style="{ 'background-color': '#f5f7fa', 'color': '#333', 'font-weight': 'bold' }" :row-class-name="tableRowClassName" fixed > <template slot="append"> <el-table-column v-for="(item, index) in tableData[0]" :key="index" :label="index === 0 ? '' : item" :width="index === 0 ? '50px' : '100px'" :fixed="index === 0" class-name="first-column" > <template slot-scope="{ row }"> <div v-if="index === 0">{{ row[0] }}</div> <div v-else>{{ row[index] }}</div> </template> </el-table-column> </template> <el-table-column v-for="(item, index) in tableData[0]" :key="index" :prop="index === 0 ? 'id' : item" :label="index === 0 ? 'ID' : item" :width="index === 0 ? '50px' : '100px'" :fixed="index === 0" ></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ ['ID', 'Name', 'Age', 'Gender', 'Address'], ['1', 'John', '20', 'Male', 'New York'], ['2', 'Mary', '25', 'Female', 'Los Angeles'], ['3', 'Tom', '30', 'Male', 'Chicago'], ['4', 'Lucy', '35', 'Female', 'Houston'], ['5', 'David', '40', 'Male', 'Philadelphia'] ] } }, computed: { tableHeight() { const headerHeight = document.querySelector('.el-table__header-wrapper').offsetHeight const appendHeight = document.querySelector('.el-table__append-wrapper').offsetHeight return `calc(100vh - ${headerHeight + appendHeight}px)` } }, methods: { tableRowClassName({ rowIndex }) { if (rowIndex === 0) { return 'first-row' } } } } </script> <style scoped> .el-table__header-wrapper { position: sticky; top: 0; z-index: 1; } .el-table__append-wrapper { position: sticky; top: 40px; z-index: 1; } .first-row { position: sticky; top: 40px; z-index: 1; } .first-column { text-align: center; } </style> ``` 在上面的代码中,我们通过 `append` slot 将第一作为子组件插入到 table 中,并设置其样式为 `position: sticky; top: 40px;` 实现第一固定。同时,设置了表头第一的 `fixed` 属性,实现表头第一固定。为了使表格高度能够自适应页面高度,我们通过计算表头第一的高度,来动态设置表格的高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值