首页 > 其他分享 >hansontable在vue中的基本使用

hansontable在vue中的基本使用

时间:2022-10-27 11:11:57浏览次数:72  
标签:基本 vue cellProperties Handsontable color hansontable td true row

代码

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

标签:基本,vue,cellProperties,Handsontable,color,hansontable,td,true,row
From: https://www.cnblogs.com/it774274680/p/16831501.html

相关文章

  • 大数据的基本概念
    1.1什么是大数据IBM用3V(Volume、Variety、Velocity)来描述大数据所拥有的特点。大容量(Volume),是指数据体量巨大。多形式(Variety),是从数据的类型角度来看的,数据的存在形式......
  • 【数据结构-链表】链表的基本操作
    目录1单向链表1.1有头结点的单向链表1.1.1数据结构定义1.1.2初始化建立链表1.1.3按序号查找结点1.1.4按值查找结点1.1.5插入操作1.1.6删除操作1.2无头结点的单向......
  • vue之hello
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><metahttp-......
  • 1-turtle基本元素绘制
    1-画线段结果代码importturtle#引入绘图库p=turtle.Pen()#示例画笔p.forward(200)#画长度为200的线段,默认水平向右turtle.done()#画布窗体不......
  • GET和POST两种基本请求方法的区别
    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二。 最直观的区别就是GET把参数包含在URL中,POST通过requestbody传递参数。 你可能......
  • 8_vue是如何进行数据代理的
    在了解了关于js当中的Object.defineProperty()这个方法后,我们继续对vue当中的数据代理做一个基于现在的解析建议观看之前先了解下js当中的Obejct.defineProperty()链接地......
  • vue3-setup 的参数
    setup(props,context){}第一个参数:    props,是一个对象,包含父组件传递给子组件的所有数据。在子组件中使用props进行接收,包含配置声明并传入的所有的属性的......
  • Tauri-Vue3桌面端聊天室|tauri+vite3仿微信|tauri聊天程序EXE
    基于tauri+vue3.js+vite3跨桌面端仿微信聊天实例TauriVue3Chat。tauri-chat运用最新tauri+vue3+vite3+element-plus+v3layer等技术跨桌面端仿微信|QQ聊天程序EXE。基本实......
  • ABAP-基本语法
    *&---------------------------------------------------------------------**&ReportZ2*&*&------------------------------------------------------------------......
  • Vue的双向绑定 v-model的原理
    Vue的双向绑定v-model的原理使用V-model进行绑定v-model的效果就是用户在输入的时候实际上实在修改txtVal的值,修改成用户输入的内容<inputtype="text"v-model="txt......