当前位置:首页 > JavaScript

js实现动态表格

2026-01-30 15:03:23JavaScript

动态表格实现方法

创建HTML结构

在HTML中定义一个容器元素用于放置动态表格,通常使用<div>或直接使用<table>标签:

<div id="tableContainer"></div>
<!-- 或 -->
<table id="dynamicTable"></table>

基础表格生成

通过JavaScript动态创建表格元素并插入数据:

function createTable(data) {
  const table = document.createElement('table');
  const thead = document.createElement('thead');
  const tbody = document.createElement('tbody');

  // 创建表头
  const headerRow = document.createElement('tr');
  Object.keys(data[0]).forEach(key => {
    const th = document.createElement('th');
    th.textContent = key;
    headerRow.appendChild(th);
  });
  thead.appendChild(headerRow);

  // 创建表格内容
  data.forEach(item => {
    const row = document.createElement('tr');
    Object.values(item).forEach(value => {
      const td = document.createElement('td');
      td.textContent = value;
      row.appendChild(td);
    });
    tbody.appendChild(row);
  });

  table.appendChild(thead);
  table.appendChild(tbody);
  document.getElementById('tableContainer').appendChild(table);
}

数据绑定

准备数据并调用表格生成函数:

js实现动态表格

const sampleData = [
  { id: 1, name: 'Alice', age: 25 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Charlie', age: 35 }
];

createTable(sampleData);

动态更新功能

实现添加/删除行功能:

function addRow(tableId, rowData) {
  const table = document.getElementById(tableId);
  const tbody = table.querySelector('tbody') || table;
  const row = document.createElement('tr');

  Object.values(rowData).forEach(value => {
    const td = document.createElement('td');
    td.textContent = value;
    row.appendChild(td);
  });

  tbody.appendChild(row);
}

function deleteRow(tableId, rowIndex) {
  const table = document.getElementById(tableId);
  const rows = table.querySelectorAll('tbody tr');
  if (rows[rowIndex]) rows[rowIndex].remove();
}

高级功能实现

添加排序和过滤功能:

js实现动态表格

function sortTable(tableId, columnIndex, ascending = true) {
  const table = document.getElementById(tableId);
  const tbody = table.querySelector('tbody');
  const rows = Array.from(tbody.querySelectorAll('tr'));

  rows.sort((a, b) => {
    const aValue = a.cells[columnIndex].textContent;
    const bValue = b.cells[columnIndex].textContent;
    return ascending 
      ? aValue.localeCompare(bValue)
      : bValue.localeCompare(aValue);
  });

  rows.forEach(row => tbody.appendChild(row));
}

function filterTable(tableId, columnIndex, filterValue) {
  const table = document.getElementById(tableId);
  const rows = table.querySelectorAll('tbody tr');

  rows.forEach(row => {
    const cellValue = row.cells[columnIndex].textContent.toLowerCase();
    row.style.display = cellValue.includes(filterValue.toLowerCase()) 
      ? '' 
      : 'none';
  });
}

响应式设计

使表格适应不同屏幕尺寸:

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 8px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

@media screen and (max-width: 600px) {
  table {
    display: block;
    overflow-x: auto;
  }
}

使用现代框架(可选)

使用Vue.js实现更简洁的动态表格:

new Vue({
  el: '#app',
  data: {
    items: [
      { name: 'Item 1', price: 10 },
      { name: 'Item 2', price: 20 }
    ]
  },
  methods: {
    addItem() {
      this.items.push({ name: 'New Item', price: 0 });
    }
  }
});

对应HTML结构:

<div id="app">
  <table>
    <tr v-for="(item, index) in items" :key="index">
      <td>{{ item.name }}</td>
      <td>{{ item.price }}</td>
    </tr>
  </table>
  <button @click="addItem">Add Item</button>
</div>

标签: 表格动态
分享给朋友:

相关文章

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现导出表格

vue实现导出表格

Vue 中实现表格导出 使用 xlsx 库导出 Excel 安装 xlsx 库: npm install xlsx --save 在 Vue 组件中引入并使用: import XLSX from…

vue实现动态配置地址

vue实现动态配置地址

动态配置地址的实现方法 在Vue项目中实现动态配置地址可以通过环境变量、API请求或配置文件等方式完成。以下是几种常见方法: 使用环境变量 创建.env文件存储基础URL,不同环境使用不同文件:…

vue实现动态用户表格

vue实现动态用户表格

Vue 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

vue实现动态显示布局

vue实现动态显示布局

Vue 实现动态显示布局的方法 动态显示布局通常涉及根据数据或用户交互动态调整页面结构和内容。以下是几种常见的实现方式: 使用 v-if 和 v-show 指令 v-if 和 v-show 都可以控…

vue实现合并表格

vue实现合并表格

vue实现合并表格的方法 使用Vue实现表格合并功能可以通过多种方式完成,常见方法包括自定义渲染、利用第三方库或手动处理数据。以下是几种典型实现方案: 使用Element UI的table组件实现合…