安装
yarn add handsontable @handsontable/vue3
demo 组件示例
<template>
<div>
<hot-table width="100%" height="500" :settings="settings" ref="hotTableComponent"></hot-table>
</div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, reactive, toRefs } from 'vue';
import { HotTable } from '@handsontable/vue3';
import 'handsontable/dist/handsontable.full.css'; // 表格样式
import { registerAllModules } from 'handsontable/registry'; // 注册所有模块
import 'handsontable/languages/zh-CN'; // 汉语包
registerAllModules();
// 导入和注册插件和单元格类型
import { registerCellType, NumericCellType } from 'handsontable/cellTypes';
import { registerPlugin, UndoRedo } from 'handsontable/plugins';
registerCellType(NumericCellType);
registerPlugin(UndoRedo);
export default defineComponent({
components: { HotTable },
setup() {
const hotTableComponent = ref<InstanceType<typeof HotTable> | null>(null);
let hotInstance: any = null;
const state = reactive({
settings: {
language: 'zh-CN', // 官方汉化
licenseKey: 'non-commercial-and-evaluation', // 去除底部非商用声明
colHeaders: ["病人姓名", "病人身份证号", "病人类型"],
columns: [
{ data: "病人姓名", type: "text" },
{ data: "病人身份证号", type: "text" },
{ data: "病人类型", type: "text" }
],
data: [
{ "病人姓名": "测试", "病人身份证号": "123456789", "病人类型": "贫困户" },
{ "病人姓名": "张三", "病人身份证号": "369258147", "病人类型": "贫困户" }
],
currentRowClassName: 'currentRow', // 突出显示行
currentColClassName: 'currentCol', // 突出显示列
minSpareRows: 2, // 行留白
autoWrapRow: false, // 自动换行
minRows: 2100,
trimWhitespace: false, // 去除空格
rowHeaderWidth: 100, // 单元格宽度
stretchH: 'all',
renderAllRows: false,
rowHeaders: true, // 行标题
formulas: false, // 公式
copyable: true, // 允许键盘复制
wordWrap: false, // 自动换行
copyPaste: true, // 复制粘贴
filters: false, // 允许通过内置组件或 API 过滤表数据
search: false,
fixedColumnsLeft: 0, // 固定左边列数
fixedRowsTop: 0, // 固定上边列数
columnSorting: true, // 排序
contextMenu: true, // 右键菜单
}
});
// 在组件挂载后获取 handsontable 实例
onMounted(() => {
if (hotTableComponent.value) {
hotInstance = hotTableComponent.value.hotInstance;
console.log('Handsontable instance:', hotInstance);
// 现在你可以安全地调用 hotInstance 的方法了
// 例如获取源数据或数据
console.log('Source Data:', hotInstance.getSourceData());
console.log('Data:', hotInstance.getData());
}
});
return {
...toRefs(state),
hotTableComponent,
};
},
});
</script>
<style scoped>
/* 根据需要添加自定义样式 */
</style>
前端工程师、程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)