功能列和新建行

功能列是tableSheet右侧的一个特殊列,它从UI行为上提供了一个非常方便进行增加/删除/保存/重置的方法。

新建行是tableSheet底端的一个特殊行,您可以通过编辑它来插入行。

这个示例使用了 自动同步模式,您可以在自动同步与批量更新的示例页面了解这个模式。 功能列 默认情况下,功能列处于启用状态,它处于tableSheet的右侧。 你可以通过sheet.actionColumn.options()来获得功能列选项。 你可以通过sheet.actionColumn.options(actionColumnOptions)来自定义,actionColumnOptions包含以下选项: 选项 类型 描述 visible boolean 功能列的可见性。 title string 在表单列头的功能列标题 width string 功能列宽度 actionButtons object 功能列按钮,包括删除, 保存和重置按钮。 actionButtons对象定义了 删除, 保存以及重置 按钮的显示属性, 每一个按钮的选项包括: 选项 类型 描述 visible boolean 按钮的可见性 title string 按钮的内容,您可以使用空字符串来隐藏按钮内容 icon GC.Spread.Sheets.ButtonImageType 按钮图标 以下示例展示了如何隐藏按钮内容: 新建行 新建行位于tableSheet底部,默认处于启用状态。您可以通过以下代码隐藏或者显示新建行:
var tableName = "Employee"; var baseApiUrl = getBaseApiUrl(); var apiUrl = baseApiUrl + "/" + tableName; var sheet; window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 0 }); initSpread(spread); bindEvents(); syncOptions(); }; function initSpread(spread) { spread.suspendPaint(); spread.clearSheets(); spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader; //init a data manager var dataManager = spread.dataManager(); var myTable = dataManager.addTable("myTable", { remote: { read: { url: apiUrl }, update: { url: apiUrl, method: 'PUT' }, create: { url: apiUrl }, delete: { url: apiUrl } }, autoSync: true, onFailed: function(type, args) { console.log("error", type, args); } }); //init a table sheet sheet = spread.addSheetTab(0, "MyTableSheet", GC.Spread.Sheets.SheetType.tableSheet); sheet.actionColumn.options({ visible: true, title: "Actions", width: 90, actionButtons: { remove: { visible: true, title: "", icon: GC.Spread.Sheets.ButtonImageType.minus }, save: { visible: true, title: "", icon: GC.Spread.Sheets.ButtonImageType.ok }, reset: { visible: true, title: "", icon: GC.Spread.Sheets.ButtonImageType.clear } } }); //bind a view to the table sheet myTable.fetch().then(function() { var view = myTable.addView("myView", [ { value: "Id", width: 50, caption: "ID" }, { value: "FirstName", width: 100, caption: "First Name" }, { value: "LastName", width: 100, caption: "Last Name" }, { value: "HomePhone", width: 100, caption: "Phone" }, { value: "Notes", width: 100, caption: "Notes" } ]); //the View has all default columns of the Table sheet.setDataView(view); }); spread.resumePaint(); } function bindEvents() { var updateButton = document.getElementById('update'); updateButton.addEventListener('click', function() { applySettings(); }); var showButton = document.getElementById('show-button'); showButton.addEventListener('change', function() { sheet.options.allowAddNew = showButton.checked; }); } function syncOptions() { if (sheet) { var options = sheet.actionColumn.options(); setProperty('visible', 'checked', options.visible); setProperty('title', 'value', options.title); setProperty('width', 'value', options.width); setProperty('remove-visible', 'checked', options.actionButtons.remove.visible); setProperty('remove-title', 'value', options.actionButtons.remove.title); setProperty('remove-icon', 'value', options.actionButtons.remove.icon); setProperty('save-visible', 'checked', options.actionButtons.save.visible); setProperty('save-title', 'value', options.actionButtons.save.title); setProperty('save-icon', 'value', options.actionButtons.save.icon); setProperty('reset-visible', 'checked', options.actionButtons.reset.visible); setProperty('reset-title', 'value', options.actionButtons.reset.title); setProperty('reset-icon', 'value', options.actionButtons.reset.icon); } } function applySettings() { var settings = { visible: getProperty('visible', 'checked'), title: getProperty('title', 'value'), width: Number(getProperty('width', 'value')), actionButtons: { remove: { visible: getProperty('remove-visible', 'checked'), title: getProperty('remove-title', 'value'), icon: Number(getProperty('remove-icon', 'value')) }, save: { visible: getProperty('save-visible', 'checked'), title: getProperty('save-title', 'value'), icon: Number(getProperty('save-icon', 'value')) }, reset: { visible: getProperty('reset-visible', 'checked'), title: getProperty('reset-title', 'value'), icon: Number(getProperty('reset-icon', 'value')) } } } if (sheet) { sheet.actionColumn.options(settings); } } function getProperty(domId, prop) { return document.getElementById(domId)[prop]; } function setProperty(domId, prop, value) { document.getElementById(domId)[prop] = value; } 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/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/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/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="options-container" class="options-container"> <fieldset> <legend>Action Column</legend> <div class="field-line"> <span>visible: </span><input type="checkbox" value="true" id="visible" /> </div> <div class="field-line"> <span>title: </span><input type="text" value="Actions" id="title" /> </div> <div class="field-line"> <span>width: </span><input type="text" value="100" id="width" /> </div> <fieldset> <legend>Remove Button</legend> <div class="field-line"> <span>visible: </span><input type="checkbox" value="true" id="remove-visible" /> </div> <div class="field-line"> <span>title: </span><input type="text" value="" id="remove-title" /> </div> <div class="field-line"> <span>icon: </span><select id="remove-icon"> <option value="0">none</option> <option value="1">custom</option> <option value="2">clear</option> <option value="3">cancel</option> <option value="4">ok</option> <option value="5">dropdown</option> <option value="6">ellipsis</option> <option value="7">left</option> <option value="8">right</option> <option value="9">plus</option> <option value="10">minus</option> <option value="11">undo</option> <option value="12">redo</option> <option value="13">search</option> <option value="14">separator</option> <option value="15">spinLeft</option> <option value="16">spinRight</option> <option value="17">collapse</option> <option value="18">expand</option> </select> </div> </fieldset> <fieldset> <legend>Save Button</legend> <div class="field-line"> <span>visible: </span><input type="checkbox" value="true" id="save-visible" /> </div> <div class="field-line"> <span>title: </span><input type="text" value="" id="save-title" /> </div> <div class="field-line"> <span>icon: </span><select id="save-icon"> <option value="0">none</option> <option value="1">custom</option> <option value="2">clear</option> <option value="3">cancel</option> <option value="4">ok</option> <option value="5">dropdown</option> <option value="6">ellipsis</option> <option value="7">left</option> <option value="8">right</option> <option value="9">plus</option> <option value="10">minus</option> <option value="11">undo</option> <option value="12">redo</option> <option value="13">search</option> <option value="14">separator</option> <option value="15">spinLeft</option> <option value="16">spinRight</option> <option value="17">collapse</option> <option value="18">expand</option> </select> </div> </fieldset> <fieldset> <legend>Reset Button</legend> <div class="field-line"> <span>visible: </span><input type="checkbox" value="true" id="reset-visible" /> </div> <div class="field-line"> <span>title: </span><input type="text" value="" id="reset-title" /> </div> <div class="field-line"> <span>icon: </span><select id="reset-icon"> <option value="0">none</option> <option value="1">custom</option> <option value="2">clear</option> <option value="3">cancel</option> <option value="4">ok</option> <option value="5">dropdown</option> <option value="6">ellipsis</option> <option value="7">left</option> <option value="8">right</option> <option value="9">plus</option> <option value="10">minus</option> <option value="11">undo</option> <option value="12">redo</option> <option value="13">search</option> <option value="14">separator</option> <option value="15">spinLeft</option> <option value="16">spinRight</option> <option value="17">collapse</option> <option value="18">expand</option> </select> </div> </fieldset> <div class="field-line"> <input type="button" id="update" value="Apply Settings"> </div> </fieldset> <fieldset> <legend>New Row</legend> <div class="field-line"> <span>visible: </span><input type="checkbox" checked="checked" id="show-button" /> </div> </fieldset> </div> </div> </html>
body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } fieldset { padding: 6px; margin: 0; margin-top: 10px; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } fieldset span, fieldset input, fieldset select { display: inline-block; text-align: left; } fieldset span { width: 50px; } fieldset input[type=text] { width: calc(100% - 58px); } fieldset input[type=button] { width: 100%; text-align: center; } fieldset select { width: calc(100% - 50px); } .field-line { margin-top: 4px; }