当前位置:首页 > JavaScript

js实现表格在线制作

2026-04-04 17:18:51JavaScript

使用原生JavaScript实现表格在线制作

通过DOM操作创建可编辑表格结构

const table = document.createElement('table');
table.id = 'editableTable';
table.style.border = '1px solid black';
document.body.appendChild(table);

添加表头生成功能

function addHeader() {
  const thead = table.createTHead();
  const row = thead.insertRow();
  const headers = ['姓名', '年龄', '操作'];

  headers.forEach(text => {
    const th = document.createElement('th');
    th.textContent = text;
    row.appendChild(th);
  });
}

实现动态添加行功能

function addRow() {
  const tbody = table.querySelector('tbody') || table.createTBody();
  const row = tbody.insertRow();

  ['name', 'age'].forEach(type => {
    const cell = row.insertCell();
    const input = document.createElement('input');
    input.type = type === 'age' ? 'number' : 'text';
    cell.appendChild(input);
  });

  const actionCell = row.insertCell();
  const delBtn = document.createElement('button');
  delBtn.textContent = '删除';
  delBtn.onclick = () => tbody.removeChild(row);
  actionCell.appendChild(delBtn);
}

使用HTML5的contenteditable属性实现即时编辑

创建可编辑表格结构

<table id="editableTable" border="1">
  <thead>
    <tr>
      <th contenteditable="true">列1</th>
      <th contenteditable="true">列2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td contenteditable="true"></td>
      <td contenteditable="true"></td>
    </tr>
  </tbody>
</table>
<button onclick="addEditableRow()">添加行</button>

实现动态行添加

function addEditableRow() {
  const tbody = document.querySelector('#editableTable tbody');
  const newRow = document.createElement('tr');

  for(let i=0; i<2; i++) {
    const td = document.createElement('td');
    td.contentEditable = true;
    newRow.appendChild(td);
  }

  tbody.appendChild(newRow);
}

使用第三方库Handsontable实现专业表格

引入Handsontable库

<link href="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.js"></script>

<div id="hot"></div>

初始化表格实例

const container = document.getElementById('hot');
const hot = new Handsontable(container, {
  data: [['', '']],
  colHeaders: true,
  rowHeaders: true,
  contextMenu: true,
  height: 300,
  width: 600,
  licenseKey: 'non-commercial-and-evaluation'
});

实现表格数据导出功能

添加导出为CSV功能

function exportToCSV() {
  let csv = [];
  const rows = document.querySelectorAll('#editableTable tr');

  rows.forEach(row => {
    const rowData = [];
    row.querySelectorAll('th, td').forEach(cell => {
      rowData.push(cell.textContent);
    });
    csv.push(rowData.join(','));
  });

  const csvContent = csv.join('\n');
  const blob = new Blob([csvContent], {type: 'text/csv'});
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = 'table_data.csv';
  link.click();
}

实现表格样式定制

添加CSS样式控制

js实现表格在线制作

#editableTable {
  border-collapse: collapse;
  width: 100%;
}

#editableTable th, #editableTable td {
  border: 1px solid #ddd;
  padding: 8px;
  min-width: 100px;
}

#editableTable th {
  background-color: #f2f2f2;
  text-align: left;
}

#editableTable tr:nth-child(even) {
  background-color: #f9f9f9;
}

标签: 在线表格
分享给朋友:

相关文章

css按钮在线制作

css按钮在线制作

CSS按钮在线制作工具 以下是一些可用于在线制作CSS按钮的工具和资源,帮助快速生成美观且响应式的按钮样式。 Button Generator Button Generator提供可视化编辑界面,可…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

vue实现表格界面

vue实现表格界面

Vue 实现表格界面的方法 使用 Element UI 的表格组件 Element UI 提供了功能丰富的表格组件,适合快速开发表格界面。安装 Element UI 后,可以直接使用 el-table…

在线jquery

在线jquery

jQuery CDN 链接 以下是常用的 jQuery CDN(内容分发网络)链接,可直接在 HTML 文件中引入: jQuery 3.x(最新稳定版) <script src="ht…

vue实现表格穿梭

vue实现表格穿梭

Vue实现表格穿梭功能 表格穿梭功能通常指在两个表格之间进行数据转移,常见于权限管理、数据筛选等场景。以下是基于Vue的实现方法: 核心实现思路 创建两个表格组件,分别代表源数据和目标数据 使用v-…