Handsontable example

by handsoncode

HTML

<script src="https://cdn.jsdelivr.net/npm/handsontable@14.4/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/handsontable@14.4/dist/handsontable.full.min.css" /> 
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@handsontable/vue3@14.4/dist/vue-handsontable.min.js"></script>
<script src="https://handsontable.com/docs/scripts/fixer.js"></script>

<div id="example1">
  <hot-table 
       ref="tableRef"
      :settings="hotSettings"
      :col-headers="colHeaders"
      :columns="rowColumns">
      </hot-table>
</div>

Babel + JSX

import { createApp, defineComponent, onMounted, ref } from 'vue';
import { HotTable } from '@handsontable/vue3';
import { registerAllModules } from 'handsontable/registry';
import 'handsontable/dist/handsontable.full.css';
import { HyperFormula } from 'hyperformula';

// register Handsontable's modules
registerAllModules();

const hyperformulaInstance = HyperFormula.buildEmpty({
  licenseKey: 'internal-use-in-handsontable'
})

const colHeaders = ref(['测试1', '测试2', '测试3']);
const rowColumns = ref([
  { data: 'test1', type: 'text' },
  { data: 'test2', type: 'text' },
  { data: 'test3', type: 'text' }
]);

const tableRef = ref();

const getHot = () => {
  return tableRef.value?.hotInstance;
};

const ExampleComponent = defineComponent({
  components: {
    HotTable,
  },
  setup() {
    onMounted(() => {
      console.log('data', getHot()?.getSourceData());
      for (let i = 0; i < getHot()?.getSourceData().length; i++) {
        getHot().setDataAtCell(i, 2, '=1+1');
      }
    });

    return {
      tableRef,
      colHeaders,
      rowColumns,
      hotSettings: {
       	data: [],
        formulas: {
          engine: hyperformulaInstance,
          sheetName: 'Sheet1',
        },
        minRows: 1,
        height: 'auto',
        autoWrapRow: true,
        autoWrapCol: true,
        licenseKey: 'non-commercial-and-evaluation'
      }
    };
  }
});

const app = createApp(ExampleComponent);
app.mount('#example1');