vue table 固定首列和首行

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
    <style>
        table{border-collapse: collapse;}
        th,td{padding: 5px;text-align: center;border:1px solid #999;min-width: 100px;}
        th{background-color: #333;color: #fff;position: sticky;top:0px;}
        td:first-child{background-color: #333;color: #fff;position: sticky;left:0px;}
        th:first-child{position: sticky;left:0px;}
    </style>
    <script src="https://cdn.staticfile.org/vue/2.5.17-beta.0/vue.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded",function(){
            let t = new Vue({el:"#t"});
        });
    </script>
</head>

<body>
    <table id="t">
        <thead>
            <tr>
                <th v-for="(n,i) of 50">字段 {{i+1}}</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(n,i) of 100">
                <td v-for="(m,j) of 50">{{j+1}}</td>
            </tr>
        </tbody>
    </table>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue可以通过数据驱动方式动态生成table行和。首先,在Vue实例中定义一个数组,用来存储table的数据。然后,在模板中使用v-for指令循环该数组,生成table的行。例如: ``` <template> <div> <table> <tr> <th>姓名</th> <th>年龄</th> </tr> <tr v-for="item in dataList" :key="item.id"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> </tr> </table> <button @click="addRow">添加行</button> <button @click="addColumn">添加</button> </div> </template> <script> export default { data() { return { dataList: [ { id: 1, name: '张三', age: 20 }, { id: 2, name: '李四', age: 18 }, { id: 3, name: '王五', age: 22 }, ] } }, methods: { addRow() { // 在dataList数组中添加一行数据 this.dataList.push({ id: this.dataList.length + 1, name: '', age: '' }); }, addColumn() { // 在table的表头中添加一 let th = document.createElement('th'); th.textContent = '性别'; document.querySelector('tr:first-child').appendChild(th); // 根据dataList数组中的数据,给每一行添加一 let rows = document.querySelectorAll('tr:not(:first-child)'); rows.forEach(row => { let td = document.createElement('td'); td.textContent = ''; row.appendChild(td); }); } } } </script> ``` 在上述代码中,通过v-for指令循环遍历dataList数组,动态生成table的行。通过addRow方法,可以在dataList数组中添加一行数据,从而动态生成一行。通过addColumn方法,可以在table的表头中添加一,并且根据dataList数组的数据,给每一行添加一

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值