[{"id":"29a4ab0f-edd1-45e1-87df-0b8b4bb068b3","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"64b61a79-5817-4746-b591-28d4d5462c0a","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"6edef660-641f-4cd8-84f6-571130f37ae2","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"89a3ee5c-06c4-49c4-ac61-8325ba4bcca1","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"1e898427-e706-440e-9077-b35c7a5b8f46","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"4266ec12-a87a-444b-a8af-beff39a7defe","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"fd867fe6-72e0-4e45-bbac-edc72e608b08","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"fad338cd-7c96-48b9-a12d-1b979ba886b2","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"6fb42249-b832-4656-9137-35d9948bea1c","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"6e353a93-c1d4-4387-bad5-4a07fd18ed3a","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"acbe820e-65b4-4bd6-bcf3-a9d5b16163da","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"167a076a-6a93-49f6-8bf8-adee9abd9817","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"7fb1e240-d39d-4391-986f-c2d40adfb011","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"09cc5e9f-99c9-4258-b69c-ca5bb09a6b16","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"0b4ea8fb-4e7b-4028-9393-d1177a68ec17","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"35b8ea08-9ca6-49eb-a021-0c806de4ff96","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"7bace066-1638-4034-82eb-0db8c15f5bd1","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"7ec5ea8e-419f-40aa-b67d-301fa3e7b025","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"38f78942-4ca0-4631-a2d8-32dcfd6157a6","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"fc1d5e2a-5d1c-4479-ac87-c726c03d49aa","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"dd70702b-48a7-4896-b92d-db78ff9e52b0","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"cda2a866-9746-4790-9942-13c450ece8a1","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"3239f53b-81be-4d67-ae63-7ca0d6ce0f91","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"74c2daa1-efb0-4d3e-bd0c-8d11b297c92d","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"a4268f67-693d-4933-9e22-a77d68880a13","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"9f17896b-55b4-4627-8b6d-b01fdabdffe7","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"e45c5458-c148-4de7-a471-e2e3808d841e","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"ac44f25f-d7e5-46a3-a247-33e23e20e956","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"feb10296-b21e-4506-8d7e-567ed4dda994","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"ebba2e23-2c8d-4948-8cca-91ce71b3183d","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"ebdc80a9-26fc-4420-af7f-e576e4f12820","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"2cb68b65-34df-4322-911c-ead14627af13","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"0f2df6ef-74f6-42d5-a6ff-fb830bac6bc1","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"1feb74f5-775b-4809-80bb-1757ff6070f9","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"d8afaaaf-0174-4a14-8ba1-59043a985e91","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"7fd72fd2-933b-4816-81d1-b9e9d86b7c2c","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"9cf8d438-a37d-4dea-b0dd-e15d187e19ed","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"01561aea-2cd9-4883-a29a-1a7b9df2e432","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"ec2f2f29-d5a6-4fa0-aa61-353d48af9d1b","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"efb477ff-7d55-4163-a55e-a3c64415f6a4","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"c2302058-2cde-443c-a67f-e77cfd399c2e","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"a61a4e1a-eaea-4814-a424-e3c1ca6e3818","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"cb0d722f-fb48-45b5-a612-ff2df9b2e56e","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"50d0749d-1dde-4d8d-8963-e605f3a89b7a","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"900c820c-2909-4065-be81-72d3ca7733de","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"fac7dd69-11b1-42ee-8c1e-a41c97f4c3ee","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"9a357d48-c648-4ddb-8c79-2dc97bea6bc9","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"d5de4b91-714d-4c4c-8619-4b88fae85046","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"3741b662-b556-4859-8720-4c79fd48d48a","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"72db3e84-44c5-4d07-86b1-5f866a897e83","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"2a701ccf-5692-4ff9-9e44-62d6ec379397","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"d2087e5d-f90d-4827-a383-ff828bd65c69","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"bbf41a2c-98d3-4b54-82fb-a51c8441957e","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"477eb38f-0505-42fc-85ee-7456d81833e8","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"d9f37084-fcee-49d5-a192-8c41ee5615ed","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"067eb4be-9082-4153-8754-3bd0106a16ef","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"e943752e-d665-4133-ab1e-b6089d61b68a","tags":[{"product":null,"links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d","name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]},{"id":"d573ae98-998d-4070-b089-620789e0738b","tags":[{"product":null,"links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce","name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99"}]}]
        
(Showing Draft Content)

Hierarchy Data

SpreadJS provides the hierarchy option in the data schema to define that the data source is hierarchical. The hierarchy option is inherited from the GC.Data.IDataSourceOption interface, which provides the following parameters:

  • type: Defines the hierarchy type. It could be a value out of the following: Parent, ChildrenPath, Level, or Custom.

  • column: Specifies the hierarchy key that will help to build the hierarchical data.

  • levelOffset: Indicates the level offset that can increase or decrease the level. Usually, the hierarchical level starts with 0.

  • outlineColumn: Inherited from GC.Data.IHierarchyOutlineColumnOptions, it specifies the column that can be shown as a hierarchy.

  • summaryFields: Inherited from GC.Data.IHierarchySummaryFieldCollection, it defines the formula for the fields.

  • parse: Inherited from GC.Data.IHierarchyCustomParseOptions, it parses the primary key of the custom hierarchy type to the parent key.

  • unparse: Inherited from GC.Data.IHierarchyCustomUnparseOptions, it builds the primary key of the custom hierarchy type.

Hierarchy Types

There are four types of hierarchy, Parent, Level, ChildrenPath, and Custom. Each of them can be configured when adding a Table to the data manager:

Parent Hierarchy

In Parent hierarchy, the hierarchy type 'Parent' is used, and the primary key is used to indicate the hierarchy id.

parent-hierarchy.png


The following code sample shows how to set parent hierarchy in the tablesheet:

<script>
        var spread = new GC.Spread.Sheets.Workbook(document.querySelector('#ss'), { sheetCount: 0 });
        initSpread(spread)
        function initSpread(spread) {
            var dataManager = spread.dataManager();

            initHierarchyParentType(spread, dataManager);

        }
        function initHierarchyParentType(spread, dataManager) {
            var table = dataManager.addTable("hierarchyParentTable", {
                data: initHierarchyParentData(),
                schema: {
                    hierarchy: {
                        type: 'Parent', // config the parent hierarchy type
                        column: 'parentId', // specify the column that could be used for building hierarchical view
                    },
                    columns: {
                        'id': { dataName: 'id', isPrimaryKey: true }, // the primary key is required
                        'parentId': { dataName: 'parentId', dataType: "reorder" },
                    }
                }
            });
            var sheet = spread.addSheetTab(0, "HierarchyParent", GC.Spread.Sheets.SheetType.tableSheet);
            sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
            sheet.options.allowAddNew = true;

            table.fetch().then(function () {
                var myView = table.addView("myView", [
                    {
                        value: "parentId", width: 200,
                        outlineColumn: {
                            showImage: true,
                            images: ['https://developer.mescius.com/spreadjs/demos/spread/source/images/task-1.png', 'https://developer.mescius.com/spreadjs/demos/spread/source/images/task-2.png', 'https://developer.mescius.com/spreadjs/demos/spread/source/images/task-3.png'],
                            expandIndicator: 'https://developer.mescius.com/spreadjs/demos/spread/source/images/increaseIndicator.png',
                            collapseIndicator: 'https://developer.mescius.com/spreadjs/demos/spread/source/images/decreaseIndicator.png',
                        },

                    },
                    { value: "id", width: 200 },
                ]);

                sheet.setDataView(myView);
            });
        }
        function initHierarchyParentData() {
            var data = [
                { id: 1, parentId: -1 },
                { id: 2, parentId: -1 },
                { id: 3, parentId: 1 },
                { id: 4, parentId: 1 },
                { id: 5, parentId: 2 },
                { id: 6, parentId: 2 },
                { id: 6, parentId: 2 },
            ];
            return data;
        }
</script>

ChildrenPath Hierarchy

In ChildrenPath hierarchy, the hierarchy type 'ChildrenPath' is used. In this case, the primary key is not required, however, it’s better to indicate the primary key for the child.

childrenpath-hierarchy.png


The following code sample shows how to set ChildrenPath hierarchy in tablesheet:

<script>
        var spread = new GC.Spread.Sheets.Workbook(document.querySelector('#ss'), { sheetCount: 0 });
        initSpread(spread)
        function initSpread(spread) {
            var dataManager = spread.dataManager();
            initHierarchyParentType(spread, dataManager);
        }
        function initHierarchyParentType(spread, dataManager) {
            var taskTable = dataManager.addTable("Tasks", {
                data: initHierarchyChildData(),
                schema: {
                    hierarchy: {
                        type: 'ChildrenPath',
                        column: 'TaskChildren',
                    },
                    columns: {
                        TaskName: { dataName: 'name' },
                        TaskChildren: { dataName: 'children' },
                        // other columns in the child
                    }
                }
            });
            var sheet = spread.addSheetTab(0, "HierarchyParent", GC.Spread.Sheets.SheetType.tableSheet);
            sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
            sheet.options.allowAddNew = true;
            taskTable.fetch().then(r => {
                var taskView = taskTable.addView('TaskView', [
                    {
                        value: 'TaskName', outlineColumn: true, width: "*" // this option indicates the column showing as outline column
                    }
                ]);
                sheet.setDataView(taskView);
            })
        }
        function initHierarchyChildData() {
            var data = [
                {
                    name: 'USA',
                    children: [
                        {
                            name: 'Texas',
                            children: [
                                {
                                    name: 'Houston',
                                },
                                {
                                    name: 'Dallas',
                                },
                                {
                                    name: 'San Antonio',
                                }
                            ]
                        }
                    ]
                },
                {
                    name: 'India',
                    children: [
                        {
                            name: 'UP',
                            children: [
                                { name: 'Noida' },
                                { name: 'Ghaziabad' },
                                { name: 'Agra' },
                            ]
                        }
                    ]
                }
            ]
            return data;
        }
</script>

Level Hierarchy

In Level hierarchy, the hierarchy type 'Level' is used. In this case as well, the primary key is not required, however it’s better to indicate the primary key.

level-hierarchy.png


The following code sample shows how to set level hierarchy in the tablesheet:

<script>
        var spread = new GC.Spread.Sheets.Workbook(document.querySelector('#ss'), { sheetCount: 0 });
        initSpread(spread)
        function initSpread(spread) {
            var dataManager = spread.dataManager();

            initHierarchyParentType(spread, dataManager);
        }
        function initHierarchyParentType(spread, dataManager) {
            var taskTable = dataManager.addTable("Tasks", {
                data: initHierarchyLevelData(),
                schema: {
                    hierarchy: {
                        type: 'Level',
                        column: 'TaskLevel',
                    },
                    columns: {
                        TaskName: { dataName: 'name' },
                        TaskId: { dataName: 'id', isPrimaryKey: true }, // using primary key to indicate the hierarchy id optionally if exist
                        TaskLevel: { dataName: 'level' },
                    }
                }
            });
            var sheet = spread.addSheetTab(0, "HierarchyParent", GC.Spread.Sheets.SheetType.tableSheet);
            sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
            sheet.options.allowAddNew = true;
            taskTable.fetch().then(r => {
                var taskView = taskTable.addView('TaskView', [
                    {
                        value: 'TaskName', outlineColumn: true// this option indicates the column showing as outline column
                    }
                ]);

                sheet.setDataView(taskView);
            })
        }
        function initHierarchyLevelData() {
            var data = [
                { name: 'USA', level: -1, id: 1 },
                { name: 'Texas', level: 0, id: 2 },
                { name: 'Houston', level: 1, id: 3 },
                { name: 'California', level: 0, id: 4 },
                { name: 'San Francisco', level: 1, id: 5 },
                { name: 'Los Angeles', level: 1, id: 6 },
            ];
            return data;
        }
 </script>

Custom Hierarchy

In Custom Hierarchy, the hierarchy type 'Custom' is used. Here, the primary key is optional.

custom-hierarchy.png

The following code sample shows how to set custom hierarchy in tablesheet:

<script>
        var spread = new GC.Spread.Sheets.Workbook(document.querySelector('#ss'), { sheetCount: 0 });
        initSpread(spread)
        function initSpread(spread) {
            var dataManager = spread.dataManager();

            initHierarchyParentType(spread, dataManager);

        }
        function initHierarchyParentType(spread, dataManager) {
            var taskTable = dataManager.addTable("Tasks", {
                data: initHierarchyChildData(),
                schema: {
                    hierarchy: {
                        type: 'Custom',
                        column: 'id',
                        parse: function (options) {
                            // parse the primary key "1.1.1" to "1.1"
                            // the returned value will be treated as parentId
                            let seg = options.data.TaskId.split('.');
                            return seg.slice(0, seg.length - 1).join('.')
                        },
                        unparse: function (options) {
                            let parentIds, currentIndex = options.index, parentData = options.parentData, parentKey = parentData && parentData.TaskId;
                            if (parentKey) {
                                let sp = parentKey.split('.');
                                parentIds = sp;
                            } else {
                                parentIds = [];
                            }
                            parentIds.push(currentIndex + 1);
                            return parentIds.join('.');
                        }
                    },
                    columns: {
                        TaskName: { dataName: 'name' },
                        TaskId: { dataName: 'id', isPrimaryKey: true }, // using primary key to indicate the hierarchy id optionally
                    }
                }
            });
            var sheet = spread.addSheetTab(0, "HierarchyParent", GC.Spread.Sheets.SheetType.tableSheet);
            sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
            sheet.options.allowAddNew = true;

            taskTable.fetch().then(r => {
                var taskView = taskTable.addView('TaskView', [
                    {
                        value: 'TaskName', outlineColumn: true, width: "*" // this option indicates the column showing as outline column
                    }
                ]);
                sheet.setDataView(taskView);
            })
        }
        function initHierarchyChildData() {
            var data = [
                {
                    id: '1', name: "1"
                },
                { id: '2', name: '2' },
                { id: '1.1', name: "1.1" },
                { id: '1.1.1', name: '1.1.1' },
                { id: '2.1', name: '2.1' }
            ]
            return data;
        }
</script>

Hierarchy Operations

TableSheet supports the following hierarchical operations:

  • Promote the hierarchy data level of the specified row

TableSheets let users promote the hierarchy data level by specified index using the promoteHierarchyLevel(row: number) method.

tableSheet.promoteHierarchyLevel(8);
  • Demote the hierarchy data level of the specified row

TableSheets let users demote the hierarchy data level of the specified row using the demoteHierarchyLevel(row: number, withChildren?: boolean) method.

tableSheet.demoteHierarchyLevel(8);
  • Move the hierarchy data up by the specified row

TableSheets let users move the hierarchy data by specified index up using the moveUp(row: number) method.

tableSheet.moveUp(8);
  • Move the hierarchy data down by the specified row

TableSheets let users move the hierarchy data up by the specified index using the moveDown(row: number) method.

tableSheet.moveDown(8);
  • Add a record after the selected row

TableSheets let users add a new row after the specified row using the addHierarchyItemAfter(row: number, rowData: any) method.

tableSheet.addHierarchyItemAfter(8, {id: 8, name: "grapecity"});
  • Add a record above the selected row

TableSheets let users add record above the selected row using the addHierarchyItemAbove(row: number, rowData: any) method. The added record will replace the position of the selected record, and the selected record will be the child of the added record.

tableSheet.addHierarchyItemAbove(8, {id: 8, name: "grapecity"});
  • Add a record below the selected row

Tablesheet let users add a new row data as the child of the specified row using the addHierarchyItemBelow(row: number, rowData: any) method. You can add a record below the selected row, and the added record will be the last child of the selected record.

tableSheet.addHierarchyItemBelow(8, {id: 8, name: "grapecity"});
  • Delete a record

TableSheet let users delete the record and its sub-records.

  • Expand a record

This menu item appears while clicking the column header. It expands all hierarchy levels using the expandAllHierarchyLevels() method.

tableSheet.expandAllHierarchyLevels();
  • Collapse a record

The menu item appears while clicking the column header, and it collapses all the hierarchy levels using the collapseAllHierarchyLevels() method.

tableSheet.collapseAllHierarchyLevels();

Toggle Menu Item Visibility

The menu items of promote, demote, move up/down, add before/after/above/below, and expand/collapse levels can be displayed or hidden at runtime.

The snapshot depicts displaying the following menu items while clicking the row header.

row-rightclick.png


The snapshot depicts displaying the following menu items while right clicking the column header.

column-rightclick.png


The code sample shows how to toggle the menu item visibility.

// show the menu items for hierarchical records
sheet.options.menuItemVisibility = {
// the options below be on the row header
promoteMenuItemVisible: true,
demoteMenuItemVisible: true,
// the options below be on the column header
expandAllLevelMenuItemVisible: true,
collapseAllLevelMenuItemVisible: true,
expandToLevelMenuItemVisible: true,
// the options below be on the row header
// and the menu items be enable for the dataType of the column be rowOrder
moveUpMenuItemVisible: true,
moveDownMenuItemVisible: true,
addBeforeMenuItemVisible: true,
addAfterMenuItemVisible: true,
addAboveMenuItemVisible: true,
addBelowMenuItemVisible: true,
};

Note: Few features like groupBy, pinning rows and resetRow may not work well with the hierarchy data.

Using SpreadJS Designer

The hierarchy data is also supported in the SpreadJS Designer using the Columns tab of Data Source and TableSheet ribbon. You can configure hierarchy type, summary formula, outline column images, etc.


datasource-hierarchy-designer


The Hierarchy section provides the following options:

  • Hierarchy Type: The type can be either the Parent, ChildrenPath, or Level.


    hierarchy-type-design.png

  • Summary Formula: Users can input the hierarchy summary formula for the specified column of the table.

    To learn more about the Summary formula, see Formulas topic.

  • Outline Column: The Outline Column can be used to customize the display of the outline column.

You can also configure the visibilities of hierarchy operation menu items in the TABLE SHEET DESIGN ribbon using the Menu Item Visibility button.

menu-item-visibility.png

Click the Menu Item Visibility button to open the Menu Item Visibility dialog.

menu-visible-dialog.png