原生table表格设置

<table
      width="100%"
      border="1"
      cellspacing="0"
      cellpadding="0"
      id="exportExcel"
    >
      <thead>
        <tr class="tac" style="margin-bottom:10px;font-size: 22px">
          <th colspan="11">租赁物列表</th>
        </tr>
        <tr>
          <th>日期(租赁时间-归还时间)</th>
          <th>分类</th>
          <th>已租数量</th>
          <th>归还数量</th>
          <th>损坏数量</th>
          <th>丢失数量</th>
          <th>超时数量</th>
          <th>租赁时长</th>
          <th>赔偿金</th>
          <th>单价</th>
          <th>合计</th>
        </tr>
      </thead>
      <tbody colspan="11">
        <tr v-for="(item,index) in tableData" :key="index">
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
            <td>{{item.RealUnitPrice}}</td>
            <td>{{item.Deposit}}</td>
        </tr>
      </tbody>
    </table>

先定义表格头部,thead,tr表示行,colspan合并列,则表头合并了11列,表头的合并数,和你展示的列数保持一致。tbody表示表格内容,td每个单元格的值。tr循环数据td进行展示。

效果如下:

原生设计复杂表格的时候,可以使用template,代码如下:

    <table
      width="100%"
      border="1"
      cellspacing="0"
      cellpadding="0"
      id="exportExcel"
      v-show="!show"
    >
      <thead>
        <tr class="tac" style="margin-bottom:10px;font-size: 22px">
          <th colspan="6">线上部分</th>
          <th colspan="6">线下部分</th>
        </tr>
      </thead>
      <tr>
        <!-- 线上 -->
        <th style="width: 25%;" colspan="3">
          <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr class="fwb" style="background-color:rgb(207, 215, 231) ;">
              <th colspan="8">营业收入</th>
            </tr>
            <tr class="fwb" style="background-color:rgb(220, 216, 216) ;">
              <th colspan="4">票种</th>
              <th colspan="2">{{ $t(`thead.amount`) }}</th>
              <th colspan="2">订单数</th>
            </tr>
            <template v-for="item in UpProductStatistics">
              <tr
                v-if="
                  item.ItemName == '营业收入' || item.ItemName == '收款总计'
                "
                v-bind:key="item.Sort"
                style="background-color: rgb(242, 216, 137)"
              >
                <td colspan="4">
                  {{ item.ItemName }}
                </td>
                <!-- <td colspan="2">{{item.ItemCode}}</td> -->
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
                <td colspan="2">{{ item.Count }}</td>
              </tr>

              <tr v-bind:key="item.ItemName" v-else>
                <td colspan="4">{{ item.ItemName }}</td>
                <!-- <td colspan="2">{{item.ItemCode}}</td> -->
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
                <td colspan="2">{{ item.Count }}</td>
              </tr>
              <tr
                v-if="item.ItemName == '营业收入'"
                v-bind:key="item.Sort"
                style="background-color: rgb(207, 215, 231);"
              >
                <td colspan="8">
                  押金统计
                </td>
              </tr>
            </template>
          </table>
        </th>
        <th style="width: 25%;" colspan="3">
          <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr class="fwb" style="background-color:rgb(207, 215, 231) ;">
              <th colspan="6">收款统计</th>
            </tr>
            <tr class="fwb" style="background-color: rgb(220, 216, 216) ;">
              <th colspan="4">支付方式</th>
              <th colspan="2">{{ $t(`thead.amount`) }}</th>
            </tr>
            <template v-for="item in UpPayTypeStatistics">
              <tr
                v-if="item.PayTypeName == '收款总计'"
                v-bind:key="item.PayTypeName"
                style="background-color: rgb(242, 216, 137)"
              >
                <td colspan="4">{{ item.PayTypeName }}</td>
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
              </tr>
              <tr v-bind:key="item.PayTypeName" v-else>
                <td colspan="4">{{ item.PayTypeName }}</td>
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
              </tr>
            </template>
          </table>
        </th>
        <!-- 线下 -->
        <th style="width: 25%;" colspan="3">
          <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr class="fwb" style="background-color:rgb(207, 215, 231) ;">
              <th colspan="8">营业收入</th>
            </tr>
            <tr class="fwb" style="background-color:rgb(220, 216, 216) ;">
              <th colspan="4">票种</th>
              <th colspan="2">{{ $t(`thead.amount`) }}</th>
              <th colspan="2">订单数</th>
            </tr>
            <template v-for="item in DownProductStatistics">
              <tr
                v-if="
                  item.ItemName == '营业收入' || item.ItemName == '收款总计'
                "
                v-bind:key="item.Sort"
                style="background-color: rgb(242, 216, 137)"
              >
                <td colspan="4">
                  {{ item.ItemName }}
                </td>
                <!-- <td colspan="2">{{item.ItemCode}}</td> -->
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
                <td colspan="2">{{ item.Count }}</td>
              </tr>

              <tr v-bind:key="item.ItemName" v-else>
                <td colspan="4">{{ item.ItemName }}</td>
                <!-- <td colspan="2">{{item.ItemCode}}</td> -->
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
                <td colspan="2">{{ item.Count }}</td>
              </tr>
              <tr
                v-if="item.ItemName == '营业收入'"
                v-bind:key="item.Sort"
                style="background-color: rgb(207, 215, 231);"
              >
                <td colspan="8">
                  押金统计
                </td>
              </tr>
            </template>
          </table>
        </th>
        <th style="width: 25%;" colspan="3">
          <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr class="fwb" style="background-color:rgb(207, 215, 231) ;">
              <th colspan="6">收款统计</th>
            </tr>
            <tr class="fwb" style="background-color: rgb(220, 216, 216) ;">
              <th colspan="4">支付方式</th>
              <th colspan="2">{{ $t(`thead.amount`) }}</th>
            </tr>
            <template v-for="item in DownPayTypeStatistics">
              <tr
                v-if="item.PayTypeName == '收款总计'"
                v-bind:key="item.PayTypeName"
                style="background-color: rgb(242, 216, 137)"
              >
                <td colspan="4">{{ item.PayTypeName }}</td>
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
              </tr>
              <tr v-bind:key="item.PayTypeName" v-else>
                <td colspan="4">{{ item.PayTypeName }}</td>
                <td colspan="2" style="text-align:right">
                  {{ item.AmountStr }} <span style="display: none;">,</span>
                </td>
              </tr>
            </template>
          </table>
        </th>
      </tr>
    </table>

展示效果如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值