项目切片器

Spread Sheets提供项目切片器, 项目切片器实现了与内置表格切片器相同的筛选界面,可以通过简单的设置在SpreadJS外部完成数据的筛选。

项目切片器是一个独立的切片器控件,并不依赖Spread Sheets,你可以在页面的任何地方添加项目切片器。 项目切片器需要与切片器数据模块相配合(包括通用切片器数据,表格切片器数据或者其他基于通用切片器数据实现的切片器数据)。如果项目切片器与表格切片器配合使用,当项目切片器发生筛选动作,表格上的数据也会随之同步。 你可以通过以下步骤构建自己的项目切片器: 构建数据源和通用切片器数据 获取列数据并构建呈现界面。 获取筛选结果并更新呈现界面。 构建项目切片器,并将通用切片器数据附加在切片器上,并将其置于页面的DOM树结构中。
window.onload = function () { var columnNames = ["Name", "Class", "Gender", "Math", "English", "Total Score"]; var data = [["Student1", 1, "female", "69", "66", "135"], ["Student2", 1, "female", "99", "85", "184"], ["Student3", 1, "male", "78", "77", "155"], ["Student4", 1, "male", "54", "80", "134"], ["Student7", 2, "male", "87", "98", "185"], ["Student8", 2, "male", "78", "85", "163"], ["Student9", 2, "female", "100", "90", "190"], ["Student10", 2, "female", "68", "54", "122"], ["Student11", 2, "female", "97", "100", "197"], ["Student12", 2, "female", "81", "90", "171"], ["Student15", 2, "female", "90", "68", "158"], ["Student16", 3, "male", "86", "70", "156"], ["Student17", 3, "male", "88", "89", "177"], ["Student18", 3, "male", "54", "80", "134"], ["Student19", 3, "male", "81", "75", "156"], ["Student23", 3, "female", "78", "98", "176"], ["Student24", 3, "female", "90", "98", "188"], ["Student25", 3, "male", "60", "30", "90"], ["Student26", 3, "female", "0", "0", "0"], ["Student27", 3, "female", "100", "100", "200"], ["Student28", 3, "male", "89", "78", "167"] ]; var slicerData = new GC.Spread.Slicers.GeneralSlicerData(data, columnNames); var onFiltered = slicerData.onFiltered; slicerData.onFiltered = function () { onFiltered.call(slicerData); refreshList(slicerData); } var nameSlicer = new GC.Spread.Sheets.Slicers.ItemSlicer("Name", slicerData, "Name"); nameSlicer.height(200); nameSlicer.width(180) nameSlicer.columnCount(2); document.getElementById('nameSlicerHost').appendChild(nameSlicer.getDOMElement()); var classSlicer = new GC.Spread.Sheets.Slicers.ItemSlicer("Class", slicerData, "Class"); classSlicer.height(200); classSlicer.width(180) document.getElementById('classSlicerHost').appendChild(classSlicer.getDOMElement()); initList(data, columnNames); }; function initList(data, columnNames) { var tableStr = '<tr>'; for (var i = 0; i < columnNames.length; i++) { tableStr += "<th>" + columnNames[i] + "</th>"; } tableStr += '</tr>'; for (var i = 0; i < data.length; i++) { tableStr += "<tr>"; for (var j = 0; j < data[i].length; j++) { tableStr += "<td>" + data[i][j] + "</td>"; } tableStr += "</tr>"; } var table = document.createElement('table'); table.innerHTML = tableStr; table.border = 1; table.cellPadding = 0; table.cellSpacing = 0; document.getElementById('ss').appendChild(table); } function refreshList(slicerData) { var filteredRowIndexs = slicerData.getFilteredRowIndexes(); var trs = document.getElementsByTagName('tr'); for (var i = 0; i < slicerData.data.length; i++) { if (filteredRowIndexs.indexOf(i) !== -1) { trs[i + 1].style.display = ''; } else { trs[i + 1].style.display = 'none'; } } }
<!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"> <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-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-slicers/dist/gc.spread.sheets.slicers.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 class="options-container"> <p class="desc">Click on the items in this slicer to filter by that class. You can Ctrl+Left Click to select multiple items.</p> <div id="nameSlicerHost" class="slicer"></div> <div id="classSlicerHost" class="slicer"></div> </div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: auto; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } .slicer { height: 220px; position: relative; } table th, table td { padding: 4px 8px; } .desc{ padding:2px 10px; background-color:#F4F8EB; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }