找回密码
 立即注册

QQ登录

只需一步,快速开始

Derrick.Jiao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-1-27 13:45  /   查看:2999  /  回复:2
本帖最后由 dexteryao 于 2021-2-3 17:10 编辑

本教程教大家如何通过点击ribbon的按钮,实现弹出对话框并且自定义对话框的CheckBox、Radio。

首先,我们需要创建一个模板实例,创建一个名为”打印“的dialog页
  1. var richSheetTagTemplate = {
  2.     title: "自定义打印",
  3.       content: [
  4.         {
  5.           type: "TabControl",
  6.           width: 670,
  7.           height: 530,
  8.           bindingPath: "dialogOption",
  9.           children: [
  10.             {
  11.               key: "Test2",
  12.               text: "打印",
  13.               children: [
  14.                 CreateId
  15.               ]
  16.             },
  17.           ]
  18.         }
  19.       ]
  20. }
复制代码


然后,我们通过API GC.Spread.Sheets.Designer.registerTemplate()注册该模板
  1. GC.Spread.Sheets.Designer.registerTemplate("newTab", richSheetTagTemplate);
复制代码


我们可以看到模板中有一个children为CreateID,这个就是对话框中,显示的内容。我们用了Flex、Column布局。并且写了对应的bindingPath,会在命令中用到。
  1. var CreateId = {
  2.   type: "FlexContainer",
  3.   margin: "10px",
  4.   children: [
  5.     {
  6.       type: "FlexContainer",
  7.       children: [
  8.         {
  9.           type: "Radio",
  10.           items: [
  11.             {
  12.               text: '横向打印',
  13.               value: 'uuid'
  14.             },
  15.             {
  16.               text: '纵向打印',
  17.               value: 'rowColId',
  18.             }],
  19.           bindingPath: "type"
  20.         },
  21.         {
  22.           type: 'FlexContainer',
  23.           enableWhen: "type=uuid",
  24.           margin: '10px 0',
  25.           children: [
  26.             {
  27.               type: 'ColumnSet',
  28.               children: [
  29.                 {
  30.                   type: 'Column',
  31.                   children: [
  32.                     {
  33.                       type: "CheckBox",
  34.                       text: '显示边框',
  35.                       bindingPath: 'row'
  36.                     }
  37.                   ],
  38.                   width: '120px'
  39.                 },
  40.                 {
  41.                   type: 'Column',
  42.                   children: [
  43.                     {
  44.                       type: "CheckBox",
  45.                       text: '显示网格线',
  46.                       bindingPath: 'col'
  47.                     }
  48.                   ],
  49.                   width: '120px'
  50.                 }
  51.               ]
  52.             }
  53.           ]
  54.         },
  55.         {
  56.           type: 'FlexContainer',
  57.           enableWhen: "type=rowColId",
  58.           margin: '10px 0',
  59.           children: [
  60.             {
  61.               type: 'ColumnSet',
  62.               children: [
  63.                 {
  64.                   type: 'Column',
  65.                   children: [
  66.                     {
  67.                       type: "CheckBox",
  68.                       text: '显示行头',
  69.                       bindingPath: 'cellId'
  70.                     }
  71.                   ],
  72.                   width: '120px'
  73.                 },
  74.                 {
  75.                   type: 'Column',
  76.                   children: [
  77.                     {
  78.                       type: "CheckBox",
  79.                       text: '显示列头',
  80.                       bindingPath: 'colClassName'
  81.                     }
  82.                   ],
  83.                   width: '150px'
  84.                 }
  85.               ]
  86.             }
  87.           ]
  88.         }
  89.       ]
  90.     },

  91.   ]
  92. }
复制代码


我们还需要注册"newTab"的命令,将我们要处理的逻辑写进去。我们可以将result打印出来,可以看到与我们的bindingPath的值对应绑定。

  1. var ribbonTabCommands = {

  2.     "newTab": {
  3.         text: "页面设置",
  4.         iconClass: "ribbon-button-sheetgeneral",
  5.         bigButton: true,
  6.         commandName: "newTab",
  7.         execute: async (context) => {

  8.             var spread = context.getWorkbook();
  9.             var sheet = spread.getActiveSheet();
  10.             var sheetIndex = spread.getSheetIndex(sheet.name())

  11.              var option = {
  12.                 tag: sheet.tag(),
  13.                 type: "rowColId",
  14.                 row: true,
  15. //                colClassName: false,
  16.                 dialogOption: {
  17.                     activeTab: "Test2",
  18.                     showTabList: ["DefaultFormat","Test2"],
  19.                 },
  20.             }
  21.             GC.Spread.Sheets.Designer.showDialog("newTab", option, (result) => {
  22.                 var printInfo = sheet.printInfo();
  23.                 console.log(result)
  24.                 console.log(result.type)
  25.                 if(result.type === "uuid"){
  26.                     printInfo.orientation(GC.Spread.Sheets.Print.PrintPageOrientation.landscape);


  27.                      if(result.row === true){
  28.                            printInfo.showBorder(true);
  29.                            printInfo.showGridLine(false);
  30.                      }

  31.                      if(result.col === true){

  32.                            if(result.row === false){
  33.                                 printInfo.showBorder(false);
  34.                            }
  35.                           printInfo.showGridLine(true);
  36.                      }
  37.                      spread.print();
  38.                 }

  39.                 if(result.cellId === false){

  40.                     printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);

  41.                     if(result.colClassName === false){
  42.                         printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
  43.                     }

  44.                      if(result.colClassName === true){
  45.                         printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.show);
  46.                      }

  47.                     spread.print();
  48.                 }

  49.                 if(result.cellId === true){

  50.                      printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.show);

  51.                      if(result.colClassName === true){
  52.                         printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.show);
  53.                      }
  54.                      if(result.colClassName === false){
  55.                         printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
  56.                      }
  57.                      spread.print();
  58.                 }
  59.         }
  60.     }
  61. }
复制代码


注册对应的ribbon
  1. var ribbonPivotConfig1 = {
  2.     "label": "打印",
  3.     "thumbnailClass": "ribbon-thumbnail-spreadsettings",
  4.     "commandGroup": {
  5.         "children": [
  6.             {
  7.                 "direction": "vertical",
  8.                 "commands": ["newTab"]
  9.             }
  10.         ]
  11.     }
  12. }
复制代码


找到对应ribbon的buttonGroups将其push进去。
  1. layoutRibbon.buttonGroups.push(ribbonPivotConfig1);
复制代码
实现效果如图
image.png200066222.png

2 个回复

倒序浏览
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-5-18 09:29:14
推荐
yu30 发表于 2021-5-18 09:25
亲  有demo嘛?我本地试过,引用相关js,按照官网文档使用的,还是不行。

您可以参考这个前后端结合的demo

spreadjs-world-main.zip

3.6 MB, 下载次数: 67

回复 使用道具 举报
yu30
注册会员   /  发表于:2021-5-18 09:25:35
沙发
亲  有demo嘛?我本地试过,引用相关js,按照官网文档使用的,还是不行。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部