当前位置:首页 > 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组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="w…

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tabl…

vue 表格实现

vue 表格实现

Vue 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDa…

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格数…

vue实现表格多行修改

vue实现表格多行修改

实现表格多行修改的方法 数据绑定与表格渲染 使用v-model绑定表格数据到Vue实例的data属性。通过v-for循环渲染表格行,并为每行数据添加编辑状态标识。 <template>…

vue日历表格实现

vue日历表格实现

实现Vue日历表格的基本步骤 使用Vue实现日历表格需要处理日期数据生成、渲染和交互逻辑。以下是核心实现方法: 安装依赖(可选) 若需复杂功能可引入date-fns或dayjs等日期库: npm…