Vue结合原生Table表头纵向排列

1.html代码

 <div
            style="padding: 10px"
            v-for="(item, index) in tableData"
            :key="index"
          >
            <table class="table table-bordered" cellspacing="1">
              <thead>
                <tr>
                  <td style="color: #990000; font-weight: 800; font-size: 14px">
                    字段名称
                  </td>
                  <td
                    style="color: #333333; font-weight: 800; font-size: 14px"
                    v-for="(cont, index) in item"
                    :key="index"
                    class="ellipsisCss"
                    :title="cont.lineName"
                  >
                    {{ cont.lineName }}
                  </td>
                </tr>
              </thead>
              <thead>
                <tr>
                  <td style="color: #990000; font-weight: 800; font-size: 14px">
                    字段定义
                  </td>
                  <td
                    class="ellipsisCss11"
                    v-for="(it, index) in item"
                    :key="index"
                    :title="it.lineDefinition"
                  >
                    {{ it.lineDefinition }}
                  </td>
                </tr>
              </thead>
              <thead>
                <tr>
                  <td style="color: #990000; font-weight: 800; font-size: 14px">
                    格式
                  </td>
                  <td v-for="(sd, index) in item" :key="index">
                    {{ sd.lineFormat }}
                  </td>
                </tr>
              </thead>
            </table>
          </div>

2.需求是一排12个 多余得往下拍

 this.tableData = res.data.map((item) => ({
              lineDefinition: item.lineDefinition,
              lineName: item.lineName,
              lineFormat: item.lineFormat,
            }));
            let arr = [...this.tableData];
            // 包含100个对象的数组
            let newArr = [];
            // 存放新数组的数组
            for (let i = 0; i < arr.length; i += 12) {
              // 如果i+8小于数组长度,执行以下代码
              let subArr = arr.slice(i, i + 12);
              let subLength = subArr.length;
              let sl = 12 - subLength;
              if (sl < 12) {
                for (let a = 0; a < sl; a++) {
                  subArr.push({
                    lineDefinition: "",
                    lineName: "",
                    lineFormat: "",
                  });
                }
              }
              newArr.push(subArr);
            }

            this.tableData = newArr;
})

3.样式(表头 内容多的省略号...,鼠标放上去显示全部)

table tr {
  width: 140px;
  // line-height: 200%;
}
table,
tr,
td {
  height: 40px;
  text-align: center;
  // width: 160px;
  border: 1px solid #ccc; /* 设置边框样式 */
}
table {
  border-collapse: collapse; /* 合并边框 */
  width: 100%; /* 设置表格宽度 */
  table-layout: fixed;
}
.ellipsisCss {
  // height: 150px;
  max-width: 160px; /* 设置单元格最大宽度 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid #ccc; /* 设置边框样式 */
  padding: 10px;
}
.ellipsisCss11 {
  // height: 150px;
  width: 130px; /* 设置单元格最大宽度 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid #ccc; /* 设置边框样式 */
  padding: 10px;
}

4.最后的效果(字段名称 字段定义省略号展示 鼠标放上去显示)

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue原生table表格表头固定,可以通过CSS的position属性和JS的scroll事件来实现。 首先,在table标签外面嵌套一个div容器,设置其样式为position: relative,用于容纳表格和表头。 然后,在表格表头的tr标签上添加一个ref属性,用于在JS获取该元素。 接下来,使用JS监听div容器的scroll事件,在事件通过ref获取到表头元素,并获取该元素的offsetTop和scrollTop属性值。 然后,判断scrollTop是否大于或等于offsetTop,如果是则添加一个css类或样式,将表头固定在顶部;如果不是,则移除该类或样式。 最后,将改变样式的操作放在一个debounce的函数,用于优化滚动事件的性能。 具体代码如下: <template> <div class="container" ref="container" @scroll="handleScroll"> <table class="table"> <thead> <tr ref="thead"> <th>表头1</th> <th>表头2</th> <th>表头3</th> </tr> </thead> <tbody> <tr> <td>内容1</td> <td>内容2</td> <td>内容3</td> </tr> ... </tbody> </table> </div> </template> <script> export default { methods: { handleScroll() { const container = this.$refs.container; const thead = this.$refs.thead; const offsetTop = thead.offsetTop; const scrollTop = container.scrollTop; if (scrollTop >= offsetTop) { thead.classList.add("fixed"); } else { thead.classList.remove("fixed"); } }, }, }; </script> <style> .container { position: relative; max-height: 300px; overflow-y: scroll; } .fixed { position: fixed; top: 0; left: 0; width: 100%; z-index: 999; } .table { width: 100%; } /* 省略其他样式 */ </style> 以上就是利用Vue原生实现table表格表头固定的方法。通过设置CSS样式和监听scroll事件,可以在滚动时使表头保持在页面顶部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值