Usage
The following code shows how to render a TableSheet with a heirarchy in a worksheet.
Features in This Demo
hierarchy
context menu
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
spread.suspendPaint();
var sheet = spread.getActiveSheet();
initTableSheet(spread, function(tableSheet) {
spread.setActiveSheet(sheet.name());
var dataRange = registerTableSheetIntoWorksheet(tableSheet, sheet);
beautifySheet(sheet, dataRange);
spread.undoManager().clear();
});
spread.resumePaint();
}
async function initTableSheet(spread, callback) {
spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader;
//init a data manager
var dataManager = spread.dataManager();
var apiUrl = getBaseApiUrl() + "/Hierarchy_Data";
var table = dataManager.addTable("hierarchyParentTable", {
remote: {
read: {
url: apiUrl
}
},
autoSync: true,
schema: {
hierarchy: {
type: 'Parent', // config the parent hierarchy type
column: 'TaskParentId', // specify the column that could be used for building hierarchical view
},
columns: {
'TaskId':{ dataName: 'id', isPrimaryKey: true}, // the primary key is required
'TaskParentId':{ dataName: 'parentId'},
'TaskTitle':{ dataName: 'title'},
'TaskOwner':{ dataName: 'owner'},
'StartDate':{ dataName: 'startDate'},
'DueDate':{ dataName: 'dueDate'},
'TaskComplete':{ dataName: 'complete'},
'TaskOrder':{ dataName: 'rowOrder', dataType: 'rowOrder'}, // if dataType be rowOrder, the records could be moved and added
}
}
});
var tableSheet = spread.addSheetTab(0, "HierarchyParent", GC.Spread.Sheets.SheetType.tableSheet);
tableSheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader);
tableSheet.options.allowAddNew = true;
// show the menu items for hierarchical records
tableSheet.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,
};
await table.fetch();
var myView = table.addView("myView", [
{
value: "TaskTitle", width: 200,
outlineColumn: {
showImage: true,
images: ['$DEMOROOT$/spread/source/images/task-1.png', '$DEMOROOT$/spread/source/images/task-2.png', '$DEMOROOT$/spread/source/images/task-3.png'],
expandIndicator: '$DEMOROOT$/spread/source/images/increaseIndicator.png',
collapseIndicator: '$DEMOROOT$/spread/source/images/decreaseIndicator.png',
}
},
{ value: "TaskOwner", width: 200 },
{ value: "StartDate", width: 200 },
{ value: "DueDate", width: 200 },
{ value: "TaskComplete", width: 200 },
]);
spread.suspendPaint();
tableSheet.setDataView(myView);
callback && callback(tableSheet);
spread.resumePaint();
return tableSheet;
}
function registerTableSheetIntoWorksheet (tableSheet, sheet) {
var name = tableSheet.name() + "_DataRange";
var row = 1;
var col = 1;
var dataRange = sheet.dataRanges.add(name, tableSheet.name(), new GC.Spread.Sheets.Range(row, col, -1, -1));
return dataRange;
}
function beautifySheet(sheet, dataRange) {
var range = dataRange.range();
for (var i = range.row; i < range.row + range.rowCount; i++) {
sheet.autoFitRow(i);
}
for (var i = range.col; i < range.col + range.colCount; i++) {
sheet.autoFitColumn(i);
}
sheet.setColumnWidth(1, 30);
}
function getBaseApiUrl() {
return window.location.href.match(/http.+spreadjs\/demos\//)[0] + 'server/api';
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- Promise Polyfill for IE, https://www.npmjs.com/package/promise-polyfill -->
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="ss"></div>
</html>
body, html {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
margin: 0;
padding: 0;
}
#ss {
width: 100%;
height: 100%;
}