自定义列头样式

TableSheet 支持为不同的列设置不同的列头样式,包括字体、背景色、前景色、对齐方式和边框等。通过自定义列头样式,可以使表格更加美观,突出显示重要信息,提升用户体验。

概述 本 Demo 展示了如何在 TableSheet(集算表)中自定义列头的样式。通过在视图的 headerStyle 属性中设置不同的样式配置,可以为每一列的列头定义独特的字体、颜色、边框和文本对齐方式。 实现思路 创建 Workbook 并初始化数据管理器(DataManager) 通过数据管理器添加远程数据表 创建 TableSheet 并设置为不可添加新行 定义三种不同的列头样式(headerStyle):普通样式、橙色背景样式和蓝色边框样式 使用 addView 方法创建视图,为不同列设置不同的 headerStyle 将视图绑定到 TableSheet 代码解析 定义列头样式 这段代码定义了三种不同的列头样式: headerStyle:仅设置斜体字体 headerStyle2:设置字体、橙色背景、白色前景色、左对齐和黑色粗边框 headerStyle3:设置粗体字体和蓝色边框 创建视图并应用列头样式 这段代码在创建视图时为每一列指定列头样式: Id 和 CompanyName 列使用普通斜体样式 ContactName 和 ContactTitle 列使用橙色背景样式 Address 和 City 列使用蓝色边框样式 State 和 Region 列使用默认样式(未设置 headerStyle) 运行效果 表格显示供应商数据,包含 Id、Company Name、Contact、Title 等列 Id 和 Company Name 列的列头显示为斜体字 Contact 和 Title 列的列头显示为橙色背景、白色文字,带有黑色粗边框 Address 和 City 列的列头显示为蓝色边框和粗体字 State 和 Region 列使用默认列头样式 API 参考 addView 方法 name:视图名称 columns:列配置数组,每列可包含以下属性: value:数据字段名 caption:列标题文本 width:列宽(像素或星形尺寸) headerStyle:列头样式对象 headerStyle 配置 font:字体设置(字体样式、大小、字体名称) backColor:背景颜色 foreColor:前景色(文字颜色) hAlign:水平对齐方式(left、center、right) borderTop/borderLeft/borderRight/borderBottom:边框设置 color:边框颜色 style:边框样式(thin、thick、medium 等)
/*REPLACE_MARKER*/ /*DO NOT DELETE THESE COMMENTS*/ window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 0 }); initSpread(spread); }; function initSpread(spread) { spread.suspendPaint(); spread.clearSheets(); spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader; //init a data manager var tableName = "Supplier"; var baseApiUrl = getBaseApiUrl(); var apiUrl = baseApiUrl + "/" + tableName; var dataManager = spread.dataManager(); var myTable = dataManager.addTable("myTable", { remote: { read: { url: apiUrl } } }); //init a table sheet var sheet = spread.addSheetTab(0, "TableSheet1", GC.Spread.Sheets.SheetType.tableSheet); sheet.options.allowAddNew = false; //hide new row //column headers style var headerStyle = { font: "italic 10pt Calibri" }; var headerStyle2 = { font: "12pt Calibri", backColor : "#F7A711", foreColor:"white", hAlign:"left", borderTop: { color: "black", style: "thick" }, borderLeft: { color: "black", style: "thick" }, borderRight: { color: "black", style: "thick" }, }; var headerStyle3 = { font: "bold 14pt Calibri", borderTop: { color: "blue", style: "thick" }, borderLeft: { color: "blue", style: "thin" }, borderRight: { color: "blue", style: "thin" }, }; //bind a view to the table sheet myTable.fetch().then(function () { var view = myTable.addView("myView", [ { value: "Id", width: 80, headerStyle: headerStyle }, { value: "CompanyName", width: 200, caption :"Company Name", headerStyle: headerStyle }, { value: "ContactName", width: 150, caption :"Contact", headerStyle: headerStyle2 }, { value: "ContactTitle", width: 200, caption :"Title", headerStyle: headerStyle2 }, { value: "Address", width: 200, headerStyle: headerStyle3 }, { value: "City", width: 150, caption :"City", headerStyle: headerStyle3 }, { value: "State", width: 100, caption :"State" }, { value: "Region", width: 100, caption :"Region" } ]); sheet.setDataView(view); }); spread.resumePaint(); } function getBaseApiUrl() { return window.location.href.match(/http.+spreadjs\/SpreadJSTutorial\//)[0] + 'server/api'; }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/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$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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 class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div id="optionContainer" class="optionContainer"> </div> </div> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; }