找回密码
 立即注册

QQ登录

只需一步,快速开始

Joestar.Xu SpreadJS 开发认证
超级版主   /  发表于:2024-4-29 12:12  /   查看:138  /  回复:0
部分同学在实现设计器的Ribbon时想要实现下图这样的下拉选中效果:


image.png500649413.png

那么要怎么实现类似的效果呢?

首先,我们先做一个简单的下拉Ribbon对象:

  1. designerConfig.ribbon.unshift({
  2.   id: "test",
  3.   text: "测试",
  4.   buttonGroups: [
  5.     {
  6.       label: "测试",
  7.       commandGroup: {
  8.         children: [
  9.           {
  10.             command: "parCommand",
  11.             type: "dropdown",
  12.             children: ["chilCommand1", "chilCommand2"],
  13.           },
  14.         ],
  15.       },
  16.     },
  17.   ],
  18. });
复制代码


这个结构中有三个Command,分别为parCommand、chilCommand1和chilCommand2。

接下来我们要在designerConfig.commandMap中分别实现这三个Command。

parCommand简单定义一下即可,无需添加特别的逻辑:

  1. parCommand: {
  2.   text: "父",
  3.   commandName: "parCommand",
  4.   execute: (context, propertyName) => {
  5.     console.log("execute", context, propertyName);
  6.   },
  7.   getState: (context, propertyName) => {
  8.     console.log("getState", context, propertyName);
  9.   },
  10. },
复制代码


重点在于chilCommand1和chilCommand2这两个Command:

  1. chilCommand1: {
  2.   text: "子1",
  3.   type: "checkbox",
  4.   commandName: "chilCommand1",
  5.   execute: (context, propertyName, checked) => {
  6.     let tag = context.getWorkbook().getActiveSheet().tag();
  7.     if (!tag) {
  8.       tag = {};
  9.     }
  10.     tag.child1 = !tag.child1;
  11.     context.getWorkbook().getActiveSheet().tag(tag);
  12.   },
  13.   getState: (context) => {
  14.     return context.getWorkbook().getActiveSheet().tag()?.child1;
  15.   },
  16. },
  17. chilCommand2: {
  18.   text: "子2",
  19.   type: "checkbox",
  20.   commandName: "chilCommand2",
  21.   execute: (context, propertyName, checked) => {
  22.     let tag = context.getWorkbook().getActiveSheet().tag();
  23.     if (!tag) {
  24.       tag = {};
  25.     }
  26.     tag.child2 = !tag.child2;
  27.     context.getWorkbook().getActiveSheet().tag(tag);
  28.   },
  29.   getState: (context) => {
  30.     return context.getWorkbook().getActiveSheet().tag()?.child2;
  31.   },
  32. },
复制代码


在每个Command中都有一个execute和getState函数,execute函数为点击时执行的函数,getState函数则为获取选中状态的函数。

上面的代码将选中状态存储在sheet的tag中,获取状态时也会从sheet的tag中获取。

具体代码请参考附件。

Ribbon下拉框带选中效果.js

2.42 KB, 下载次数: 5

SpreadJS 17.0.8 | GcExcel 7.1.1 已发布~

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部