Vue + el-table中html返显 + title悬浮显示 + 超长打点

本文介绍了如何在Vue的el-table中,结合template和v-html指令,实现单元格内容的动态title显示,并通过row-class-name和cell-mouse-enter事件,根据单元格的property值动态绑定title属性。还提及了如何使用CSS解决内容过长时的省略显示。
摘要由CSDN通过智能技术生成

0. 整体代码:

【效果】:
在这里插入图片描述
【栗子代码】:

<template>
  <div>
    <el-table @cell-mouse-enter="hoverDate" :row-class-name="tableRowClassName" :data="tableData">
      <el-table-column align="center" label="序号">
        <template v-slot="scope">
          <div :title="scope.row.index+1" v-html="scope.row.index+1" class="title-tips"></div>
        </template>
      </el-table-column>
      <el-table-column prop="keyName" label="名称" min-width="200">
        <template v-slot="scope">
          <div :title="scope.row.keyName" v-html="scope.row.keyName" class="title-tips"></div>
        </template>
      </el-table-column>
      <el-table-column prop="valueByte" label="值" min-width="350">
        <template v-slot="scope">
          <div :id="'valueByte'.concat(scope.$index)" v-html="scope.row.valueByte" class="title-tips"></div>
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="描述" min-width="150">
        <template v-slot="scope">
          <div :id="'desc'.concat(scope.$index)" v-html="scope.row.desc" class="title-tips"></div>
        </template>
      </el-table-column>
      <el-table-column prop="userId" label="最后更新人" min-width="100">
        <template v-slot="scope">
          <div :title="scope.row.userId" v-html="scope.row.userId" class="title-tips"></div>
        </template>
      </el-table-column>
      <el-table-column prop="cTime" label="最后更新时间" min-width="120">
        <template v-slot="scope">
          <div :id="'cTime'.concat(scope.$index)" v-html="scope.row.cTime" class="title-tips"></div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "SideOne",
  methods: {
    // 将行索引设置到row里面
    tableRowClassName ({ row, rowIndex }) {
      row.index = rowIndex
    },
    // 鼠标悬浮显示title
    hoverDate: function (row, column) {
      let id
      if (column.property === "cTime") {
        id = "cTime".concat(row.index)
      } else if (column.property === "valueByte") {
        id = "valueByte".concat(row.index)
      } else if (column.property === "desc") {
        id = "desc".concat(row.index)
      } else {
        return
      }

      let titleVule = document.getElementById(id).textContent
      if (typeof titleVule === 'undefined' || titleVule === null) {
        titleVule = ""
      }
      document.getElementById(id).setAttribute("title", titleVule)
    }
  },
  data: function () {
    return {
      tableData: [
        {
          keyName: "com.test.keyname",
          valueByte: "&lt;noscript&gt;&#xa;&lt;strong&gt;We&#x27;re sorry but vue dosen&#x27;t work &lt;&#x2f;strong&gt;",
          desc: "&lt;div&gt;desc&lt;&#x2f;div&gt;",
          userId: "zhangsan",
          cTime: "2021-08-21&#x3a;22&#x3a;22&#x3a;22"
        }
      ]
    }
  }
}
</script>

<style scoped>
.title-tips {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
</style>

1. html代码反显:

 el-table-column中html代码片段反显,可以使用template+ v-html指令来实现,如:

<el-table-column prop="desc" label="描述" min-width="150">
  <template v-slot="scope">
    <div :id="'desc'.concat(scope.$index)" v-html="scope.row.desc" class="title-tips"></div>
  </template>
</el-table-column>

2. title显示:

 el-table-column可以使用:show-overflow-tooltip="true"来进行单元格内容超长title显示,如:

    <el-table-column prop="desc" label="描述" min-width="150" :show-overflow-tooltip="true">
        <template v-slot="scope">
          <div v-html="scope.row.desc" class="title-tips"></div>
        </template>
      </el-table-column>

 但这只对内容超长时才显示,要想不分内容长短都title显示,就想到document中的title属性
·
 所以栗子代码中使用document来动态设置title属性,要点如下:

  • 通过:row-class-name="tableRowClassName"将行索引设置到row里面
  • 通过 :id="‘valueByte’.concat(scope.$index)"与行索引构成动态绑定id
  • 通过@cell-mouse-enter=“hoverDate” 鼠标进入单元格,触发方法动态绑定,通过id来设置title属性
    // 鼠标悬浮显示title
    hoverDate: function (row, column) {
      let id
      if (column.property === "cTime") {
        id = "cTime".concat(row.index)
      } else if (column.property === "valueByte") {
        id = "valueByte".concat(row.index)
      } else if (column.property === "desc") {
        id = "desc".concat(row.index)
      } else {
        return
      }

      let titleVule = document.getElementById(id).textContent
      if (typeof titleVule === 'undefined' || titleVule === null) {
        titleVule = ""
      }
      document.getElementById(id).setAttribute("title", titleVule)
    }

3. 内容超长打点:

 单元格内容超长时,使用…显示,只要设置如下css样式:

.title-tips {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
您可以通过在 `el-table` 使用 `el-input-number` 组件来实现购物车功能。以下是一个简单的示例代码: 首先,需要在组件引入所需的组件: ```vue <template> <div> <el-table :data="cartData" style="width: 100%"> <el-table-column prop="name" label="商品名称"></el-table-column> <el-table-column prop="quantity" label="数量"> <template slot-scope="scope"> <el-input-number v-model="scope.row.quantity" @change="updateTotalPrice"></el-input-number> </template> </el-table-column> <el-table-column prop="price" label="单价"></el-table-column> <el-table-column prop="totalPrice" label="总价"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { cartData: [ { name: '商品1', quantity: 1, price: 10, totalPrice: 10 }, { name: '商品2', quantity: 2, price: 20, totalPrice: 40 }, { name: '商品3', quantity: 3, price: 30, totalPrice: 90 } ] }; }, methods: { updateTotalPrice() { this.cartData.forEach(item => { item.totalPrice = item.quantity * item.price; }); } } }; </script> ``` 在上述代码,`cartData` 数组是购物车的数据源,包含商品名称、数量、单价和总价等信息。在 `el-table` 的 `el-table-column` ,使用 `el-input-number` 组件来编辑商品的数量,并通过 `v-model` 绑定到 `cartData` 数组的 `quantity` 属性上。当数量发生变化时,通过 `@change` 事件触发 `updateTotalPrice` 方法,更新对应商品的总价。 注意:以上代码仅为示例,实际应用,您可能需要从后端动态获取购物车数据,并与后端进行交互来更新购物车信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值