Handsontable vue如何实现在线编辑excal

官网地址:https://handsontable.com/

1.实现效果

2.安装

import { HotTable } from ‘@handsontable/vue‘

import Handsontable from ‘handsontable‘

3.页面引用

<template>
           <hot-table
                  ref="tableServer"
                  :data="hotSettings.dataList"                          // excal中的数据源
                  :context-menu="hotSettings.contextMenu"               // 右击鼠标可进行的操作
                  :col-headers="hotSettings.colHeaders"                 // 表格头部标题
                  :start-rows="hotSettings.startRows"                   // 默认固定行数
                  :start-cols="hotSettings.startCols"                   // 默认固定列数
                  :row-headers="hotSettings.rowHeaders"                 // 默认显示表格行头部
                  :row-heights="hotSettings.rowHeights"                 // 默认设置行高度
                  :manual-row-resize="hotSettings.manualRowResize"      //
                  :manual-column-resize="hotSettings.manualColumnResize"
                  :manual-row-move="hotSettings.manualRowMove"
                  :columns="hotSettings.columns"
                  :before-remove-row="hotSettings.beforeRemoveRow"          // 删除行之前触发的事件
                  :after-create-row="hotSettings.afterCreateRow"            // 添加行后触发的事件                   //一定要记住所有的事件、数值一定要绑定在标签上才可使用,只在js的data中绑定是不可以的
                  :after-change="hotSettings.afterChange"                   // 新增行,动态改变值触发的事件
                  :id="`hotTableAll${$route.params.id}`"
                  class="table_hot"
                  license-key="non-commercial-and-evaluation"
                />
              </el-form-item>
              <el-button class="primary-button uploadButton"
                         size="small"
                         type="primary"
                         icon="el-icon-upload"
                         @click="uploadFile">导出</el-button>
</template>
<script>
import addressProvince from ‘@/components/addressProvince‘
import { HotTable } from ‘@handsontable/vue‘
import Handsontable from ‘handsontable‘
import { Message } from ‘element-ui‘import priceList from ‘@/utils/priceList.js‘

// 如果excal中有值为空,可拦截const validatorList = (rule, value, callback) => {
  for (let i = 0; i < value.length; i++) {
    if (!value[i].region || !value[i].destination) {
      callback(new Error(‘请输入价格表中所有信息, 多余的可通过鼠标右击进行删除行‘))
      return
    }
  }
  callback()
}
const riseWeightVal = function (value, callback) {
  if (/^\d+(?=\.{0,1}\d+$|$)/.test(value) || value === ‘‘) {
    callback(true)
  } else {
    Message.error({
      message: ‘请输入数字‘
    })
    callback(false)
  }
}

export default {
  components: {
    addressProvince,
    HotTable
  },
  data() {
    return {
      addressList: [{
        area: []
      }],
      hotSettings: {
        dataList: [],  //
        colHeaders: [‘区域‘, ‘目的地‘, ‘首重 kg‘, ‘首重价格‘, ‘续重 kg‘, ‘续重价格‘],
        startRows: 8,
        startCols: 6,
        rowHeaders: true,
        rowHeights: 40,
        manualRowResize: true,
        manualColumnResize: true,
        manualRowMove: true,
        customBorders: true,
        columns: [      //data: dataList数组中对应的 键,readOnly: 只读, type: 属于什么类型 例:text, numeric, validator: 数据验证
          { data: ‘region‘, readOnly: false, type: ‘text‘ },
          { data: ‘destination‘, readOnly: false, type: ‘text‘ },
          { data: ‘firstWeight‘, readOnly: false, validator: riseWeightVal, allowInvalid: false },
          { data: ‘firstWeightPrice‘, readOnly: false, validator: riseWeightVal, allowInvalid: false },
          { data: ‘riseWeight‘, readOnly: false, validator: riseWeightVal, allowInvalid: false },
          { data: ‘riseWeightPrice‘, readOnly: false, validator: riseWeightVal, allowInvalid: false }
        ],
        beforeRemoveRow: this.beforeRemoveRowMe,
        afterCreateRow: this.afterCreateRowMe,
        afterChange: this.afterChangeMe,
        contextMenu: {
          items: {
            ‘row_above‘: {
              name: ‘在此行上方插入行‘
            },
            ‘row_below‘: {
              name: ‘在此行下方插入行‘
            },
            ‘separator‘: Handsontable.plugins.ContextMenu.SEPARATOR,
            ‘copy‘: {
              name: ‘复制‘
            },
            ‘undo‘: {
              name: ‘撤消‘
            },
            ‘separator1‘: Handsontable.plugins.ContextMenu.SEPARATOR,
            ‘remove_row‘: {
              name: ‘删除行‘
            },
            ‘clear_column‘: {
              name: ‘删除列‘
            },
            ‘clear_custom‘: {
              name: ‘删除所有单元格‘,
              callback: function () {
                this.clear()
              }
            }
          }
        }
      },
      expressCompanyList: [],
      dateRange: [],
      typeBol: false,
      searchParam: {
        supplierServiceRegionPriceVos: []
      },
      searchParamRules: {
        supplierServiceRegionPriceVos: [
          { type: ‘array‘, required: true, message: ‘请填写价格表‘, trigger: ‘blur‘ },
          { validator: validatorList, required: true, message: ‘请输入价格表中所有信息, 多余的可通过鼠标右击进行删除行‘, trigger: ‘blur‘ }
        ]
      },
      showContent: false,
      loadShow: false,
      saveLimit: false
    }
  },

  mounted() {   this.hotSettings.dataList = this._.cloneDeep(priceList)
  },
  methods: {
    // 删除行之前调用
    beforeRemoveRowMe: function (changes, source) { // 数据改变时触发此方法
      this.hotSettings.dataList.splice(changes, source)
    },
    // 添加行
    afterCreateRowMe: function (changes) {
      this.hotSettings.dataList.splice(changes, 0, {
        region: ‘‘,
        destination: ‘‘,
        firstWeight: ‘‘,
        firstWeightPrice: ‘‘,
        riseWeight: ‘‘,
        riseWeightPrice: ‘‘
      })
    },
    // 新增行时,动态改变值
    afterChangeMe: function (changes) {
      if (changes) {
        changes.forEach(([row, prop, oldValue, newValue]) => {
          console.log(oldValue)
          this.hotSettings.dataList[row][prop] = newValue
        })
      }
    },
    // 查看-excel不可编辑
    excalEdit() {
      this.hotSettings.columns.forEach(par => {
        par.readOnly = true
      })
    },
    definedShow() {
      this.showContent = true
    },
    saveSubmit() {
      // 防止点击多次触发
      if (this.saveLimit) return
    conso.log()
      const that = this
      that.searchParam.supplierServiceRegionPriceVos = that.hotSettings.dataList
      const temp = that._.cloneDeep(this.searchParam)
      that.$refs.searchForm.validate((valid) => {
        if (valid) {      console.log(this.hotSettings.dataList)
          that.saveLimit = true
        }
      })
    },
    // 导出文件
    uploadFile() {
      // 可查看网址https://handsontable.com/docs/7.1.0/ExportFile.html
      const container = this.$refs.tableServer.hotInstance
      const exportPlugin = container.getPlugin(‘exportFile‘)
      exportPlugin.downloadFile(‘csv‘, {
        bom: ‘UTF-8‘, // 允许您使用BOM签名导出数据。
        columnDelimiter: ‘,‘, // 允许您定义列分隔符。
        columnHeaders: false, // 允许使用列标题导出数据。
        exportHiddenColumns: true, // 允许您从隐藏列导出数据。
        exportHiddenRows: true, // 允许您从隐藏行导出数据
        fileExtension: ‘csv‘, // 允许您定义文件扩展名。
        filename: ‘供应商服务价格表[YYYY]-[MM]-[DD]‘, // 允许您定义文件名。
        mimeType: ‘text/csv‘, // 允许您定义MIME类型。
        rowDelimiter: ‘\r\n‘, // 允许您定义行分隔符。
        rowHeaders: true // 允许您使用行标题导出数据。
      })
    }
  }
}
</script>

  

  

原文地址:https://www.cnblogs.com/gqx-html/p/11268383.html

时间: 2024-08-30 18:09:38

Handsontable vue如何实现在线编辑excal的相关文章

shell——bash在线编辑

无意中发现了一个bash在线编辑的网址,初学者可以用这个试着学习shell http://www.runoob.com/try/runcode.php?filename=helloworld&type=bash

ArcGIS JavaScript在线编辑

代码: <html> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <title>Demo:Edit Features</title>     <link rel="stylesheet" href="https://js.arcgis.com/3.17

常见的web在线编辑工具

         在线编辑工具      JSbin http://jsbin.com/优点:不需要登录,即发即预览缺点:速度不靠谱 RUN JShttp://runjs.cn/code优点:国内访问速度快,可以上传图片(左下角)缺点:需要登录,用户名

Node.js 切近实战(九) 之Excel在线(在线编辑)

最近实在是太想去西藏了,我自己总是喜欢人少的旅游地,喜欢一望无垠,喜欢蓝天白云大草原. 之前有一节我给大家讲过文件列表,如下,今天我们要讲的就是Excel在线编辑. 当我们双击文件图标的时候会跳转到一个Excel修改界面,如下. ok,这里我们使用的依然是Telerik Kendo UI中的SpreadSheet,看一下这个Spread Sheet是如何用的. 我们定义一个spreadsheet的div,我们看一下这个div怎么生成sheet. $("#spreadsheet").ke

【网页在线编辑】图文发送的模式

1.需求 网页在线编辑第三方插件很多,我需要做一个手机上发布图片+文字的精简版的编辑器,文字和图片就自上而下排列就完了. iframe的实现架构很多. 2.实现 2.1 iframe定义 2.2 编辑模式设置和焦点获取 ifEdit = this.getElementByXid("ifEdit").contentWindow;                //编辑模式        ifEdit.document.designMode = "on";       

NET中weboffice组件在线编辑文档并保存到服务器上

页面中组件的引用以及控件触发事件: <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="9" background="images/tab_12.gif"> <asp:Button ID="btnuploadsave"

java 在线编辑模版 代码编辑器 兼容手机平板PC freemaker 静态引擎 网站源码

java 企业网站源码 前后台都有 静态模版引擎, 代码生成器大大提高开发效率 前台: 支持三套模版, 可以在后台切换 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的html,所以访问速度快,轻便,对服务器负担小 3.网站前端采用主流的响应式布局,同一页面同时支持PC.平板.手机(三合一)浏览器访问 4.springmvc +spring4.2.5+ mybaits3.3  SSM 普通java we

openlayers实现在线编辑

概述: 在前面有篇博文讲述了基于Arcgis for js和wkt实现在线数据的采集和编辑功能,在本文讲述如何在openlayers实现类似的功能.上一篇博文的地址为: http://blog.csdn.net/gisshixisheng/article/details/44310765 思路: 前后台的数据交互以wkt的形式,加载已完成对象用wkt,对象更新完成之后将geometry转换为wkt传给后台,将信息保存到数据库中.实现在线编辑主要为OpenLayers.Control.Modify

Jcrop ,.net仿百度上传在线编辑头像

(源码下载链接: http://pan.baidu.com/s/1pJVxMAn 密码: gbuz)  个人习惯,代码不贴出来,自己去云盘下. 我们先看一下百度上传头像的页面.如下图. 再 看看我做的效果. 图1 图2 上面是我做出的效果,灰色是一个400X400的背景,图片会自动适应 (图1和图2 一个横向一个是纵向),宽度和高度始终能放入这个背景中. 介绍一下使用的技术,Jquery, Jcrop ,ajaxfileupload.js . 我们先看一下百度上传头像的页面.如下图. 再 看看我