hansontable在vue中的基本使用教程

 更新时间:2022年10月27日 11:22:29   作者:远方除了遥远一无所有  
handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作,这篇文章主要介绍了hansontable在vue中的基本使用,需要的朋友可以参考下

简介

Vue Handsontable是一个具有Spreadsheet外观的Vue数据表格组件,是Handsontable的官方封装。 Handsontable易于与任何数据源集成,并具有许多有用的功能,如数据绑定、验证、排序和强大的上下文菜单。

特点

  • 多列排序
  • 非连续选择
  • 过滤数据
  • 导出文件
  • 验证数据
  • 条件格式
  • 合并单元格
  • 自定义单元格类型
  • 冻结行/列
  • 移动行/列
  • 调整大小行/列
  • 隐藏行/列
  • 上下文菜单
  • 注释
  • 自动填充选项

handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作

代码

Test.vue

<template>
  <div id="hansontable">
    <hot-table
      :data="data"
      :settings="hotSettings"
      ref="hotTableRef"
    ></hot-table>
  </div>
</template>

<script>
import Handsontable from 'handsontable'
import { HotTable } from '@handsontable/vue'
import 'handsontable/dist/handsontable.full.css'
import { registerAllModules } from 'handsontable/registry'

// register Handsontable's modules
registerAllModules()

import hotSettings from './hotSettings'

export default {
  components: {
    HotTable,
  },
  data() {
    return {
      // data: Handsontable.helper.createSpreadsheetData(10, 7),
      data: [
        { car: 'Tesla', year: 2017, chassis: 'black', bumper: 'black' },
        { car: 'Nissan', year: 2018, chassis: 'blue', bumper: 'blue' },
        { car: 'Chrysler', year: 2019, chassis: 'yellow', bumper: 'black' },
        { car: 'Volvo', year: 2020, chassis: 'white', bumper: 'gray' },
      ],
      hotSettings,
      hotInstance: null,
    }
  },
  mounted() {
    // 获取实例
    this.hotInstance = this.$refs.hotTableRef.hotInstance
    const getDataAtRowProp = this.hotInstance.getDataAtRowProp
    // 示例:只允许单元格值为2019的数据进行更改
    this.hotInstance.updateSettings({
      cells(row, col, prop) {
        const cellProperties = {}
        console.log(row, prop)
        if (getDataAtRowProp(row, prop) == 2019) {
          cellProperties.editor = false
        } else {
          cellProperties.editor = 'text'
        }
        return cellProperties
      },
    })
  },
}
</script>

<!-- 注意:这里不能加"scoped",否则表头的背景颜色无法设置 -->
<style>
.make-me-red {
  color: red;
}
.custom-table thead th {
  background-color: #d7f1e1;
}
</style>

hotSettings.js

import Handsontable from 'handsontable'

Handsontable.renderers.registerRenderer(
  'negativeValueRenderer',
  negativeValueRenderer
)

function negativeValueRenderer(
  instance,
  td,
  row,
  col,
  prop,
  value,
  cellProperties
) {
  Handsontable.renderers.TextRenderer.apply(this, arguments)

  // 示例1:如果单元格的值小于10,则添加类名
  if (parseInt(value, 10) < 0) {
    td.className = 'make-me-red'
  }

  // 如果单元格的值为空或者没值
  if (!value || value === '') {
    td.style.background = '#EEE'
  } else {
    if (value === 'Nissan') {
      td.style.fontStyle = 'italic'
    }

    td.style.background = ''
  }
}

function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
  Handsontable.renderers.TextRenderer.apply(this, arguments)
  td.style.fontWeight = 'bold'
  td.style.color = 'green'
  td.style.background = 'orange'
}

const hotSetting = {
  licenseKey: 'non-commercial-and-evaluation',
  // colHeaders: true,
  // 列合并
  //    注意:在第一列的表头是不能合并的
  // nestedHeaders: [
  //   ['Car', { label: 'Year', colspan: 5 }, 'Chassis color', 'Bumper color'],
  //   [
  //     'Country',
  //     { label: 'City', colspan: 3 },
  //     'Address',
  //     'Zip code',
  //     'MobileH',
  //   ],
  // ],
  // 列名
  colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
  // rowHeaders: true,
  rowHeights: 40,
  width: '100%',
  // height: 'auto',
  height: 400,
  // 是否可以手动调整列大小
  manualColumnResize: true,
  // 将所有的列平均拉伸到父容器的宽度
  stretchH: 'all',
  // 右键下拉菜单
  // dropdownMenu: true,
  filters: true,
  dropdownMenu: ['filter_by_condition', 'filter_by_value', 'filter_action_bar'],
  // 列排序
  columnSorting: true,
  // 单元格的合并
  mergeCells: [{ row: 0, col: 0, rowspan: 2, colspan: 2 }],
  // 设置单元格的水平和垂直居中,并为表格添加自定义的类名
  className: 'htCenter htMiddle custom-table',
  // 单元格样式设置
  cells(row, col) {
    const cellProperties = {}

    // 对第一行设置样式,注意:不包括表头
    //     方式1: 直接通过函数
    //     方式2: 字符串,通过hansontable的map映射使用渲染器
    if (row === 0) {
      cellProperties.renderer = firstRowRenderer // uses function directly
    } else {
      cellProperties.renderer = 'negativeValueRenderer'
    }
    return cellProperties
  },
  // 是否只读
  // readOnly: true,
}

export default hotSetting

效果图

参考文档

https://juejin.cn/post/7062875824730406919

https://www.cnblogs.com/my-secret-base/p/13390054.html

https://www.jianshu.com/p/924481947c30

到此这篇关于hansontable在vue中的基本使用的文章就介绍到这了,更多相关vue  hansontable使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue3中使用fetch实现数据请求的过程详解

    Vue3中使用fetch实现数据请求的过程详解

    在现代前端开发中,数据请求是一个不可或缺的环节,而在Vue3中,我们有许多方法可以进行数据请求,其中使用fetch方法是一个非常常见的选择,本文将详细讲解如何在Vue3中使用fetch来实现数据请求,需要的朋友可以参考下
    2024-09-09
  • Vue绑定内联样式问题

    Vue绑定内联样式问题

    这篇文章主要介绍了Vue绑定内联样式的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-10-10
  • vue3中实现使用element-plus调用message

    vue3中实现使用element-plus调用message

    这篇文章主要介绍了vue3中实现使用element-plus调用message,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • vue 实现网页截图功能详解

    vue 实现网页截图功能详解

    这篇文章主要介绍了通过vue实现网页截图的功能,有兴趣的童鞋可以了解一下
    2021-11-11
  • vue中vue-router的使用说明(包括在ssr中的使用)

    vue中vue-router的使用说明(包括在ssr中的使用)

    这篇文章主要介绍了vue中vue-router的使用说明(包括在ssr中的使用),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • 关于vue的element-ui web端引入高德地图并获取经纬度

    关于vue的element-ui web端引入高德地图并获取经纬度

    这篇文章主要介绍了关于vue的element-ui web端引入高德地图并获取经纬度,高德地图首先要去申请key和密钥,文中提供了部分实现代码和解决思路,感兴趣的朋友可以学习一下
    2023-04-04
  • 浅析Vue中defineProps的解构和不解构

    浅析Vue中defineProps的解构和不解构

    defineProps 是 Vue 3 Composition API 中用来声明组件接收的 props 的方法,本文主要为大家介绍了defineProps的解构和不解构,感兴趣的可以了解下
    2025-02-02
  • ElementUI中el-tabs事件绑定的具体使用

    ElementUI中el-tabs事件绑定的具体使用

    本文主要介绍了ElementUI中el-tabs事件绑定的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Vue3之列表动画和状态动画示例详解

    Vue3之列表动画和状态动画示例详解

    这篇文章主要为大家介绍了Vue3之列表动画和状态动画示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • vue项目实现一键网站换肤效果实例(webpack-theme-color-replacer的使用)

    vue项目实现一键网站换肤效果实例(webpack-theme-color-replacer的使用)

    换皮肤一般都是点击一个按钮弹出一些皮肤的选项,选中选项后皮肤生效,下面这篇文章主要给大家介绍了关于vue项目实现一键网站换肤效果的相关资料,文中主要介绍的是webpack-theme-color-replacer的使用,需要的朋友可以参考下
    2023-02-02

最新评论