js前端,一个漂亮的自写表格table

1.写一个简单的table表格框架

 <table>
        <thead class="my-table">
          <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in tableData" :key="index">
            <td>{{ item.name }}</td>
            <td>{{ item.age }}</td>
            <td>{{ item.address }}</td>
          </tr>
        </tbody>
     </table>

table里的数据在data里返回,以下是实例数据

   tableData: [
          {
            name: "张三",
            age: 30,
            address: "北京市朝阳区"
          },
          {
            name: "李四",
            age: 28,
            address: "上海市浦东新区"
          },
          {
            name: "王五",
            age: 32,
            address: "广州市天河区"
          },
          {
            name: "赵六",
            age: 25,
            address: "深圳市福田区"
          },
          {
            name: "孙七",
            age: 27,
            address: "成都市武侯区"
          },
      ],

这样我们就拥有了一个没有样式的基础表格

2.为了使表格更好看,我实现了如下几步:

  1. 固定表头,滚动时保留表格上面的标题

将th的 position设置为sticky,固定首行,也就是top=0,为其设置为一个区分的颜色

  th {
    background-color: #3c7ca7;
    font-weight: bold;
    position: sticky;
    top: 0;
    background-color: rgb(110, 138, 163);
  }
  1. 将表格间的分割线去除
<table cellspacing="0"  >
	<--...table内容-->
</table>
  1. 修改表格背景颜色,字体等样式
    为了好看点还设置了隔行的颜色区分也就是 tr:nth-child(even)
 th, td {
    padding: 20px;
    text-align: center;
    font-family: '微软雅黑',"times","courier","arial";
    font-weight:"medium";
    //border: 1px solid #ccc;
    font-size: 20px;
    color: #ffffff;
  }
  th {
    background-color: #3c7ca7;
    font-weight: bold;
    position: sticky;
    top: 0;
    background-color: rgb(110, 138, 163);
  }
  tr:nth-child(even) {
    background-color: #3c7ca7;
  }
  1. 自动滚动效果
    首先为了滚动效果的顺利实现,给table类增加一个外框类table-container
    增加滑动框并隐藏
.table-container {
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch; 
  }
  .table-container::-webkit-scrollbar {
  display: none;
}

然后利用timer定时器和scrollTop 属性设置滚动效果,

mounted() {
     this.timerId = setInterval(() => {
      const tableContainer = this.$refs.tableContainer;
      const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight;
      tableContainer.scrollTop += rowHeight;
      if (tableContainer.scrollTop == lasttop) {
        tableContainer.scrollTop = 0;
      }else{
          lasttop=tableContainer.scrollTop
      }
    }, 2000);
    this.init();
  },
   beforeDestroy() {
    // 在组件销毁前清除 setInterval
    clearInterval(this.timerId);

},
由于上面的滚动效果不好,看起来很卡顿,下面优化了滚动效果,加入缓动

  mounted() {
     this.timerId = setInterval(() => {
      const tableContainer = this.$refs.tableContainer;
      const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight;
      tableContainer.scrollTop += rowHeight/30;
      if (tableContainer.scrollTop == lasttop) {
        tableContainer.scrollTop = 0;
      }else{
          lasttop=tableContainer.scrollTop
      }
    }, 30);
    this.init();
   // this.tableroll();
  },
  1. 最后实现效果
    表格最终效果

3.全部代码

<div class="table-container" ref="tableContainer">
      <table cellspacing="0"  >
        <thead class="my-table">
          <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in tableData" :key="index">
            <td>{{ item.name }}</td>
            <td>{{ item.age }}</td>
            <td>{{ item.address }}</td>
          </tr>
        </tbody>
     </table>
    </div>
   
export default {
  data() {
        tableData: [
          {
            name: "张三",
            age: 30,
            address: "北京市朝阳区"
          },
          {
            name: "李四",
            age: 28,
            address: "上海市浦东新区"
          },
          {
            name: "王五",
            age: 32,
            address: "广州市天河区.dfasfaweorfaiadsfasd"
          },
          {
            name: "赵六",
            age: 25,
            address: "深圳市福田区"
          },
          {
            name: "孙七",
            age: 27,
            address: "成都市武侯区"
          },
      ],
  },
  .table-container::-webkit-scrollbar {
  display: none;
}
.table-container {
    position: absolute;
    left: 80%;
    top:70%;
    height: 300px;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch; 
    background-color:rgba(18, 76, 117, 0.7);
    .my-table {
  }
  th, td {
    padding: 20px;
    text-align: center;
    font-family: '微软雅黑',"times","courier","arial";
    font-weight:"medium";
    //border: 1px solid #ccc;
    font-size: 20px;
    color: #ffffff;
  }
  th {
    background-color: #3c7ca7;
    font-weight: bold;
    position: sticky;
    top: 0;
    background-color: rgb(110, 138, 163);
  }
  tr:nth-child(even) {
    background-color: #3c7ca7;
  }
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个常见的前端任务。您可以使用 HTML 和 JavaScript 来创建一个表格,并使用 AJAX 或其他技术与后端进行通信。以下是一个简单的示例代码: HTML: ``` <table id="myTable"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Actions</th> </tr> </thead> <tbody> <!-- Table rows will be added dynamically with JavaScript --> </tbody> </table> <button id="addRowBtn">Add Row</button> ``` JavaScript: ``` // Define a variable to keep track of the next available ID let nextId = 1; // Add a click event listener to the "Add Row" button document.getElementById("addRowBtn").addEventListener("click", function() { // Get the table body element let tbody = document.querySelector("#myTable tbody"); // Create a new row element let row = document.createElement("tr"); // Add cells to the row let idCell = document.createElement("td"); idCell.textContent = nextId++; row.appendChild(idCell); let nameCell = document.createElement("td"); nameCell.innerHTML = '<input type="text">'; row.appendChild(nameCell); let ageCell = document.createElement("td"); ageCell.innerHTML = '<input type="number">'; row.appendChild(ageCell); let actionsCell = document.createElement("td"); actionsCell.innerHTML = '<button class="editBtn">Edit</button> <button class="deleteBtn">Delete</button>'; row.appendChild(actionsCell); // Add the row to the table tbody.appendChild(row); // Add click event listeners to the edit and delete buttons row.querySelector(".editBtn").addEventListener("click", function() { // TODO: Implement edit functionality }); row.querySelector(".deleteBtn").addEventListener("click", function() { // TODO: Implement delete functionality }); }); ``` 这段代码创建了一个包含 ID、姓名、年龄和操作的表格,并添加了一个“添加行”按钮。每次单击该按钮时,它将在表格中添加一行,并为每个单元格添加适当的元素和事件监听器。您可以根据需要添加其他功能,例如编辑和删除行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值