找回密码
 立即注册

QQ登录

只需一步,快速开始

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证

超级版主

123

主题

8927

帖子

1万

积分

超级版主

Rank: 8Rank: 8

积分
13532

讲师达人悬赏达人元老葡萄SpreadJS 认证SpreadJS 高级认证微信认证勋章

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-9-17 16:23  /   查看:3947  /  回复:12
本帖最后由 dexteryao 于 2021-9-23 10:49 编辑

SpreadJS自定义单元格功能提供了强大的拓展能力,一方面拓展数据展示的效果,比如checkbox,Radio button的形式展示数据。另一方面,当单元格进入编辑状态可以使用下拉菜单等任何输入控件来进行输入。

关于自定义单元格的内容可以参考学习指南-自定义单元格以及API文档
由于框架生命周期以及自定义单元格渲染逻辑的问题,目前无法直接在框架页面下直接通过template的方式使用框架下的组件。

经过了一些调研,VUE支持动态渲染的方式来创建和挂载组件,考虑可以通过这种方式在自定义单元格中注入组件。
下面使用Element UI中autocomplete为例,演示如何在VUE 2 的项目中给SpreadJS创建一个使用VUE 组件的自定义单元格。
这种方式是否有副作用还不清楚,使用时请详细测试,大家也可以跟帖讨论,一起完善方案。
Demo中数据时mock的,实际使用中可以参考combo celltype 通过items传入。

首先,需要在项目中开启运行时加载,在vue.config.js中添加runtimeCompiler: true(更新:可以参考https://gcdn.grapecity.com.cn/showtopic-131931-1-1.html 不设置此选项)
  1. module.exports = {
  2.     devServer: {
  3.         port: 3000
  4.     },
  5.     <font color="#ff0000">runtimeCompiler: true</font>
  6.   }
复制代码
引用ElementUI,这里要注意要把element 的css引用放在APP import前,这样组件中对于样式的修改,才能覆盖原有项目
  1. import Vue from 'vue'
  2. import ElementUI from 'element-ui';
  3. import 'element-ui/lib/theme-chalk/index.css';
  4. import App from './App.vue'
  5. import router from './router'

  6. Vue.use(ElementUI);

  7. new Vue({
  8.   el: '#app',
  9.   router,
  10.   render: h => h(App)
  11. })

  12. Vue.config.productionTip = false
复制代码
创建AutoComplateCellType,具体代码如下,需要注意几点。
1. 对于自定义的元素,需要添加gcUIElement属性,如果元素或者其父元素没有该属性,点击创建的组件便会直接退出编辑状态无法编辑。
对于ElementUI 的autocomplete,默认下拉选项内容是注入到body中的,这就导致上面说问题,这里需要给组件模板中设置:popper-append-to-body="false",让弹出的下拉选项在gcUIElement的Div中渲染。
如果使用其他组件没有类似选项,也可以跟进实际情况在弹出时在添加gcUIElement属性。
2.  使用动态挂载组件的 this.vm  设置和获取 单元格的值。
3. 在deactivateEditor中销毁组件

  1. import Vue from 'vue'
  2. import * as GC from "@grapecity/spread-sheets"
  3. import DataService from './dataService'

  4. function AutoComplateCellType() {
  5. }
  6. AutoComplateCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  7. AutoComplateCellType.prototype.createEditorElement = function (context, cellWrapperElement) {
  8.   cellWrapperElement.style.overflow = 'visible'
  9.   let editorContext = document.createElement("div")
  10.   editorContext.setAttribute("gcUIElement", "gcEditingInput");
  11.   let editor = document.createElement("div");
  12.   // 自定义单元格中editorContext作为容器,需要在创建一个child用于挂载,不能直接挂载到editorContext上
  13.   editorContext.appendChild(editor);
  14.   return editorContext;
  15. }
  16. AutoComplateCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  17.     let width = cellRect.width > 180 ? cellRect.width : 180;
  18.     if (editorContext) {
  19.         
  20.         // 动态创建VUE 组件并挂载到editor
  21.         const AutoCompleteComponent = {
  22.             props: ['text','cellStyle'],
  23.             template: `<div>
  24.                         <el-autocomplete
  25.                         :style="cellStyle"
  26.                         popper-class="my-autocomplete"
  27.                         v-model="text"
  28.                         :fetch-suggestions="querySearch"
  29.                         placeholder="请输入内容"
  30.                         :popper-append-to-body="false"
  31.                         value-key="name"
  32.                         @select="handleSelect">
  33.                         <i class="el-icon-edit el-input__icon"
  34.                             slot="suffix"
  35.                             @click="handleIconClick">
  36.                         </i>
  37.                         <template slot-scope="{ item }">
  38.                             <div class="name">{{ item.name }}</div>
  39.                             <span class="addr">{{ item.phone }}</span>
  40.                         </template>
  41.                         </el-autocomplete>
  42.                     </div>`,
  43.             mounted() {
  44.                 this.items = DataService.getEmployeesData();
  45.             },
  46.             methods: {
  47.                 querySearch(queryString, cb) {
  48.                     var items = this.items;
  49.                     var results = queryString ? items.filter(this.createFilter(queryString)) : items;
  50.                     // 无法设置动态内容的位置,可以动态添加gcUIElement
  51.                     // setTimeout(() => {
  52.                     //   let popDiv = document.getElementsByClassName("my-autocomplete")[0];
  53.                     //   if(popDiv){
  54.                     //     popDiv.setAttribute("gcUIElement", "gcEditingInput");
  55.                     //   }
  56.                     // }, 500);
  57.                     // 调用 callback 返回建议列表的数据
  58.                     cb(results);
  59.                 },
  60.                 createFilter(queryString) {
  61.                     return (restaurant) => {
  62.                     return (restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
  63.                     };
  64.                 },
  65.                 handleSelect(item) {
  66.                     console.log(item);
  67.                 },
  68.                 handleIconClick(ev) {
  69.                     console.log(ev);
  70.                 }
  71.             }
  72.         };

  73.       // create component constructor
  74.       const AutoCompleteCtor = Vue.extend(AutoCompleteComponent);
  75.       this.vm = new AutoCompleteCtor({
  76.         propsData: {
  77.           cellStyle: {width: width+"px"}
  78.         }
  79.       }).$mount(editorContext.firstChild);
  80.     }
  81.     return editorContext;
  82. };
  83. AutoComplateCellType.prototype.updateEditor = function(editorContext, cellStyle, cellRect) {
  84.     // 给定一个最小编辑区域大小
  85.     let width = cellRect.width > 180 ? cellRect.width : 180;
  86.     let height = cellRect.height > 40 ? cellRect.height : 40;
  87.     return {width: width, height: height};
  88. };
  89. AutoComplateCellType.prototype.getEditorValue = function (editorContext) {
  90.     // 设置组件默认值
  91.     if (this.vm) {
  92.         return this.vm.text;
  93.     }
  94. };
  95. AutoComplateCellType.prototype.setEditorValue = function (editorContext, value) {
  96.     // 获取组件编辑后的值
  97.     if (editorContext) {
  98.       this.vm.text = value;
  99.     }
  100. };
  101. AutoComplateCellType.prototype.deactivateEditor = function (editorContext, context) {
  102.     // 销毁组件
  103.     this.vm.$destroy();
  104.     this.vm = undefined;
  105. };


  106. export {AutoComplateCellType};
复制代码

最后,按照一般使用单元格类型的方式设置即可
  1. import {AutoComplateCellType} from "../static/autocomplateCellType"
复制代码
  1. var sheet = spread.getActiveSheet();
  2.         sheet.getCell(1, 1).value("A").backColor("lightblue")
  3.         sheet.setCellType(1, 1, new AutoComplateCellType())
复制代码




效果如图:
image.png326746825.png

12 个回复

倒序浏览
小鱼
注册会员   /  发表于:2021-10-11 15:49:38
沙发
React  的 autocomplete  或者其他组件有相似例子吗? 这个input框里可以加前缀icon吗
回复 使用道具 举报
小鱼
注册会员   /  发表于:2021-10-11 16:24:38
板凳
查了一下ElementUI,  里面没有找到 <el-atuocomplete>组件,我搜的2.14.1 版本
回复 使用道具 举报
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-10-11 16:55:17
地板
小鱼 发表于 2021-10-11 16:24
查了一下ElementUI,  里面没有找到 组件,我搜的2.14.1 版本

请看这个例子
image.png668992919.png

https://element.eleme.cn/2.14/#/zh-CN/component/input
回复 使用道具 举报
小鱼
注册会员   /  发表于:2021-10-13 09:53:06
5#
hi,我自己定义了一个组件,和你的一样,react 内 用了antd的 dropdown,但是下拉框不能从单元格出来,隐藏到里面了,有什么建议吗
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-10-13 10:19:36
6#
小鱼 发表于 2021-10-13 09:53
hi,我自己定义了一个组件,和你的一样,react 内 用了antd的 dropdown,但是下拉框不能从单元格出来,隐藏 ...

DOM里看看是那个元素overflow设置了hidden,改一下,可以参考下面代码
  1. AutoComplateCellType.prototype.createEditorElement = function (context, cellWrapperElement) {
  2.   cellWrapperElement.style.overflow = 'visible'
复制代码
回复 使用道具 举报
梅梅梅梅
中级会员   /  发表于:2022-7-22 17:20:12
7#
您好,我用的版本9,为什么new GC.Spread.Sheets.CellTypes.Base();这个方法报错,显示没有CellTypes方法?
image.png410888366.png
回复 使用道具 举报
Lynn.Dou讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-7-22 18:17:59
8#
注意到关于此问题您另开了新帖,后续在新帖中交流:
https://gcdn.grapecity.com.cn/fo ... read&tid=151077
回复 使用道具 举报
sanyue
初级会员   /  发表于:2023-5-31 19:55:44
9#
这样写element-ui autoconplete自身的样式都失效了是为什么? 8b9a77124dbcaa8b751348a87fbcdc6.jpg343937682.png 变成这样了
回复 使用道具 举报
BattleHawk76
注册会员   /  发表于:2023-11-21 21:39:43
10#
这个自定义的化每次都要写一个js方法吗?有没有更方便的方法?直接通过vue的h函数渲染上去.能够将这行的数据暴露出来让组件能够操作数据
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部