主页

handsontable contextMenu 在表格下方插入新的一行

2025-08-25 04:37PM

Handsontable 的 Context Menu(右键菜单) 功能允许你自定义右键时显示的选项,从而执行特定操作

例如:在表格下方插入一个带有年份的新行

import React, { useRef } from 'react';
import Handsontable from 'handsontable';
import 'handsontable/dist/handsontable.full.css';
import 'handsontable-context-menu/dist/handsontable-contextMenu.min.css';
import { HotTable } from '@handsontable/react';

const App = () => {
  const hotTableRef = useRef(null);

  const data = [
    ['', 'Ford', 'Volvo', 'Toyota', 'Honda'],
    ['2019', 10, 11, 12, 13],
    ['2020', 20, 11, 14, 13],
    ['2021', 30, 15, 12, 13],
  ];

  const contextMenu = {
    items: {
      insertYear: {
        name: '插入年份',
        callback: () => {
          const currentYear = new Date().getFullYear();
          const newRow = [currentYear, '', '', '', ''];
          const hot = hotTableRef.current.hotInstance;

          hot.alter('insert_row', hot.countRows());
          hot.setDataAtRow(hot.countRows() - 1, newRow);
        },
      },
    },
  };

  return (
    <div>
      <HotTable
        ref={hotTableRef}
        data={data}
        colHeaders={true}
        rowHeaders={true}
        contextMenu={contextMenu}
        licenseKey="non-commercial-and-evaluation"
      />
    </div>
  );
};

export default App;

 

 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论